1. Introduction
  2. Guidance
  3. 1. Get started
  4. 2. Bundles
    1. 2.1. All-in-One Library for Browsers
    2. 2.2. All-in-One Library for Node.js
  5. 3. Compilers
    1. 3.1. (Pre-)Compiler in CLI
    2. 3.2. Compiler in Rust
    3. 3.3. Compiler in Node.js
    4. 3.4. Compiler in Wasm (Web)
  6. 4. Renderers
    1. 4.1. Renderer in Rust
    2. 4.2. Renderer in Node.js
    3. 4.3. Renderer in Wasm (Web)
    4. 4.4. Renderer in React
    5. 4.5. Renderer in Solid
    6. 4.6. Renderer in Angular
    7. 4.7. Renderer in Vue3
    8. 4.8. Hexo Plugin
  7. 5. Samples
    1. 5.1. Static, Responsive rendering
    2. 5.2. Streaming, Incremental rendering
    3. 5.3. Serverless rendering
  8. 6. Trouble Shooting
  9. Project samples
  10. 8. shiroa
  11. 9. typst-preview
  12. 10. hexo-renderer-typst
  13. References
  14. 13. Routing to Renferences

reflexo-typst Documentation

Claim: the Typst compiling functions is provided by official typst

The most simple examples always work with snippet utility library, an all-in-one library with simplified API interfaces:


        
import { $typst } from '@myriaddreamin/typst.ts';

        
console.log((await $typst.svg({

        
  mainContent: 'Hello, typst!' })).length);

        
// :-> 7317

        
import { $typst } from '@myriaddreamin/typst.ts';

        
console.log((await $typst.svg({

        
  mainContent: 'Hello, typst!' })).length);

        
// :-> 7317

Please check All-in-one (Simplified) Library for Browsers for more details.

Quick example for the harder way to serverless compiler:


        
import { createTypstCompiler } from '@myriaddreamin/typst.ts';

        


        
const mainFilePath = '/main.typ';

        
const cc /* compiler */ = createTypstCompiler();

        
await cc.init();

        
cc.addSource(mainFilePath, 'Hello, typst!');

        
await cc.compile({ mainFilePath });

        
import { createTypstCompiler } from '@myriaddreamin/typst.ts';

        


        
const mainFilePath = '/main.typ';

        
const cc /* compiler */ = createTypstCompiler();

        
await cc.init();

        
cc.addSource(mainFilePath, 'Hello, typst!');

        
await cc.compile({ mainFilePath });

For tree-shaking, you should import it with longer path:


        
import { createTypstCompiler } from '@myriaddreamin/typst.ts/compiler';

        
import { createTypstCompiler } from '@myriaddreamin/typst.ts/compiler';

#

Add or remove source/binary files

You can also use the {map,unmap,reset}Shadow function to manipulate any text or binary file data for typst compiler. They will shadow the file access from provided access model directly in memory.

The mapShadow(path: string, content: Uint8Array): void; resembles addSource(path: string, source: string): void;, but retrieves some binary data without guessing the underlying encoding.

Example usage:


        
const encoder = new TextEncoder();

        
// add a json file (utf8)

        
compiler.mapShadow('/assets/data.json', encoder.encode(jsonData));

        
// remove a json file

        
compiler.unmapShadow('/assets/data.json');

        
// clean up all shadow files (Note: this function will also clean all files added by `addSource`)

        
compiler.resetShadow();

        


        
// add an image file

        
const pngData = await fetch(...).arrayBuffer();

        
compiler.mapShadow('/assets/tiger.png', new Uint8Array(pngData));

        
const encoder = new TextEncoder();

        
// add a json file (utf8)

        
compiler.mapShadow('/assets/data.json', encoder.encode(jsonData));

        
// remove a json file

        
compiler.unmapShadow('/assets/data.json');

        
// clean up all shadow files (Note: this function will also clean all files added by `addSource`)

        
compiler.resetShadow();

        


        
// add an image file

        
const pngData = await fetch(...).arrayBuffer();

        
compiler.mapShadow('/assets/tiger.png', new Uint8Array(pngData));

#

Specify output format

Export document as Vector Format which can then load to the renderer to render the document.


        
const artifactData = await compiler.compile({

        
  mainFilePath: '/main.typ',

        
  // the default value of format field:

        
  // format: 'vector',

        
});

        
const artifactData = await compiler.compile({

        
  mainFilePath: '/main.typ',

        
  // the default value of format field:

        
  // format: 'vector',

        
});

#

Specify extra initialization options

You must specify the extra init options when calling the init function. For example, to load the wasm module from a custom path:


        
await cc.init({

        
  getModule: () =>

        
    '/path/to/typst_ts_web_compiler_bg.wasm',

        
});

        
await cc.init({

        
  getModule: () =>

        
    '/path/to/typst_ts_web_compiler_bg.wasm',

        
});