Skip to content

Commit

Permalink
feat(template): add node compile package example
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin committed Oct 30, 2023
1 parent 1c7cd9d commit c7b2bb5
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 17 deletions.
9 changes: 5 additions & 4 deletions packages/templates/node.js-compiler-next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
"@myriaddreamin/typst-ts-renderer": "^0.4.0-rc6",
"@myriaddreamin/typst-ts-web-compiler": "^0.4.0-rc6",
"@myriaddreamin/typst.ts": "^0.4.0-rc6",
"node-fetch": "3.3.2"
"node-fetch": "3.3.2",
"sync-request-curl": "^2.1.9"
},
"devDependencies": {
"@types/web": "^0.0.99",
"@types/node": "^20.6.3",
"typescript": "=5.0.4",
"tslib": "2.5.2"
"@types/web": "^0.0.99",
"tslib": "2.5.2",
"typescript": "=5.0.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import * as path from 'path';
import { HttpsProxyAgent } from 'https-proxy-agent';

export async function cachedFontInitOptoins() {
export async function cachedFontInitOptions() {
const fetcher = (await import('node-fetch')).default;
const dataDir =
process.env.APPDATA ||
Expand Down
77 changes: 77 additions & 0 deletions packages/templates/node.js-compiler-next/src/main.package.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import * as _1 from '@myriaddreamin/typst-ts-renderer';

import {
createTypstCompiler,
createTypstRenderer,
FetchPackageRegistry,
} from '@myriaddreamin/typst.ts';
import { MemoryAccessModel } from '@myriaddreamin/typst.ts/dist/cjs/fs/memory.cjs';
import {
withAccessModel,
withPackageRegistry,
} from '@myriaddreamin/typst.ts/dist/cjs/options.init.cjs';
import { PackageSpec } from '@myriaddreamin/typst.ts/dist/cjs/internal.types.cjs';
import { cachedFontInitOptions } from './cached-font-middleware';
import request from 'sync-request-curl';
import { writeFileSync } from 'fs';

class NodeFetchPackageRegistry extends FetchPackageRegistry {
pullPackageData(path: PackageSpec): Uint8Array | undefined {
const response = request('GET', this.resolvePath(path), {
insecure: true,
});

if (response.statusCode === 200) {
return response.getBody(undefined);
}
return undefined;
}
}

async function main(coordinate: { x: number; y: number }) {
let typstCode: string = `#import "@preview/cetz:0.1.2"
#set page(margin: (top: 0pt, bottom: 0pt, left: 0pt, right: 0pt))
#cetz.canvas({
import cetz.draw: *
grid((0, 0), (${coordinate.x}, ${coordinate.y}), step: 1, stroke: gray + 2pt)
})`;

const compiler = createTypstCompiler();
const accessModel = new MemoryAccessModel();
await compiler.init({
beforeBuild: [
...(await cachedFontInitOptions()).beforeBuild,
withAccessModel(accessModel),
withPackageRegistry(new NodeFetchPackageRegistry(accessModel)),
],
});

console.log('/main.typ');
compiler.addSource('/main.typ', typstCode);
let artifact: Uint8Array = await compiler.compile({
mainFilePath: '/main.typ',
format: 'vector',
});

const renderer = createTypstRenderer();
await renderer.init();

const svg = await renderer.runWithSession(async session => {
renderer.manipulateData({
renderSession: session,
action: 'reset',
data: artifact,
});
return renderer.renderSvgDiff({
renderSession: session,
});
});

return svg;
}

main({ x: 15, y: 15 }).then(svg => {
// console.log(svg);
writeFileSync('test.svg', svg);
});
4 changes: 2 additions & 2 deletions packages/templates/node.js-compiler-next/src/main.snippet.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { $typst } from '@myriaddreamin/typst.ts/dist/cjs/contrib/snippet.cjs';

import { cachedFontInitOptoins } from './cached-font-middleware';
import { cachedFontInitOptions } from './cached-font-middleware';

async function main() {
$typst.setCompilerInitOptions(await cachedFontInitOptoins());
$typst.setCompilerInitOptions(await cachedFontInitOptions());

const svg = await $typst.svg({
mainContent: 'Hello, typst!',
Expand Down
4 changes: 2 additions & 2 deletions packages/templates/node.js-compiler-next/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import { preloadFontAssets } from '@myriaddreamin/typst.ts/dist/cjs/options.init
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import * as path from 'path';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { cachedFontInitOptoins } from './cached-font-middleware';
import { cachedFontInitOptions } from './cached-font-middleware';

async function main() {
const compiler = createTypstCompiler();
await compiler.init(await cachedFontInitOptoins());
await compiler.init(await cachedFontInitOptions());

compiler.addSource('/main.typ', 'Hello, typst!');
const artifactData = await compiler.compile({
Expand Down
Loading

0 comments on commit c7b2bb5

Please sign in to comment.