-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(template): add node compile package example
- Loading branch information
1 parent
1c7cd9d
commit c7b2bb5
Showing
6 changed files
with
202 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/templates/node.js-compiler-next/src/main.package.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.