• Stars
    star
    355
  • Rank 119,764 (Top 3 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 1 year ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Run Typst in JavaScriptWorld.

typst.ts

Markdown | typst | Online SVG | Online Canvas

typst_ts::ci Apache-2 License

typst.ts is a project dedicated to bring the power of typst to the world of JavaScript. In short, it provides an typst::World implementation and several exporters to help compile and render your Typst document typically inside Browser Environment. In the scope of server-side rendering collaborated by $\textcolor{#3c9123}{\textsf{server}}$ and $\textcolor{#0074d9}{\textsf{browser}}$, there would be a data flow like this:

Data Flow

Specifically, it first typically presents a typst document in three forms:

  • Form1: Render to SVG and then embed it as a high-quality vectored image directly.

  • Form2: Preprocessed to a Vector Format artifact.

  • Form3: Manipulate a canvas element directly.

The Form2: Vector Format is developed specially for typst documents, and it has several fancy features:

Data Flow

So with Form2, you can continue rendering the document in different ways:

Static but responsive rendering

Example Application: single-file, typst-book and hexo-renderer-typst

A compressed artifact containing data for different theme and screen settings. The bundle size of artifacts is optimized for typst documents.

Incremental server-side rendering

Example Application: typst-preview

Build a server for compilation with Compiler Service, streaming the artifact, and render it incrementally.

Serverless client-side rendering

Example Application: single-file

Run the entire typst directly in browser, like typst.app.

Application

Installation (CLI)

Install latest CLI of typst.ts via cargo:

cargo install --locked --git https://github.com/Myriad-Dreamin/typst.ts typst-ts-cli

Or Download the latest release from GitHub Releases.

Installation (Packages)

The JavaScript packages are published on npm.

The rust crates are not published on crates.io yet, since it has the git dependency on typst.

Installation (All-in-one Bundle)

Download the latest bundle file from GitHub Releases.

Documentation

See Documentation.

Templates

Please check Templates and usage in Get Started.

Minimal Example

Note: In default, all-in-one.bundle.js will download the font assets from GitHub in browser, so you need to connect to the Internet.

Download all-in-one.bundle.js from GitHub Releases and start a local server with following content of index.html:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Svg Document</title>
    <script type="module" src="http://localhost:12345/all-in-one.bundle.js" id="typst"></script>
  </head>
  <body>
    <textarea id="input" style="width: 100%"></textarea>
    <div id="content"></div>
    <script>
      const input = document.getElementById('input');
      input.value = 'Hello, Typst!';
      document.getElementById('typst').addEventListener('load', function () {
        const compile = function (mainContent) {
          $typst.svg({ mainContent }).then(svg => {
            console.log(`rendered! SvgElement { len: ${svg.length} }`);
            // append svg text
            document.getElementById('content').innerHTML = svg;
          });
        };
        input.oninput = () => compile(input.value);
        compile(input.value);
      });
    </script>
  </body>
</html>

And you will see the result.

You can also load the all-in-one bundle file and wasm modules from jsdelivr:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Svg Document</title>
    <script
      type="module"
      src="https://cdn.jsdelivr.net/npm/@myriaddreamin/typst.ts/dist/esm/contrib/all-in-one-lite.bundle.js"
      id="typst"
    ></script>
  </head>
  <body>
    <textarea id="input" style="width: 100%"></textarea>
    <div id="content"></div>
    <script>
      const input = document.getElementById('input');
      input.value = 'Hello, Typst!';
      document.getElementById('typst').addEventListener('load', function () {
        $typst.setCompilerInitOptions({
          getModule: () =>
            'https://cdn.jsdelivr.net/npm/@myriaddreamin/typst-ts-web-compiler/pkg/typst_ts_web_compiler_bg.wasm',
        });
        $typst.setRendererInitOptions({
          getModule: () =>
            'https://cdn.jsdelivr.net/npm/@myriaddreamin/typst-ts-renderer/pkg/typst_ts_renderer_bg.wasm',
        });

        const compile = function (mainContent) {
          $typst.svg({ mainContent }).then(svg => {
            console.log(`rendered! SvgElement { len: ${svg.length} }`);
            // append svg text
            document.getElementById('content').innerHTML = svg;
          });
        };
        input.oninput = () => compile(input.value);
        compile(input.value);
      });
    </script>
  </body>
</html>

Develop projects along with a local built typst.ts

You can put your owned projects under the projects folder, and that yarn workspace will automatically identify your project. We recommend you to use git, Yarn, and turbo to manage your projects.

Example: link a project by git submodule

To develop core external projects, e.g. typst-preview, you could initialize them by command:

git submodule update --init --checkout projects/typst-preview
Example: build and run

Ensured that you have built typst.ts from source, you can build and run the project by (typst-preview as an example):

# install dependencies for project
yarn install --pure-lockfile
# build typst-preview and its dependencies
turbo build --filter=typst-preview
@myriaddreamin/typst-ts-renderer:build: cache hit, replaying logs bc0a0b151bd8eb6d
@myriaddreamin/typst.ts:build: cache hit, replaying logs 729cb43a3242b80
typst-preview-frontend:build: cache miss, executing 5ae30471e8957877
typst-preview-frontend:build: ...
typst-preview-frontend:build: ✓ built in 1.25s
typst-preview-frontend:build: Done in 4.57s.
typst-preview:build: cache miss, executing a1bd8ca8233f8a0c
typst-preview:build: ...
typst-preview:build: ✓ built in 1.01s
typst-preview:build: Done in 3.73s.

The project (typst-preview as an example) will cache and use the local built packages.

Build from source and check

Note: you could build from source with/without wasm-pack.

Note: see Troubleshooting WASM Build for (especially) Arch Linux users.

Note: Since we use turborepo for >=v0.4.0 development, if you are the earlier developer of typst.ts, please clean up all of your node_modules and dist folders before running the commands.

# Optional: download the font assets if you haven't done so.
$ git submodule update --init --recursive .
# build all of typescript packages
$ yarn install && yarn run build:pkg
# compile typst document for demo
$ cargo run --bin typst-ts-dev-server -- compile --compiler debug corpus --cat skyzh-cv
# start a local server
$ cargo run --bin typst-ts-dev-server -- run http --corpus ./fuzzers/corpora/

And open your browser to http://localhost:20810/.

You can also run yarn run build:core instead of yarn run build:pkg to build core library (@myriaddreamin/typst.ts) and avoid building the WASM modules from source.

Concept: Precompiler

The precompiler is capable of producing artifact outputs from a Typst project. Thet artifact outputs can be easily distributed to remote endpoints.

Install latest precompiler via cargo:

cargo install --locked --git https://github.com/Myriad-Dreamin/typst.ts typst-ts-cli

Or Download the latest release from GitHub Releases.

Concept: Renderer

The renderer accepts an input in artifact format and renders the document as HTML elements.

Import typst.ts in your project:

  • Using @myriaddreamin/typst.ts

    import { $typst } from '@myriaddreamin/typst.ts/dist/esm/contrib/snippet.mjs';
    const mainContent = 'Hello, typst!';
    
    console.log(await $typst.svg({ mainContent }));
    

    Specify correct path to the wasm modules if it complains.

    $typst.setCompilerInitOptions({ getModule: ... });
    $typst.setRendererInitOptions({ getModule: ... });
    

    The path option is likely required in browser but not in node.js.

    Further reading: Get Started with Typst.ts

  • Using @myriaddreamin/typst.react

    import { TypstDocument } from '@myriaddreamin/typst.react';
    
    export const App = (artifact: Uint8Array) => {
      return (
        <div>
          <h1>Demo: Embed Your Typst Document in React</h1>
          <TypstDocument fill="#343541" artifact={artifact} />
        </div>
      );
    };
    
  • Using @myriaddreamin/typst.angular

    In the module file of your awesome component.

    /// component.module.ts
    import { TypstDocumentModule } from '@myriaddreamin/typst.angular';
    

    Using directive typst-document in your template file.

    <typst-document fill="#343541" artifact="{{ artifact }}"></typst-document>
    
  • Using @myriaddreamin/typst.vue3

    Coming soon.

More Repositories

1

tinymist

Tinymist [ˈtaɪni mɪst] is an integrated language service for Typst [taɪpst].
Rust
498
star
2

shiroa

shiroa is a simple tool for creating modern online books in pure typst.
Rust
247
star
3

cosmo

Cosmo is a language replacing awful C++'s compile-time magics
Scala
5
star
4

aliali

Go
4
star
5

httpfs

filesystem like api to access any file via http or https protocol
TypeScript
4
star
6

influxdb-v2-client

c++11 flux api (influxdb v2)
C++
4
star
7

marktex

latex style extension for markdown
TypeScript
4
star
8

artisan

golang framework code generation
Go
4
star
9

tarus

Online Judge Engine Powered by runC, gVisor and other execution runtime
Go
4
star
10

topo

time graph
TypeScript
3
star
11

algorithm-tester

c++
C++
3
star
12

gdb-stack-trace

Python
3
star
13

cetz-editor

Simple visualized cetz editor
HTML
3
star
14

market

web course
Go
2
star
15

qemu-book

CSS
2
star
16

optee-for-hikey970

Makefile
1
star
17

arch-media-box-typst

Arch Linux 盒装安装媒介的小盒子
Typst
1
star
18

llvm-tutorial

C++
1
star
19

bmp-parser

simplest bmp encoder/decoder
Python
1
star
20

xp3-parser

decode kirikiri xp3 file into files
Go
1
star
21

marktex-vscode-language-support

MarkTeX Language Support for Visual Studio Code
1
star
22

net.io

Network Model Visualization
TypeScript
1
star
23

fuzzer-cov

fuzzer coverage tool, generate readable coverage report in html format
Python
1
star
24

PerfRdtsc

Use tsc on x86 Linux / Linux Kernel / Windows
C++
1
star
25

clusterfuzz-ex

Python
1
star
26

pycanalysis

Python
1
star
27

py-mconv

a command line tool for converting many formats of key-value data from files to files.
Python
1
star
28

ACM

C++
1
star
29

llvm-pascal-s

C++
1
star
30

gvm

Extensible virtual machine (toy).
Go
1
star