@r4ai/typst-ast-node 0.0.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +118 -0
- package/package.json +5 -1
- package/typst_ast.d.ts +28 -1
- package/typst_ast_bg.wasm +0 -0
package/README.md
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# typst-ast
|
|
2
|
+
|
|
3
|
+
A WebAssembly library that parses [Typst](https://typst.app/) source code and returns its AST as a JSON-serializable object. Built with Rust and [`typst-syntax`](https://crates.io/crates/typst-syntax), distributed as npm packages via `wasm-pack`.
|
|
4
|
+
|
|
5
|
+
## Packages
|
|
6
|
+
|
|
7
|
+
| Package | Target |
|
|
8
|
+
| ---------------------------------------------------------------------------- | ------------- |
|
|
9
|
+
| [`@r4ai/typst-ast-web`](https://www.npmjs.com/package/@r4ai/typst-ast-web) | Web (ESM, Browser) |
|
|
10
|
+
| [`@r4ai/typst-ast-node`](https://www.npmjs.com/package/@r4ai/typst-ast-node) | Node.js |
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
### Browser
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import init, { parse } from "@r4ai/typst-ast-web";
|
|
18
|
+
|
|
19
|
+
await init();
|
|
20
|
+
|
|
21
|
+
const result = parse("= Hello\n\nThis is *typst*.", { mode: "markup" });
|
|
22
|
+
console.log(result);
|
|
23
|
+
// {
|
|
24
|
+
// root: { kind: "Markup", range: [0, 26], text: null, children: [...] },
|
|
25
|
+
// errors: []
|
|
26
|
+
// }
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Node.js
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { parse } from "@r4ai/typst-ast-node";
|
|
33
|
+
|
|
34
|
+
const result = parse("#let x = 1 + 2", { mode: "code" });
|
|
35
|
+
console.log(result);
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### API
|
|
39
|
+
|
|
40
|
+
#### `parse(text, options?)`
|
|
41
|
+
|
|
42
|
+
Parses Typst source text and returns the AST.
|
|
43
|
+
|
|
44
|
+
**Parameters**
|
|
45
|
+
|
|
46
|
+
- `text: string` — Typst source code to parse
|
|
47
|
+
- `options.mode?: "markup" | "code" | "math"` — Parse mode (default: `"markup"`)
|
|
48
|
+
|
|
49
|
+
**Return value**
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
type ParseResult = {
|
|
53
|
+
root: Node;
|
|
54
|
+
errors: ParseError[];
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
type Node = {
|
|
58
|
+
kind: string; // SyntaxKind variant name (e.g. "Markup", "Heading", "Strong")
|
|
59
|
+
range: [number, number]; // [start, end] byte offsets
|
|
60
|
+
text: string | null; // leaf node text, null for inner nodes
|
|
61
|
+
children: Node[];
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
type ParseError = {
|
|
65
|
+
message: string;
|
|
66
|
+
range: [number, number];
|
|
67
|
+
};
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Development
|
|
71
|
+
|
|
72
|
+
### Prerequisites
|
|
73
|
+
|
|
74
|
+
- Rust (stable) with `wasm32-unknown-unknown` target
|
|
75
|
+
- Node.js 24
|
|
76
|
+
- pnpm 10
|
|
77
|
+
- [`wasm-pack`](https://rustwasm.github.io/wasm-pack/)
|
|
78
|
+
|
|
79
|
+
### Commands
|
|
80
|
+
|
|
81
|
+
```sh
|
|
82
|
+
# Install Node.js and pnpm via mise
|
|
83
|
+
mise install
|
|
84
|
+
|
|
85
|
+
# Install JS dependencies
|
|
86
|
+
pnpm install
|
|
87
|
+
|
|
88
|
+
# Build WASM packages (outputs to dist/web and dist/node)
|
|
89
|
+
pnpm run build:lib
|
|
90
|
+
|
|
91
|
+
# Lint
|
|
92
|
+
cargo fmt --check
|
|
93
|
+
cargo clippy -- -D warnings
|
|
94
|
+
pnpm run check
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Release
|
|
98
|
+
|
|
99
|
+
```sh
|
|
100
|
+
# Bump version and create a release commit + tag
|
|
101
|
+
bash scripts/release.sh 1.2.3
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Examples
|
|
105
|
+
|
|
106
|
+
### React app
|
|
107
|
+
|
|
108
|
+
A React-based example app is located at [`examples/app`](./examples/app).
|
|
109
|
+
|
|
110
|
+
```sh
|
|
111
|
+
cd examples/app
|
|
112
|
+
pnpm install
|
|
113
|
+
pnpm dev
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## License
|
|
117
|
+
|
|
118
|
+
MIT
|
package/package.json
CHANGED
package/typst_ast.d.ts
CHANGED
|
@@ -1,6 +1,33 @@
|
|
|
1
1
|
/* tslint:disable */
|
|
2
2
|
/* eslint-disable */
|
|
3
|
+
export interface SyntaxNode {
|
|
4
|
+
kind: string;
|
|
5
|
+
range: [number, number];
|
|
6
|
+
text?: string;
|
|
7
|
+
children: SyntaxNode[];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface ParseError {
|
|
11
|
+
message: string;
|
|
12
|
+
range: [number, number];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface ParseResult {
|
|
16
|
+
root: SyntaxNode;
|
|
17
|
+
errors: ParseError[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type ParseMode = "markup" | "code" | "math";
|
|
21
|
+
|
|
22
|
+
export interface ParseOptions {
|
|
23
|
+
mode?: ParseMode;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export declare function parse(
|
|
27
|
+
text: string,
|
|
28
|
+
options?: ParseOptions,
|
|
29
|
+
): ParseResult;
|
|
30
|
+
|
|
3
31
|
|
|
4
|
-
export function parse(text: string, options: any): any;
|
|
5
32
|
|
|
6
33
|
export function start(): void;
|
package/typst_ast_bg.wasm
CHANGED
|
Binary file
|