@junejs/pdf 0.2.1 → 0.2.2
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/dist/bidi-classes.d.ts +4 -0
- package/dist/bidi-classes.js +1698 -0
- package/dist/markdown-typst.js +125 -14
- package/dist/tools.js +1 -1
- package/package.json +2 -2
- package/src/bidi-classes.ts +99 -0
- package/src/markdown-typst.ts +145 -15
- package/src/tools.ts +1 -1
package/dist/markdown-typst.js
CHANGED
|
@@ -1,18 +1,28 @@
|
|
|
1
|
+
import { STRONG } from "./bidi-classes.js";
|
|
1
2
|
import { Lexer } from "marked";
|
|
2
3
|
//#region src/markdown-typst.ts
|
|
3
4
|
function markdownToTypst(markdown) {
|
|
4
|
-
return
|
|
5
|
+
return body(lex(markdown));
|
|
5
6
|
}
|
|
7
|
+
const lex = (markdown) => new Lexer({ gfm: true }).lex(markdown);
|
|
8
|
+
const body = (tokens, base = "ltr") => blocks(tokens, "\n\n", base).trim() + "\n";
|
|
6
9
|
function typstSource(doc, opts = {}) {
|
|
7
10
|
const lines = [];
|
|
8
11
|
const meta = [doc.title ? `title: ${str(doc.title)}` : "", doc.author ? `author: ${str(doc.author)}` : ""].filter(Boolean);
|
|
9
12
|
if (meta.length) lines.push(`#set document(${meta.join(", ")})`);
|
|
10
13
|
lines.push(`#set page(paper: ${str(doc.paper === "us-letter" ? "us-letter" : "a4")}, margin: (x: 2.2cm, y: 2.4cm), numbering: "1")`);
|
|
11
|
-
const
|
|
14
|
+
const tokens = lex(doc.markdown);
|
|
15
|
+
const lang = langArgs(doc.lang);
|
|
16
|
+
const base = lang.length ? RTL_LANGS.has(doc.lang.toLowerCase().split(/[-_]/)[0]) ? "rtl" : "ltr" : firstStrong(tokens) ?? "ltr";
|
|
17
|
+
const text = [
|
|
18
|
+
"size: 10.5pt",
|
|
19
|
+
...lang,
|
|
20
|
+
...lang.length === 0 && base === "rtl" ? ["dir: rtl"] : []
|
|
21
|
+
];
|
|
12
22
|
if (opts.fonts?.length) text.push(`font: (${opts.fonts.map(str).join(", ")},)`);
|
|
13
23
|
lines.push(`#set text(${text.join(", ")})`);
|
|
14
24
|
lines.push("#set par(justify: true, leading: 0.75em)", "#show heading: set block(above: 1.4em, below: 0.9em)", "#show link: set text(fill: rgb(\"#1f4e9e\"))", "#set table(stroke: 0.5pt + luma(170), inset: 6pt)", "#show raw.where(block: true): block.with(fill: luma(245), inset: 8pt, radius: 3pt, width: 100%)", "");
|
|
15
|
-
return lines.join("\n") +
|
|
25
|
+
return lines.join("\n") + body(tokens, base);
|
|
16
26
|
}
|
|
17
27
|
function langArgs(tag) {
|
|
18
28
|
if (!tag) return [];
|
|
@@ -21,10 +31,107 @@ function langArgs(tag) {
|
|
|
21
31
|
const region = rest.find((p) => /^[a-z]{2}$/.test(p));
|
|
22
32
|
return [`lang: ${str(lang)}`, ...region ? [`region: ${str(region)}`] : []];
|
|
23
33
|
}
|
|
24
|
-
|
|
25
|
-
|
|
34
|
+
const RTL_LANGS = /* @__PURE__ */ new Set([
|
|
35
|
+
"ar",
|
|
36
|
+
"dv",
|
|
37
|
+
"fa",
|
|
38
|
+
"he",
|
|
39
|
+
"ks",
|
|
40
|
+
"pa",
|
|
41
|
+
"ps",
|
|
42
|
+
"sd",
|
|
43
|
+
"ug",
|
|
44
|
+
"ur",
|
|
45
|
+
"yi"
|
|
46
|
+
]);
|
|
47
|
+
function strong(cp) {
|
|
48
|
+
let lo = 0;
|
|
49
|
+
let hi = STRONG.length / 3 - 1;
|
|
50
|
+
while (lo <= hi) {
|
|
51
|
+
const mid = lo + hi >> 1;
|
|
52
|
+
if (cp < STRONG[mid * 3]) hi = mid - 1;
|
|
53
|
+
else if (cp > STRONG[mid * 3 + 1]) lo = mid + 1;
|
|
54
|
+
else return STRONG[mid * 3 + 2] ? "rtl" : "ltr";
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function firstStrong(tokens) {
|
|
58
|
+
let isolated = 0;
|
|
59
|
+
for (const run of rendered(tokens)) for (const ch of run) {
|
|
60
|
+
const cp = ch.codePointAt(0);
|
|
61
|
+
if (cp === PARAGRAPH_END) isolated = 0;
|
|
62
|
+
else if (cp >= 8294 && cp <= 8296) isolated++;
|
|
63
|
+
else if (cp === 8297) isolated = Math.max(0, isolated - 1);
|
|
64
|
+
else if (!isolated) {
|
|
65
|
+
const dir = strong(cp);
|
|
66
|
+
if (dir) return dir;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
26
69
|
}
|
|
27
|
-
|
|
70
|
+
const PARAGRAPH_END = 8233;
|
|
71
|
+
const END = String.fromCodePoint(PARAGRAPH_END);
|
|
72
|
+
const BLOCK_TYPES = /* @__PURE__ */ new Set([
|
|
73
|
+
"paragraph",
|
|
74
|
+
"heading",
|
|
75
|
+
"code",
|
|
76
|
+
"blockquote",
|
|
77
|
+
"list",
|
|
78
|
+
"table"
|
|
79
|
+
]);
|
|
80
|
+
const endsParagraph = (t) => BLOCK_TYPES.has(t.type) || t.type === "html" && t.block;
|
|
81
|
+
function* rendered(tokens) {
|
|
82
|
+
for (const t of tokens) {
|
|
83
|
+
yield* renderedToken(t);
|
|
84
|
+
if (endsParagraph(t)) yield END;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function* renderedToken(t) {
|
|
88
|
+
const x = t;
|
|
89
|
+
switch (t.type) {
|
|
90
|
+
case "code":
|
|
91
|
+
yield t.text;
|
|
92
|
+
break;
|
|
93
|
+
case "html":
|
|
94
|
+
yield decodeEntities(htmlText(x.text ?? ""));
|
|
95
|
+
break;
|
|
96
|
+
case "image":
|
|
97
|
+
yield decodeEntities(t.text || "[image]");
|
|
98
|
+
break;
|
|
99
|
+
case "link":
|
|
100
|
+
yield* rendered(t.tokens);
|
|
101
|
+
break;
|
|
102
|
+
case "list":
|
|
103
|
+
for (const item of t.items) {
|
|
104
|
+
yield* rendered(item.tokens);
|
|
105
|
+
yield END;
|
|
106
|
+
}
|
|
107
|
+
break;
|
|
108
|
+
case "table": {
|
|
109
|
+
const tb = t;
|
|
110
|
+
for (const c of [...tb.header, ...tb.rows.flat()]) {
|
|
111
|
+
yield* rendered(c.tokens);
|
|
112
|
+
yield END;
|
|
113
|
+
}
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
case "space":
|
|
117
|
+
case "def":
|
|
118
|
+
case "hr":
|
|
119
|
+
case "checkbox": break;
|
|
120
|
+
default: if (x.tokens) yield* rendered(x.tokens);
|
|
121
|
+
else if (x.text) yield decodeEntities(x.text);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
function turned(content, dir) {
|
|
125
|
+
return content ? `#[#set text(dir: ${dir})\n${content}]` : content;
|
|
126
|
+
}
|
|
127
|
+
function blocks(tokens, sep = "\n\n", dir = "ltr", turns = () => true) {
|
|
128
|
+
return tokens.map((t) => {
|
|
129
|
+
const own = turns(t) ? firstStrong([t]) ?? dir : dir;
|
|
130
|
+
const out = block(t, own);
|
|
131
|
+
return own === dir ? out : turned(out, own);
|
|
132
|
+
}).filter((s) => s !== "").join(sep);
|
|
133
|
+
}
|
|
134
|
+
function block(t, dir) {
|
|
28
135
|
switch (t.type) {
|
|
29
136
|
case "heading": {
|
|
30
137
|
const h = t;
|
|
@@ -40,9 +147,9 @@ function block(t) {
|
|
|
40
147
|
const lang = c.lang?.trim().split(/\s+/)[0];
|
|
41
148
|
return `#raw(block: true, ${lang ? `lang: ${str(lang)}, ` : ""}${str(c.text)})`;
|
|
42
149
|
}
|
|
43
|
-
case "blockquote": return `#quote(block: true)[${blocks(t.tokens)}]`;
|
|
44
|
-
case "list": return list(t);
|
|
45
|
-
case "table": return table(t);
|
|
150
|
+
case "blockquote": return `#quote(block: true)[${blocks(t.tokens, "\n\n", dir)}]`;
|
|
151
|
+
case "list": return list(t, dir);
|
|
152
|
+
case "table": return table(t, dir);
|
|
46
153
|
case "hr": return "#line(length: 100%, stroke: 0.5pt + luma(170))";
|
|
47
154
|
case "html": return text(htmlText(t.text).trim());
|
|
48
155
|
case "space":
|
|
@@ -53,10 +160,10 @@ function block(t) {
|
|
|
53
160
|
}
|
|
54
161
|
}
|
|
55
162
|
}
|
|
56
|
-
function list(l) {
|
|
163
|
+
function list(l, dir) {
|
|
57
164
|
const items = l.items.map((item) => {
|
|
58
165
|
const body = item.tokens.filter((x) => x.type !== "checkbox");
|
|
59
|
-
return `[${item.task ? `#${str(item.checked ? "☑ " : "☐ ")};` : ""}${blocks(body, l.loose ? "\n\n" : "\n")}]`;
|
|
166
|
+
return `[${item.task ? `#${str(item.checked ? "☑ " : "☐ ")};` : ""}${blocks(body, l.loose ? "\n\n" : "\n", dir, (x) => x.type === "list")}]`;
|
|
60
167
|
});
|
|
61
168
|
const args = [`tight: ${!l.loose}`];
|
|
62
169
|
if (l.ordered) {
|
|
@@ -65,10 +172,14 @@ function list(l) {
|
|
|
65
172
|
}
|
|
66
173
|
return `#${l.ordered ? "enum" : "list"}(${args.join(", ")}, ${items.join(", ")})`;
|
|
67
174
|
}
|
|
68
|
-
function table(t) {
|
|
175
|
+
function table(t, dir) {
|
|
69
176
|
const align = t.align.map((a) => a ?? "auto");
|
|
70
|
-
const
|
|
71
|
-
|
|
177
|
+
const cell = (tokens, content) => {
|
|
178
|
+
const own = firstStrong(tokens) ?? dir;
|
|
179
|
+
return `[${own === dir ? content : turned(content, own)}]`;
|
|
180
|
+
};
|
|
181
|
+
const header = t.header.map((c) => cell(c.tokens, `#strong[${inlines(c.tokens)}]`));
|
|
182
|
+
const rows = t.rows.flatMap((row) => t.header.map((_, i) => row[i] ? cell(row[i].tokens, inlines(row[i].tokens)) : "[]"));
|
|
72
183
|
return [
|
|
73
184
|
"#table(",
|
|
74
185
|
` columns: ${t.header.length},`,
|
package/dist/tools.js
CHANGED
|
@@ -25,7 +25,7 @@ function pdfTools(opts) {
|
|
|
25
25
|
},
|
|
26
26
|
lang: {
|
|
27
27
|
type: "string",
|
|
28
|
-
description: "The document language as a BCP 47 tag, e.g. \"en\", \"zh-TW\", \"ja\"."
|
|
28
|
+
description: "The document language as a BCP 47 tag, e.g. \"en\", \"zh-TW\", \"ja\", \"ar\". It sets hyphenation, quotes, CJK line breaking and the page direction, right to left for Arabic, Hebrew, Persian or Urdu; pass it."
|
|
29
29
|
},
|
|
30
30
|
paper: {
|
|
31
31
|
type: "string",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@junejs/pdf",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "June — PDF output as an agent capability: a render_pdf tool over a replaceable PdfRenderer (@junejs/pdf-typst-wasm is June's).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"build": "tsdown"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@junejs/core": "^0.2.
|
|
27
|
+
"@junejs/core": "^0.2.2",
|
|
28
28
|
"marked": "^18.0.14"
|
|
29
29
|
},
|
|
30
30
|
"keywords": [
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// Generated by scripts/gen-bidi.ts from Unicode 17.0.0 DerivedBidiClass.txt — do not edit.
|
|
2
|
+
// Strong bidi classes as flat [start, end, rtl] triples, sorted: rtl 0 is L, 1 is R or AL.
|
|
3
|
+
export const UNICODE_VERSION = "17.0.0";
|
|
4
|
+
export const STRONG: readonly number[] = [
|
|
5
|
+
0x41, 0x5a, 0, 0x61, 0x7a, 0, 0xaa, 0xaa, 0, 0xb5, 0xb5, 0, 0xba, 0xba, 0, 0xc0, 0xd6, 0,
|
|
6
|
+
0xd8, 0xf6, 0, 0xf8, 0x2b8, 0, 0x2bb, 0x2c1, 0, 0x2d0, 0x2d1, 0, 0x2e0, 0x2e4, 0, 0x2ee, 0x2ee, 0,
|
|
7
|
+
0x370, 0x373, 0, 0x376, 0x37d, 0, 0x37f, 0x383, 0, 0x386, 0x386, 0, 0x388, 0x3f5, 0, 0x3f7, 0x482, 0,
|
|
8
|
+
0x48a, 0x589, 0, 0x58b, 0x58c, 0, 0x590, 0x590, 1, 0x5be, 0x5be, 1, 0x5c0, 0x5c0, 1, 0x5c3, 0x5c3, 1,
|
|
9
|
+
0x5c6, 0x5c6, 1, 0x5c8, 0x5ff, 1, 0x608, 0x608, 1, 0x60b, 0x60b, 1, 0x60d, 0x60d, 1, 0x61b, 0x64a, 1,
|
|
10
|
+
0x66d, 0x66f, 1, 0x671, 0x6d5, 1, 0x6e5, 0x6e6, 1, 0x6ee, 0x6ef, 1, 0x6fa, 0x710, 1, 0x712, 0x72f, 1,
|
|
11
|
+
0x74b, 0x7a5, 1, 0x7b1, 0x7ea, 1, 0x7f4, 0x7f5, 1, 0x7fa, 0x7fc, 1, 0x7fe, 0x815, 1, 0x81a, 0x81a, 1,
|
|
12
|
+
0x824, 0x824, 1, 0x828, 0x828, 1, 0x82e, 0x858, 1, 0x85c, 0x88f, 1, 0x892, 0x896, 1, 0x8a0, 0x8c9, 1,
|
|
13
|
+
0x903, 0x939, 0, 0x93b, 0x93b, 0, 0x93d, 0x940, 0, 0x949, 0x94c, 0, 0x94e, 0x950, 0, 0x958, 0x961, 0,
|
|
14
|
+
0x964, 0x980, 0, 0x982, 0x9bb, 0, 0x9bd, 0x9c0, 0, 0x9c5, 0x9cc, 0, 0x9ce, 0x9e1, 0, 0x9e4, 0x9f1, 0,
|
|
15
|
+
0x9f4, 0x9fa, 0, 0x9fc, 0x9fd, 0, 0x9ff, 0xa00, 0, 0xa03, 0xa3b, 0, 0xa3d, 0xa40, 0, 0xa43, 0xa46, 0,
|
|
16
|
+
0xa49, 0xa4a, 0, 0xa4e, 0xa50, 0, 0xa52, 0xa6f, 0, 0xa72, 0xa74, 0, 0xa76, 0xa80, 0, 0xa83, 0xabb, 0,
|
|
17
|
+
0xabd, 0xac0, 0, 0xac6, 0xac6, 0, 0xac9, 0xacc, 0, 0xace, 0xae1, 0, 0xae4, 0xaf0, 0, 0xaf2, 0xaf9, 0,
|
|
18
|
+
0xb00, 0xb00, 0, 0xb02, 0xb3b, 0, 0xb3d, 0xb3e, 0, 0xb40, 0xb40, 0, 0xb45, 0xb4c, 0, 0xb4e, 0xb54, 0,
|
|
19
|
+
0xb57, 0xb61, 0, 0xb64, 0xb81, 0, 0xb83, 0xbbf, 0, 0xbc1, 0xbcc, 0, 0xbce, 0xbf2, 0, 0xbfb, 0xbff, 0,
|
|
20
|
+
0xc01, 0xc03, 0, 0xc05, 0xc3b, 0, 0xc3d, 0xc3d, 0, 0xc41, 0xc45, 0, 0xc49, 0xc49, 0, 0xc4e, 0xc54, 0,
|
|
21
|
+
0xc57, 0xc61, 0, 0xc64, 0xc77, 0, 0xc7f, 0xc80, 0, 0xc82, 0xcbb, 0, 0xcbd, 0xccb, 0, 0xcce, 0xce1, 0,
|
|
22
|
+
0xce4, 0xcff, 0, 0xd02, 0xd3a, 0, 0xd3d, 0xd40, 0, 0xd45, 0xd4c, 0, 0xd4e, 0xd61, 0, 0xd64, 0xd80, 0,
|
|
23
|
+
0xd82, 0xdc9, 0, 0xdcb, 0xdd1, 0, 0xdd5, 0xdd5, 0, 0xdd7, 0xe30, 0, 0xe32, 0xe33, 0, 0xe3b, 0xe3e, 0,
|
|
24
|
+
0xe40, 0xe46, 0, 0xe4f, 0xeb0, 0, 0xeb2, 0xeb3, 0, 0xebd, 0xec7, 0, 0xecf, 0xf17, 0, 0xf1a, 0xf34, 0,
|
|
25
|
+
0xf36, 0xf36, 0, 0xf38, 0xf38, 0, 0xf3e, 0xf70, 0, 0xf7f, 0xf7f, 0, 0xf85, 0xf85, 0, 0xf88, 0xf8c, 0,
|
|
26
|
+
0xf98, 0xf98, 0, 0xfbd, 0xfc5, 0, 0xfc7, 0x102c, 0, 0x1031, 0x1031, 0, 0x1038, 0x1038, 0, 0x103b, 0x103c, 0,
|
|
27
|
+
0x103f, 0x1057, 0, 0x105a, 0x105d, 0, 0x1061, 0x1070, 0, 0x1075, 0x1081, 0, 0x1083, 0x1084, 0, 0x1087, 0x108c, 0,
|
|
28
|
+
0x108e, 0x109c, 0, 0x109e, 0x135c, 0, 0x1360, 0x138f, 0, 0x139a, 0x13ff, 0, 0x1401, 0x167f, 0, 0x1681, 0x169a, 0,
|
|
29
|
+
0x169d, 0x1711, 0, 0x1715, 0x1731, 0, 0x1734, 0x1751, 0, 0x1754, 0x1771, 0, 0x1774, 0x17b3, 0, 0x17b6, 0x17b6, 0,
|
|
30
|
+
0x17be, 0x17c5, 0, 0x17c7, 0x17c8, 0, 0x17d4, 0x17da, 0, 0x17dc, 0x17dc, 0, 0x17de, 0x17ef, 0, 0x17fa, 0x17ff, 0,
|
|
31
|
+
0x1810, 0x1884, 0, 0x1887, 0x18a8, 0, 0x18aa, 0x191f, 0, 0x1923, 0x1926, 0, 0x1929, 0x1931, 0, 0x1933, 0x1938, 0,
|
|
32
|
+
0x193c, 0x193f, 0, 0x1941, 0x1943, 0, 0x1946, 0x19dd, 0, 0x1a00, 0x1a16, 0, 0x1a19, 0x1a1a, 0, 0x1a1c, 0x1a55, 0,
|
|
33
|
+
0x1a57, 0x1a57, 0, 0x1a5f, 0x1a5f, 0, 0x1a61, 0x1a61, 0, 0x1a63, 0x1a64, 0, 0x1a6d, 0x1a72, 0, 0x1a7d, 0x1a7e, 0,
|
|
34
|
+
0x1a80, 0x1aaf, 0, 0x1ade, 0x1adf, 0, 0x1aec, 0x1aff, 0, 0x1b04, 0x1b33, 0, 0x1b35, 0x1b35, 0, 0x1b3b, 0x1b3b, 0,
|
|
35
|
+
0x1b3d, 0x1b41, 0, 0x1b43, 0x1b6a, 0, 0x1b74, 0x1b7f, 0, 0x1b82, 0x1ba1, 0, 0x1ba6, 0x1ba7, 0, 0x1baa, 0x1baa, 0,
|
|
36
|
+
0x1bae, 0x1be5, 0, 0x1be7, 0x1be7, 0, 0x1bea, 0x1bec, 0, 0x1bee, 0x1bee, 0, 0x1bf2, 0x1c2b, 0, 0x1c34, 0x1c35, 0,
|
|
37
|
+
0x1c38, 0x1ccf, 0, 0x1cd3, 0x1cd3, 0, 0x1ce1, 0x1ce1, 0, 0x1ce9, 0x1cec, 0, 0x1cee, 0x1cf3, 0, 0x1cf5, 0x1cf7, 0,
|
|
38
|
+
0x1cfa, 0x1dbf, 0, 0x1e00, 0x1fbc, 0, 0x1fbe, 0x1fbe, 0, 0x1fc2, 0x1fcc, 0, 0x1fd0, 0x1fdc, 0, 0x1fe0, 0x1fec, 0,
|
|
39
|
+
0x1ff0, 0x1ffc, 0, 0x1fff, 0x1fff, 0, 0x200e, 0x200e, 0, 0x200f, 0x200f, 1, 0x2071, 0x2073, 0, 0x207f, 0x207f, 0,
|
|
40
|
+
0x208f, 0x209f, 0, 0x20f1, 0x20ff, 0, 0x2102, 0x2102, 0, 0x2107, 0x2107, 0, 0x210a, 0x2113, 0, 0x2115, 0x2115, 0,
|
|
41
|
+
0x2119, 0x211d, 0, 0x2124, 0x2124, 0, 0x2126, 0x2126, 0, 0x2128, 0x2128, 0, 0x212a, 0x212d, 0, 0x212f, 0x2139, 0,
|
|
42
|
+
0x213c, 0x213f, 0, 0x2145, 0x2149, 0, 0x214e, 0x214f, 0, 0x2160, 0x2188, 0, 0x218c, 0x218f, 0, 0x2336, 0x237a, 0,
|
|
43
|
+
0x2395, 0x2395, 0, 0x242a, 0x243f, 0, 0x244b, 0x245f, 0, 0x249c, 0x24e9, 0, 0x26ac, 0x26ac, 0, 0x2800, 0x28ff, 0,
|
|
44
|
+
0x2b74, 0x2b75, 0, 0x2c00, 0x2ce4, 0, 0x2ceb, 0x2cee, 0, 0x2cf2, 0x2cf8, 0, 0x2d00, 0x2d7e, 0, 0x2d80, 0x2ddf, 0,
|
|
45
|
+
0x2e5e, 0x2e7f, 0, 0x2e9a, 0x2e9a, 0, 0x2ef4, 0x2eff, 0, 0x2fd6, 0x2fef, 0, 0x3005, 0x3007, 0, 0x3021, 0x3029, 0,
|
|
46
|
+
0x302e, 0x302f, 0, 0x3031, 0x3035, 0, 0x3038, 0x303c, 0, 0x3040, 0x3098, 0, 0x309d, 0x309f, 0, 0x30a1, 0x30fa, 0,
|
|
47
|
+
0x30fc, 0x31bf, 0, 0x31e6, 0x31ee, 0, 0x31f0, 0x321c, 0, 0x321f, 0x324f, 0, 0x3260, 0x327b, 0, 0x327f, 0x32b0, 0,
|
|
48
|
+
0x32c0, 0x32cb, 0, 0x32d0, 0x3376, 0, 0x337b, 0x33dd, 0, 0x33e0, 0x33fe, 0, 0x3400, 0x4dbf, 0, 0x4e00, 0xa48f, 0,
|
|
49
|
+
0xa4c7, 0xa60c, 0, 0xa610, 0xa66e, 0, 0xa680, 0xa69d, 0, 0xa6a0, 0xa6ef, 0, 0xa6f2, 0xa6ff, 0, 0xa722, 0xa787, 0,
|
|
50
|
+
0xa789, 0xa801, 0, 0xa803, 0xa805, 0, 0xa807, 0xa80a, 0, 0xa80c, 0xa824, 0, 0xa827, 0xa827, 0, 0xa82d, 0xa837, 0,
|
|
51
|
+
0xa83a, 0xa873, 0, 0xa878, 0xa8c3, 0, 0xa8c6, 0xa8df, 0, 0xa8f2, 0xa8fe, 0, 0xa900, 0xa925, 0, 0xa92e, 0xa946, 0,
|
|
52
|
+
0xa952, 0xa97f, 0, 0xa983, 0xa9b2, 0, 0xa9b4, 0xa9b5, 0, 0xa9ba, 0xa9bb, 0, 0xa9be, 0xa9e4, 0, 0xa9e6, 0xaa28, 0,
|
|
53
|
+
0xaa2f, 0xaa30, 0, 0xaa33, 0xaa34, 0, 0xaa37, 0xaa42, 0, 0xaa44, 0xaa4b, 0, 0xaa4d, 0xaa7b, 0, 0xaa7d, 0xaaaf, 0,
|
|
54
|
+
0xaab1, 0xaab1, 0, 0xaab5, 0xaab6, 0, 0xaab9, 0xaabd, 0, 0xaac0, 0xaac0, 0, 0xaac2, 0xaaeb, 0, 0xaaee, 0xaaf5, 0,
|
|
55
|
+
0xaaf7, 0xab69, 0, 0xab6c, 0xabe4, 0, 0xabe6, 0xabe7, 0, 0xabe9, 0xabec, 0, 0xabee, 0xfb1c, 0, 0xfb1d, 0xfb1d, 1,
|
|
56
|
+
0xfb1f, 0xfb28, 1, 0xfb2a, 0xfbc2, 1, 0xfbd3, 0xfd3d, 1, 0xfd50, 0xfd8f, 1, 0xfd92, 0xfdc7, 1, 0xfdf0, 0xfdfc, 1,
|
|
57
|
+
0xfe1a, 0xfe1f, 0, 0xfe53, 0xfe53, 0, 0xfe67, 0xfe67, 0, 0xfe6c, 0xfe6f, 0, 0xfe70, 0xfefe, 1, 0xff00, 0xff00, 0,
|
|
58
|
+
0xff21, 0xff3a, 0, 0xff41, 0xff5a, 0, 0xff66, 0xffdf, 0, 0xffe7, 0xffe7, 0, 0xffef, 0xffef, 0, 0x10000, 0x10100, 0,
|
|
59
|
+
0x10102, 0x1013f, 0, 0x1018d, 0x1018f, 0, 0x1019d, 0x1019f, 0, 0x101a1, 0x101fc, 0, 0x101fe, 0x102df, 0, 0x102fc, 0x10375, 0,
|
|
60
|
+
0x1037b, 0x107ff, 0, 0x10800, 0x1091e, 1, 0x10920, 0x10a00, 1, 0x10a04, 0x10a04, 1, 0x10a07, 0x10a0b, 1, 0x10a10, 0x10a37, 1,
|
|
61
|
+
0x10a3b, 0x10a3e, 1, 0x10a40, 0x10ae4, 1, 0x10ae7, 0x10b38, 1, 0x10b40, 0x10d23, 1, 0x10d28, 0x10d2f, 1, 0x10d3a, 0x10d3f, 1,
|
|
62
|
+
0x10d4a, 0x10d68, 1, 0x10d6f, 0x10e5f, 1, 0x10e7f, 0x10eaa, 1, 0x10ead, 0x10ecf, 1, 0x10ed9, 0x10ef9, 1, 0x10f00, 0x10f45, 1,
|
|
63
|
+
0x10f51, 0x10f81, 1, 0x10f86, 0x10fff, 1, 0x11000, 0x11000, 0, 0x11002, 0x11037, 0, 0x11047, 0x11051, 0, 0x11066, 0x1106f, 0,
|
|
64
|
+
0x11071, 0x11072, 0, 0x11075, 0x1107e, 0, 0x11082, 0x110b2, 0, 0x110b7, 0x110b8, 0, 0x110bb, 0x110c1, 0, 0x110c3, 0x110ff, 0,
|
|
65
|
+
0x11103, 0x11126, 0, 0x1112c, 0x1112c, 0, 0x11135, 0x11172, 0, 0x11174, 0x1117f, 0, 0x11182, 0x111b5, 0, 0x111bf, 0x111c8, 0,
|
|
66
|
+
0x111cd, 0x111ce, 0, 0x111d0, 0x1122e, 0, 0x11232, 0x11233, 0, 0x11235, 0x11235, 0, 0x11238, 0x1123d, 0, 0x1123f, 0x11240, 0,
|
|
67
|
+
0x11242, 0x112de, 0, 0x112e0, 0x112e2, 0, 0x112eb, 0x112ff, 0, 0x11302, 0x1133a, 0, 0x1133d, 0x1133f, 0, 0x11341, 0x11365, 0,
|
|
68
|
+
0x1136d, 0x1136f, 0, 0x11375, 0x113ba, 0, 0x113c1, 0x113cd, 0, 0x113cf, 0x113cf, 0, 0x113d1, 0x113d1, 0, 0x113d3, 0x113e0, 0,
|
|
69
|
+
0x113e3, 0x11437, 0, 0x11440, 0x11441, 0, 0x11445, 0x11445, 0, 0x11447, 0x1145d, 0, 0x1145f, 0x114b2, 0, 0x114b9, 0x114b9, 0,
|
|
70
|
+
0x114bb, 0x114be, 0, 0x114c1, 0x114c1, 0, 0x114c4, 0x115b1, 0, 0x115b6, 0x115bb, 0, 0x115be, 0x115be, 0, 0x115c1, 0x115db, 0,
|
|
71
|
+
0x115de, 0x11632, 0, 0x1163b, 0x1163c, 0, 0x1163e, 0x1163e, 0, 0x11641, 0x1165f, 0, 0x1166d, 0x116aa, 0, 0x116ac, 0x116ac, 0,
|
|
72
|
+
0x116ae, 0x116af, 0, 0x116b6, 0x116b6, 0, 0x116b8, 0x1171c, 0, 0x1171e, 0x1171e, 0, 0x11720, 0x11721, 0, 0x11726, 0x11726, 0,
|
|
73
|
+
0x1172c, 0x1182e, 0, 0x11838, 0x11838, 0, 0x1183b, 0x1193a, 0, 0x1193d, 0x1193d, 0, 0x1193f, 0x11942, 0, 0x11944, 0x119d3, 0,
|
|
74
|
+
0x119d8, 0x119d9, 0, 0x119dc, 0x119df, 0, 0x119e1, 0x11a00, 0, 0x11a07, 0x11a08, 0, 0x11a0b, 0x11a32, 0, 0x11a39, 0x11a3a, 0,
|
|
75
|
+
0x11a3f, 0x11a46, 0, 0x11a48, 0x11a50, 0, 0x11a57, 0x11a58, 0, 0x11a5c, 0x11a89, 0, 0x11a97, 0x11a97, 0, 0x11a9a, 0x11b5f, 0,
|
|
76
|
+
0x11b61, 0x11b61, 0, 0x11b65, 0x11b65, 0, 0x11b67, 0x11c2f, 0, 0x11c37, 0x11c37, 0, 0x11c3e, 0x11c91, 0, 0x11ca8, 0x11ca9, 0,
|
|
77
|
+
0x11cb1, 0x11cb1, 0, 0x11cb4, 0x11cb4, 0, 0x11cb7, 0x11d30, 0, 0x11d37, 0x11d39, 0, 0x11d3b, 0x11d3b, 0, 0x11d3e, 0x11d3e, 0,
|
|
78
|
+
0x11d46, 0x11d46, 0, 0x11d48, 0x11d8f, 0, 0x11d92, 0x11d94, 0, 0x11d96, 0x11d96, 0, 0x11d98, 0x11ef2, 0, 0x11ef5, 0x11eff, 0,
|
|
79
|
+
0x11f02, 0x11f35, 0, 0x11f3b, 0x11f3f, 0, 0x11f41, 0x11f41, 0, 0x11f43, 0x11f59, 0, 0x11f5b, 0x11fd4, 0, 0x11ff2, 0x1343f, 0,
|
|
80
|
+
0x13441, 0x13446, 0, 0x13456, 0x1611d, 0, 0x1612a, 0x1612c, 0, 0x16130, 0x16aef, 0, 0x16af5, 0x16b2f, 0, 0x16b37, 0x16f4e, 0,
|
|
81
|
+
0x16f50, 0x16f8e, 0, 0x16f93, 0x16fe1, 0, 0x16fe3, 0x16fe3, 0, 0x16fe5, 0x1bc9c, 0, 0x1bc9f, 0x1bc9f, 0, 0x1bca4, 0x1cbff, 0,
|
|
82
|
+
0x1ccd6, 0x1ccef, 0, 0x1ccfd, 0x1ccff, 0, 0x1ceb4, 0x1ceb9, 0, 0x1ced1, 0x1cedf, 0, 0x1cef1, 0x1ceff, 0, 0x1cf2e, 0x1cf2f, 0,
|
|
83
|
+
0x1cf47, 0x1d166, 0, 0x1d16a, 0x1d172, 0, 0x1d183, 0x1d184, 0, 0x1d18c, 0x1d1a9, 0, 0x1d1ae, 0x1d1e8, 0, 0x1d1eb, 0x1d1ff, 0,
|
|
84
|
+
0x1d246, 0x1d2ff, 0, 0x1d357, 0x1d6c0, 0, 0x1d6c2, 0x1d6da, 0, 0x1d6dc, 0x1d6fa, 0, 0x1d6fc, 0x1d714, 0, 0x1d716, 0x1d734, 0,
|
|
85
|
+
0x1d736, 0x1d74e, 0, 0x1d750, 0x1d76e, 0, 0x1d770, 0x1d788, 0, 0x1d78a, 0x1d7a8, 0, 0x1d7aa, 0x1d7c2, 0, 0x1d7c4, 0x1d7cd, 0,
|
|
86
|
+
0x1d800, 0x1d9ff, 0, 0x1da37, 0x1da3a, 0, 0x1da6d, 0x1da74, 0, 0x1da76, 0x1da83, 0, 0x1da85, 0x1da9a, 0, 0x1daa0, 0x1daa0, 0,
|
|
87
|
+
0x1dab0, 0x1dfff, 0, 0x1e007, 0x1e007, 0, 0x1e019, 0x1e01a, 0, 0x1e022, 0x1e022, 0, 0x1e025, 0x1e025, 0, 0x1e02b, 0x1e08e, 0,
|
|
88
|
+
0x1e090, 0x1e12f, 0, 0x1e137, 0x1e2ad, 0, 0x1e2af, 0x1e2eb, 0, 0x1e2f0, 0x1e2fe, 0, 0x1e300, 0x1e4eb, 0, 0x1e4f0, 0x1e5ed, 0,
|
|
89
|
+
0x1e5f0, 0x1e6e2, 0, 0x1e6e4, 0x1e6e5, 0, 0x1e6e7, 0x1e6ed, 0, 0x1e6f0, 0x1e6f4, 0, 0x1e6f6, 0x1e7ff, 0, 0x1e800, 0x1e8cf, 1,
|
|
90
|
+
0x1e8d7, 0x1e943, 1, 0x1e94b, 0x1eeef, 1, 0x1eef2, 0x1efff, 1, 0x1f02c, 0x1f02f, 0, 0x1f094, 0x1f09f, 0, 0x1f0af, 0x1f0b0, 0,
|
|
91
|
+
0x1f0c0, 0x1f0c0, 0, 0x1f0d0, 0x1f0d0, 0, 0x1f0f6, 0x1f0ff, 0, 0x1f110, 0x1f12e, 0, 0x1f130, 0x1f169, 0, 0x1f170, 0x1f1ac, 0,
|
|
92
|
+
0x1f1ae, 0x1f25f, 0, 0x1f266, 0x1f2ff, 0, 0x1f6d9, 0x1f6db, 0, 0x1f6ed, 0x1f6ef, 0, 0x1f6fd, 0x1f6ff, 0, 0x1f7da, 0x1f7df, 0,
|
|
93
|
+
0x1f7ec, 0x1f7ef, 0, 0x1f7f1, 0x1f7ff, 0, 0x1f80c, 0x1f80f, 0, 0x1f848, 0x1f84f, 0, 0x1f85a, 0x1f85f, 0, 0x1f888, 0x1f88f, 0,
|
|
94
|
+
0x1f8ae, 0x1f8af, 0, 0x1f8bc, 0x1f8bf, 0, 0x1f8c2, 0x1f8cf, 0, 0x1f8d9, 0x1f8ff, 0, 0x1fa58, 0x1fa5f, 0, 0x1fa6e, 0x1fa6f, 0,
|
|
95
|
+
0x1fa7d, 0x1fa7f, 0, 0x1fa8b, 0x1fa8d, 0, 0x1fac7, 0x1fac7, 0, 0x1fac9, 0x1facc, 0, 0x1fadd, 0x1fade, 0, 0x1faeb, 0x1faee, 0,
|
|
96
|
+
0x1faf9, 0x1faff, 0, 0x1fb93, 0x1fb93, 0, 0x1fbfb, 0x1fffd, 0, 0x20000, 0x2fffd, 0, 0x30000, 0x3fffd, 0, 0x40000, 0x4fffd, 0,
|
|
97
|
+
0x50000, 0x5fffd, 0, 0x60000, 0x6fffd, 0, 0x70000, 0x7fffd, 0, 0x80000, 0x8fffd, 0, 0x90000, 0x9fffd, 0, 0xa0000, 0xafffd, 0,
|
|
98
|
+
0xb0000, 0xbfffd, 0, 0xc0000, 0xcfffd, 0, 0xd0000, 0xdfffd, 0, 0xe1000, 0xefffd, 0, 0xf0000, 0xffffd, 0, 0x100000, 0x10fffd, 0,
|
|
99
|
+
];
|
package/src/markdown-typst.ts
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
|
|
14
14
|
import { Lexer, type Token, type Tokens } from "marked";
|
|
15
15
|
|
|
16
|
+
import { STRONG } from "./bidi-classes";
|
|
16
17
|
import type { PdfDocument } from "./renderer";
|
|
17
18
|
|
|
18
19
|
export type TypstSourceOptions = {
|
|
@@ -22,17 +23,27 @@ export type TypstSourceOptions = {
|
|
|
22
23
|
};
|
|
23
24
|
|
|
24
25
|
export function markdownToTypst(markdown: string): string {
|
|
25
|
-
|
|
26
|
-
return blocks(tokens).trim() + "\n";
|
|
26
|
+
return body(lex(markdown));
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
const lex = (markdown: string) => new Lexer({ gfm: true }).lex(markdown);
|
|
30
|
+
const body = (tokens: Token[], base: Dir = "ltr") => blocks(tokens, "\n\n", base).trim() + "\n";
|
|
31
|
+
|
|
29
32
|
// The whole document: page and text setup, then the body.
|
|
30
33
|
export function typstSource(doc: PdfDocument, opts: TypstSourceOptions = {}): string {
|
|
31
34
|
const lines: string[] = [];
|
|
32
35
|
const meta = [doc.title ? `title: ${str(doc.title)}` : "", doc.author ? `author: ${str(doc.author)}` : ""].filter(Boolean);
|
|
33
36
|
if (meta.length) lines.push(`#set document(${meta.join(", ")})`);
|
|
34
37
|
lines.push(`#set page(paper: ${str(doc.paper === "us-letter" ? "us-letter" : "a4")}, margin: (x: 2.2cm, y: 2.4cm), numbering: "1")`);
|
|
35
|
-
const
|
|
38
|
+
const tokens = lex(doc.markdown);
|
|
39
|
+
const lang = langArgs(doc.lang);
|
|
40
|
+
// The document's direction: Typst takes it from `lang` (its own table, mirrored in
|
|
41
|
+
// RTL_LANGS); without a lang, from the first strong character that renders, as Unicode's bidi
|
|
42
|
+
// rule P2 does for a paragraph (#392). Each block then takes its own the same way and is
|
|
43
|
+
// turned only where it differs, like HTML's dir="auto": an Arabic paragraph in an English
|
|
44
|
+
// report reads right to left, and the other way round.
|
|
45
|
+
const base: Dir = lang.length ? (RTL_LANGS.has(doc.lang!.toLowerCase().split(/[-_]/)[0]!) ? "rtl" : "ltr") : (firstStrong(tokens) ?? "ltr");
|
|
46
|
+
const text = ["size: 10.5pt", ...lang, ...(lang.length === 0 && base === "rtl" ? ["dir: rtl"] : [])];
|
|
36
47
|
if (opts.fonts?.length) text.push(`font: (${opts.fonts.map(str).join(", ")},)`);
|
|
37
48
|
lines.push(`#set text(${text.join(", ")})`);
|
|
38
49
|
lines.push(
|
|
@@ -43,7 +54,7 @@ export function typstSource(doc: PdfDocument, opts: TypstSourceOptions = {}): st
|
|
|
43
54
|
"#show raw.where(block: true): block.with(fill: luma(245), inset: 8pt, radius: 3pt, width: 100%)",
|
|
44
55
|
"",
|
|
45
56
|
);
|
|
46
|
-
return lines.join("\n") +
|
|
57
|
+
return lines.join("\n") + body(tokens, base);
|
|
47
58
|
}
|
|
48
59
|
|
|
49
60
|
// BCP 47 → Typst's `lang` (ISO 639) and `region` (ISO 3166-1 alpha-2). A tag Typst can't take
|
|
@@ -56,16 +67,127 @@ function langArgs(tag: string | undefined): string[] {
|
|
|
56
67
|
return [`lang: ${str(lang)}`, ...(region ? [`region: ${str(region)}`] : [])];
|
|
57
68
|
}
|
|
58
69
|
|
|
70
|
+
type Dir = "ltr" | "rtl";
|
|
71
|
+
|
|
72
|
+
// The languages Typst sets right to left (typst-library's Lang::dir), so a block is compared
|
|
73
|
+
// with the direction Typst actually gives the document.
|
|
74
|
+
const RTL_LANGS = new Set(["ar", "dv", "fa", "he", "ks", "pa", "ps", "sd", "ug", "ur", "yi"]);
|
|
75
|
+
|
|
76
|
+
// A code point's strong direction by its Unicode Bidi_Class (bidi-classes.ts): L is ltr, R and
|
|
77
|
+
// AL are rtl, anything else (digits, punctuation, marks, spaces) is not strong.
|
|
78
|
+
function strong(cp: number): Dir | undefined {
|
|
79
|
+
let lo = 0;
|
|
80
|
+
let hi = STRONG.length / 3 - 1;
|
|
81
|
+
while (lo <= hi) {
|
|
82
|
+
const mid = (lo + hi) >> 1;
|
|
83
|
+
const start = STRONG[mid * 3]!;
|
|
84
|
+
if (cp < start) hi = mid - 1;
|
|
85
|
+
else if (cp > STRONG[mid * 3 + 1]!) lo = mid + 1;
|
|
86
|
+
else return STRONG[mid * 3 + 2] ? "rtl" : "ltr";
|
|
87
|
+
}
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// The direction of the first strong character the tokens put on the page (rule P2); undefined
|
|
92
|
+
// when there is none, and the block keeps the direction around it. As P2 says, text between an
|
|
93
|
+
// isolate initiator (LRI, RLI, FSI) and its matching PDI is skipped; an isolate left open ends
|
|
94
|
+
// with its paragraph, which rendered() marks with U+2029 (Bidi_Class B, the paragraph separator).
|
|
95
|
+
function firstStrong(tokens: Token[]): Dir | undefined {
|
|
96
|
+
let isolated = 0;
|
|
97
|
+
for (const run of rendered(tokens)) {
|
|
98
|
+
for (const ch of run) {
|
|
99
|
+
const cp = ch.codePointAt(0)!;
|
|
100
|
+
if (cp === PARAGRAPH_END) isolated = 0;
|
|
101
|
+
else if (cp >= 0x2066 && cp <= 0x2068) isolated++;
|
|
102
|
+
else if (cp === 0x2069) isolated = Math.max(0, isolated - 1);
|
|
103
|
+
else if (!isolated) {
|
|
104
|
+
const dir = strong(cp);
|
|
105
|
+
if (dir) return dir;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// The text runs block() and inline() put on the page, in order, as rendered: HTML through
|
|
113
|
+
// htmlText, entities decoded, a link as its text, an image as its alt text, a code block
|
|
114
|
+
// verbatim. Each paragraph — a block, a list item, a table cell — ends with PARAGRAPH_END.
|
|
115
|
+
const PARAGRAPH_END = 0x2029;
|
|
116
|
+
const END = String.fromCodePoint(PARAGRAPH_END);
|
|
117
|
+
// Marked uses "html" for an inline tag too; only a block of HTML ends a paragraph.
|
|
118
|
+
const BLOCK_TYPES = new Set(["paragraph", "heading", "code", "blockquote", "list", "table"]);
|
|
119
|
+
const endsParagraph = (t: Token) => BLOCK_TYPES.has(t.type) || (t.type === "html" && (t as Tokens.HTML).block);
|
|
120
|
+
function* rendered(tokens: Token[]): Generator<string> {
|
|
121
|
+
for (const t of tokens) {
|
|
122
|
+
yield* renderedToken(t);
|
|
123
|
+
if (endsParagraph(t)) yield END;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function* renderedToken(t: Token): Generator<string> {
|
|
128
|
+
const x = t as { type: string; text?: string; tokens?: Token[] };
|
|
129
|
+
switch (t.type) {
|
|
130
|
+
case "code":
|
|
131
|
+
yield (t as Tokens.Code).text;
|
|
132
|
+
break;
|
|
133
|
+
case "html":
|
|
134
|
+
yield decodeEntities(htmlText(x.text ?? ""));
|
|
135
|
+
break;
|
|
136
|
+
case "image":
|
|
137
|
+
yield decodeEntities((t as Tokens.Image).text || "[image]");
|
|
138
|
+
break;
|
|
139
|
+
case "link":
|
|
140
|
+
yield* rendered((t as Tokens.Link).tokens);
|
|
141
|
+
break;
|
|
142
|
+
case "list":
|
|
143
|
+
for (const item of (t as Tokens.List).items) {
|
|
144
|
+
yield* rendered(item.tokens);
|
|
145
|
+
yield END;
|
|
146
|
+
}
|
|
147
|
+
break;
|
|
148
|
+
case "table": {
|
|
149
|
+
const tb = t as Tokens.Table;
|
|
150
|
+
for (const c of [...tb.header, ...tb.rows.flat()]) {
|
|
151
|
+
yield* rendered(c.tokens);
|
|
152
|
+
yield END;
|
|
153
|
+
}
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
case "space":
|
|
157
|
+
case "def":
|
|
158
|
+
case "hr":
|
|
159
|
+
case "checkbox":
|
|
160
|
+
break;
|
|
161
|
+
default:
|
|
162
|
+
if (x.tokens) yield* rendered(x.tokens);
|
|
163
|
+
else if (x.text) yield decodeEntities(x.text);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Content set in `dir`, for a block whose direction differs from the one around it.
|
|
168
|
+
function turned(content: string, dir: Dir): string {
|
|
169
|
+
return content ? `#[#set text(dir: ${dir})\n${content}]` : content;
|
|
170
|
+
}
|
|
171
|
+
|
|
59
172
|
// ── blocks ──────────────────────────────────────────────────────────────────
|
|
60
173
|
|
|
61
174
|
// Blocks are separated by a paragraph break — except inside a tight list item, where a
|
|
62
175
|
// break would loosen the list; a single newline is a space in Typst markup, and the
|
|
63
176
|
// block-level calls that follow (a nested list) start their own line regardless.
|
|
64
|
-
|
|
65
|
-
|
|
177
|
+
// Each block in its own direction (its first strong character), turned where that differs
|
|
178
|
+
// from `dir`, the direction around it — for the blocks `turns` accepts; the rest keep `dir`.
|
|
179
|
+
function blocks(tokens: Token[], sep = "\n\n", dir: Dir = "ltr", turns: (t: Token) => boolean = () => true): string {
|
|
180
|
+
return tokens
|
|
181
|
+
.map((t) => {
|
|
182
|
+
const own = turns(t) ? (firstStrong([t]) ?? dir) : dir;
|
|
183
|
+
const out = block(t, own);
|
|
184
|
+
return own === dir ? out : turned(out, own);
|
|
185
|
+
})
|
|
186
|
+
.filter((s) => s !== "")
|
|
187
|
+
.join(sep);
|
|
66
188
|
}
|
|
67
189
|
|
|
68
|
-
function block(t: Token): string {
|
|
190
|
+
function block(t: Token, dir: Dir): string {
|
|
69
191
|
switch (t.type) {
|
|
70
192
|
case "heading": {
|
|
71
193
|
const h = t as Tokens.Heading;
|
|
@@ -84,11 +206,11 @@ function block(t: Token): string {
|
|
|
84
206
|
return `#raw(block: true, ${lang ? `lang: ${str(lang)}, ` : ""}${str(c.text)})`;
|
|
85
207
|
}
|
|
86
208
|
case "blockquote":
|
|
87
|
-
return `#quote(block: true)[${blocks((t as Tokens.Blockquote).tokens)}]`;
|
|
209
|
+
return `#quote(block: true)[${blocks((t as Tokens.Blockquote).tokens, "\n\n", dir)}]`;
|
|
88
210
|
case "list":
|
|
89
|
-
return list(t as Tokens.List);
|
|
211
|
+
return list(t as Tokens.List, dir);
|
|
90
212
|
case "table":
|
|
91
|
-
return table(t as Tokens.Table);
|
|
213
|
+
return table(t as Tokens.Table, dir);
|
|
92
214
|
case "hr":
|
|
93
215
|
return "#line(length: 100%, stroke: 0.5pt + luma(170))";
|
|
94
216
|
case "html":
|
|
@@ -104,11 +226,14 @@ function block(t: Token): string {
|
|
|
104
226
|
}
|
|
105
227
|
}
|
|
106
228
|
|
|
107
|
-
function list(l: Tokens.List): string {
|
|
229
|
+
function list(l: Tokens.List, dir: Dir): string {
|
|
108
230
|
const items = l.items.map((item) => {
|
|
109
231
|
const body = item.tokens.filter((x) => x.type !== "checkbox");
|
|
110
232
|
const box = item.task ? `#${str(item.checked ? "☑ " : "☐ ")};` : "";
|
|
111
|
-
|
|
233
|
+
// An item's own text keeps the list's direction (blocks() turned the list as a whole):
|
|
234
|
+
// turning it alone would part it from its marker, and it still reads in its own order
|
|
235
|
+
// within the line. A nested list chooses its own direction, as a whole, like any list.
|
|
236
|
+
return `[${box}${blocks(body, l.loose ? "\n\n" : "\n", dir, (x) => x.type === "list")}]`;
|
|
112
237
|
});
|
|
113
238
|
const args = [`tight: ${!l.loose}`];
|
|
114
239
|
if (l.ordered) {
|
|
@@ -118,10 +243,15 @@ function list(l: Tokens.List): string {
|
|
|
118
243
|
return `#${l.ordered ? "enum" : "list"}(${args.join(", ")}, ${items.join(", ")})`;
|
|
119
244
|
}
|
|
120
245
|
|
|
121
|
-
function table(t: Tokens.Table): string {
|
|
246
|
+
function table(t: Tokens.Table, dir: Dir): string {
|
|
122
247
|
const align = t.align.map((a) => a ?? "auto");
|
|
123
|
-
|
|
124
|
-
const
|
|
248
|
+
// A cell in its own direction, like a block.
|
|
249
|
+
const cell = (tokens: Token[], content: string) => {
|
|
250
|
+
const own = firstStrong(tokens) ?? dir;
|
|
251
|
+
return `[${own === dir ? content : turned(content, own)}]`;
|
|
252
|
+
};
|
|
253
|
+
const header = t.header.map((c) => cell(c.tokens, `#strong[${inlines(c.tokens)}]`));
|
|
254
|
+
const rows = t.rows.flatMap((row) => t.header.map((_, i) => (row[i] ? cell(row[i]!.tokens, inlines(row[i]!.tokens)) : "[]")));
|
|
125
255
|
return [
|
|
126
256
|
"#table(",
|
|
127
257
|
` columns: ${t.header.length},`,
|
package/src/tools.ts
CHANGED
|
@@ -60,7 +60,7 @@ export function pdfTools(opts: PdfToolsOptions): Tool[] {
|
|
|
60
60
|
markdown: { type: "string", description: "The whole document, in Markdown. Start with a # heading for the title." },
|
|
61
61
|
filename: { type: "string", description: 'The attachment\'s filename, e.g. "revenue-2026-w41.pdf". Defaults to the title.' },
|
|
62
62
|
title: { type: "string", description: "The document title (PDF metadata). Defaults to the first heading." },
|
|
63
|
-
lang: { type: "string", description: 'The document language as a BCP 47 tag, e.g. "en", "zh-TW", "ja".' },
|
|
63
|
+
lang: { type: "string", description: 'The document language as a BCP 47 tag, e.g. "en", "zh-TW", "ja", "ar". It sets hyphenation, quotes, CJK line breaking and the page direction, right to left for Arabic, Hebrew, Persian or Urdu; pass it.' },
|
|
64
64
|
paper: { type: "string", enum: ["a4", "us-letter"], description: "Paper size (default a4)." },
|
|
65
65
|
},
|
|
66
66
|
required: ["markdown"],
|