@haklex/rich-headless 0.1.1 → 0.3.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.
@@ -1,436 +1,401 @@
1
- import { CHECK_LIST, TRANSFORMERS, $convertToMarkdownString } from "@lexical/markdown";
2
- import { $isTableNode, $isTableRowNode, $isTableCellNode } from "@lexical/table";
3
- const NEVER = /a^/;
4
- const NOOP = () => {
5
- };
1
+ import { $isTableCellNode, $isTableNode, $isTableRowNode } from "@lexical/table";
2
+ import { $convertToMarkdownString, CHECK_LIST, TRANSFORMERS } from "@lexical/markdown";
3
+ //#region src/transformers.ts
4
+ /**
5
+ * Headless Markdown transformers — export-only (Lexical → Markdown).
6
+ * Import stubs are no-ops; used only for $convertToMarkdownString.
7
+ */
8
+ var NEVER = /a^/;
9
+ var NOOP = () => {};
6
10
  function qA(v) {
7
- return v.replaceAll('"', '\\"');
11
+ return v.replaceAll("\"", "\\\"");
8
12
  }
9
13
  function escHtml(v) {
10
- return v.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
14
+ return v.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
11
15
  }
12
16
  function escCell(v) {
13
- return v.replaceAll("|", "\\|").replaceAll("\n", "<br/>");
17
+ return v.replaceAll("|", "\\|").replaceAll("\n", "<br/>");
14
18
  }
15
19
  function fence(content) {
16
- const m = content.match(/`+/g);
17
- const max = m?.reduce((a, r) => Math.max(a, r.length), 0) ?? 0;
18
- return "`".repeat(Math.max(3, max + 1));
20
+ const max = content.match(/`+/g)?.reduce((a, r) => Math.max(a, r.length), 0) ?? 0;
21
+ return "`".repeat(Math.max(3, max + 1));
19
22
  }
20
23
  function walkText(node) {
21
- const children = node.children;
22
- if (!children) return node.text ?? "";
23
- return children.map(walkText).join("\n");
24
+ const children = node.children;
25
+ if (!children) return node.text ?? "";
26
+ return children.map(walkText).join("\n");
24
27
  }
25
28
  function stateText(state) {
26
- if (!state || typeof state !== "object") return "";
27
- const root = state.root;
28
- return root ? walkText(root) : "";
29
+ if (!state || typeof state !== "object") return "";
30
+ const root = state.root;
31
+ return root ? walkText(root) : "";
29
32
  }
30
- const SPOILER_TRANSFORMER = {
31
- dependencies: [],
32
- export: (node) => node.getType() === "spoiler" ? `||${node.getTextContent()}||` : null,
33
- importRegExp: /\|\|(.+?)\|\|/,
34
- regExp: /\|\|(.+?)\|\|$/,
35
- replace: NOOP,
36
- trigger: "|",
37
- type: "text-match"
33
+ var SPOILER_TRANSFORMER = {
34
+ dependencies: [],
35
+ export: (node) => node.getType() === "spoiler" ? `||${node.getTextContent()}||` : null,
36
+ importRegExp: /\|\|(.+?)\|\|/,
37
+ regExp: /\|\|(.+?)\|\|$/,
38
+ replace: NOOP,
39
+ trigger: "|",
40
+ type: "text-match"
41
+ };
42
+ var MENTION_TRANSFORMER = {
43
+ dependencies: [],
44
+ export: (node) => {
45
+ if (node.getType() !== "mention") return null;
46
+ const j = node.exportJSON();
47
+ const base = `{${j.platform}@${j.handle}}`;
48
+ return j.displayName ? `[${j.displayName}]${base}` : base;
49
+ },
50
+ importRegExp: /(?:\[([^\]]+)\])?\{(\w+)@(\w[\w.-]*)\}/,
51
+ regExp: /(?:\[([^\]]+)\])?\{(\w+)@(\w[\w.-]*)\}$/,
52
+ replace: NOOP,
53
+ trigger: "}",
54
+ type: "text-match"
38
55
  };
39
- const MENTION_TRANSFORMER = {
40
- dependencies: [],
41
- export: (node) => {
42
- if (node.getType() !== "mention") return null;
43
- const j = node.exportJSON();
44
- const base = `{${j.platform}@${j.handle}}`;
45
- return j.displayName ? `[${j.displayName}]${base}` : base;
46
- },
47
- importRegExp: /(?:\[([^\]]+)\])?\{(\w+)@(\w[\w.-]*)\}/,
48
- regExp: /(?:\[([^\]]+)\])?\{(\w+)@(\w[\w.-]*)\}$/,
49
- replace: NOOP,
50
- trigger: "}",
51
- type: "text-match"
56
+ var FOOTNOTE_TRANSFORMER = {
57
+ dependencies: [],
58
+ export: (node) => node.getType() === "footnote" ? `[^${node.__identifier ?? ""}]` : null,
59
+ importRegExp: /\[\^(\w+)\]/,
60
+ regExp: /\[\^(\w+)\]$/,
61
+ replace: NOOP,
62
+ trigger: "]",
63
+ type: "text-match"
52
64
  };
53
- const FOOTNOTE_TRANSFORMER = {
54
- dependencies: [],
55
- export: (node) => node.getType() === "footnote" ? `[^${node.__identifier ?? ""}]` : null,
56
- importRegExp: /\[\^(\w+)\]/,
57
- regExp: /\[\^(\w+)\]$/,
58
- replace: NOOP,
59
- trigger: "]",
60
- type: "text-match"
65
+ var KATEX_INLINE_TRANSFORMER = {
66
+ dependencies: [],
67
+ export: (node) => node.getType() === "katex-inline" ? `$${node.__equation ?? ""}$` : null,
68
+ importRegExp: /\$([^\n$]+)\$/,
69
+ regExp: /\$([^\n$]+)\$$/,
70
+ replace: NOOP,
71
+ trigger: "$",
72
+ type: "text-match"
61
73
  };
62
- const KATEX_INLINE_TRANSFORMER = {
63
- dependencies: [],
64
- export: (node) => node.getType() === "katex-inline" ? `$${node.__equation ?? ""}$` : null,
65
- importRegExp: /\$([^\n$]+)\$/,
66
- regExp: /\$([^\n$]+)\$$/,
67
- replace: NOOP,
68
- trigger: "$",
69
- type: "text-match"
74
+ var RUBY_TRANSFORMER = {
75
+ dependencies: [],
76
+ export: (node) => {
77
+ if (node.getType() !== "ruby") return null;
78
+ const base = escHtml(node.getTextContent());
79
+ const reading = escHtml(node.__reading ?? "");
80
+ if (!reading) return base;
81
+ return `<ruby>${base}<rt>${reading}</rt></ruby>`;
82
+ },
83
+ importRegExp: /<ruby>([^<]*)<rt>([^<]*)<\/rt>(?:<rp>[^<]*<\/rp>)*<\/ruby>/i,
84
+ regExp: /<ruby>([^<]*)<rt>([^<]*)<\/rt>(?:<rp>[^<]*<\/rp>)*<\/ruby>$/i,
85
+ replace: NOOP,
86
+ trigger: ">",
87
+ type: "text-match"
70
88
  };
71
- const RUBY_TRANSFORMER = {
72
- dependencies: [],
73
- export: (node) => {
74
- if (node.getType() !== "ruby") return null;
75
- const base = escHtml(node.getTextContent());
76
- const reading = escHtml(node.__reading ?? "");
77
- if (!reading) return base;
78
- return `<ruby>${base}<rt>${reading}</rt></ruby>`;
79
- },
80
- importRegExp: /<ruby>([^<]*)<rt>([^<]*)<\/rt>(?:<rp>[^<]*<\/rp>)*<\/ruby>/i,
81
- regExp: /<ruby>([^<]*)<rt>([^<]*)<\/rt>(?:<rp>[^<]*<\/rp>)*<\/ruby>$/i,
82
- replace: NOOP,
83
- trigger: ">",
84
- type: "text-match"
89
+ var INSERT_TRANSFORMER = {
90
+ format: ["underline"],
91
+ tag: "++",
92
+ type: "text-format"
85
93
  };
86
- const INSERT_TRANSFORMER = {
87
- format: ["underline"],
88
- tag: "++",
89
- type: "text-format"
94
+ var SUPERSCRIPT_TRANSFORMER = {
95
+ format: ["superscript"],
96
+ tag: "^",
97
+ type: "text-format"
90
98
  };
91
- const SUPERSCRIPT_TRANSFORMER = {
92
- format: ["superscript"],
93
- tag: "^",
94
- type: "text-format"
99
+ var SUBSCRIPT_TRANSFORMER = {
100
+ format: ["subscript"],
101
+ tag: "~",
102
+ type: "text-format"
95
103
  };
96
- const SUBSCRIPT_TRANSFORMER = {
97
- format: ["subscript"],
98
- tag: "~",
99
- type: "text-format"
104
+ var TAG_TRANSFORMER = {
105
+ dependencies: [],
106
+ export: (node) => node.getType() === "tag" ? `<tag>${node.__text ?? ""}</tag>` : null,
107
+ importRegExp: /<tag>([^<]+)<\/tag>/,
108
+ regExp: /<tag>([^<]+)<\/tag>$/,
109
+ replace: NOOP,
110
+ trigger: ">",
111
+ type: "text-match"
100
112
  };
101
- const TAG_TRANSFORMER = {
102
- dependencies: [],
103
- export: (node) => node.getType() === "tag" ? `<tag>${node.__text ?? ""}</tag>` : null,
104
- importRegExp: /<tag>([^<]+)<\/tag>/,
105
- regExp: /<tag>([^<]+)<\/tag>$/,
106
- replace: NOOP,
107
- trigger: ">",
108
- type: "text-match"
113
+ var COMMENT_TRANSFORMER = {
114
+ dependencies: [],
115
+ export: (node) => node.getType() === "comment" ? `<!--${node.exportJSON().text ?? ""}-->` : null,
116
+ importRegExp: /<!--([\s\S]*?)-->/,
117
+ regExp: /<!--([\s\S]*?)-->$/,
118
+ replace: NOOP,
119
+ trigger: ">",
120
+ type: "text-match"
109
121
  };
110
- const COMMENT_TRANSFORMER = {
111
- dependencies: [],
112
- export: (node) => node.getType() === "comment" ? `<!--${node.exportJSON().text ?? ""}-->` : null,
113
- importRegExp: /<!--([\s\S]*?)-->/,
114
- regExp: /<!--([\s\S]*?)-->$/,
115
- replace: NOOP,
116
- trigger: ">",
117
- type: "text-match"
122
+ var FOOTNOTE_SECTION_TRANSFORMER = {
123
+ dependencies: [],
124
+ export: (node) => {
125
+ if (node.getType() !== "footnote-section") return null;
126
+ const defs = node.__definitions ?? {};
127
+ return Object.entries(defs).map(([id, content]) => `[^${id}]: ${content}`).join("\n");
128
+ },
129
+ regExp: NEVER,
130
+ replace: NOOP,
131
+ type: "element"
118
132
  };
119
- const FOOTNOTE_SECTION_TRANSFORMER = {
120
- dependencies: [],
121
- export: (node) => {
122
- if (node.getType() !== "footnote-section") return null;
123
- const defs = node.__definitions ?? {};
124
- return Object.entries(defs).map(([id, content]) => `[^${id}]: ${content}`).join("\n");
125
- },
126
- regExp: NEVER,
127
- replace: NOOP,
128
- type: "element"
133
+ var CONTAINER_TRANSFORMER = {
134
+ dependencies: [],
135
+ export: (node) => {
136
+ const t = node.getType();
137
+ if (t === "banner") return `::: ${node.__bannerType ?? "note"}\n${node.getTextContent()}\n:::`;
138
+ if (t === "details") return `::: details{summary="${qA(node.__summary ?? "")}"}\n${node.getTextContent()}\n:::`;
139
+ return null;
140
+ },
141
+ regExp: NEVER,
142
+ replace: NOOP,
143
+ type: "element"
129
144
  };
130
- const CONTAINER_TRANSFORMER = {
131
- dependencies: [],
132
- export: (node) => {
133
- const t = node.getType();
134
- if (t === "banner") {
135
- const type = node.__bannerType ?? "note";
136
- return `::: ${type}
137
- ${node.getTextContent()}
138
- :::`;
139
- }
140
- if (t === "details") {
141
- const summary = node.__summary ?? "";
142
- return `::: details{summary="${qA(summary)}"}
143
- ${node.getTextContent()}
144
- :::`;
145
- }
146
- return null;
147
- },
148
- regExp: NEVER,
149
- replace: NOOP,
150
- type: "element"
145
+ var ALERT_KEY = {
146
+ note: "NOTE",
147
+ tip: "TIP",
148
+ important: "IMPORTANT",
149
+ warning: "WARNING",
150
+ caution: "CAUTION"
151
151
  };
152
- const ALERT_KEY = {
153
- note: "NOTE",
154
- tip: "TIP",
155
- important: "IMPORTANT",
156
- warning: "WARNING",
157
- caution: "CAUTION"
152
+ var GIT_ALERT_TRANSFORMER = {
153
+ dependencies: [],
154
+ export: (node) => {
155
+ if (node.getType() !== "alert-quote") return null;
156
+ const key = ALERT_KEY[node.__alertType ?? "note"] ?? "NOTE";
157
+ const lines = node.getTextContent().split("\n");
158
+ return [`> [!${key}]`, ...lines.map((l) => `> ${l}`)].join("\n");
159
+ },
160
+ regExp: NEVER,
161
+ replace: NOOP,
162
+ type: "element"
158
163
  };
159
- const GIT_ALERT_TRANSFORMER = {
160
- dependencies: [],
161
- export: (node) => {
162
- if (node.getType() !== "alert-quote") return null;
163
- const key = ALERT_KEY[node.__alertType ?? "note"] ?? "NOTE";
164
- const lines = node.getTextContent().split("\n");
165
- return [`> [!${key}]`, ...lines.map((l) => `> ${l}`)].join("\n");
166
- },
167
- regExp: NEVER,
168
- replace: NOOP,
169
- type: "element"
164
+ var KATEX_BLOCK_TRANSFORMER = {
165
+ dependencies: [],
166
+ export: (node) => node.getType() === "katex-block" ? `$$${node.__equation ?? ""}$$` : null,
167
+ importRegExp: /\$\$([^$]+)\$\$/,
168
+ regExp: /\$\$([^$]+)\$\$$/,
169
+ replace: NOOP,
170
+ trigger: "$",
171
+ type: "text-match"
170
172
  };
171
- const KATEX_BLOCK_TRANSFORMER = {
172
- dependencies: [],
173
- export: (node) => node.getType() === "katex-block" ? `$$${node.__equation ?? ""}$$` : null,
174
- importRegExp: /\$\$([^$]+)\$\$/,
175
- regExp: /\$\$([^$]+)\$\$$/,
176
- replace: NOOP,
177
- trigger: "$",
178
- type: "text-match"
173
+ var IMAGE_BLOCK_TRANSFORMER = {
174
+ dependencies: [],
175
+ export: (node) => {
176
+ if (node.getType() !== "image") return null;
177
+ const j = node.exportJSON();
178
+ const title = j.caption ? ` "${qA(j.caption)}"` : "";
179
+ return `![${j.altText || ""}](${j.src}${title})`;
180
+ },
181
+ regExp: NEVER,
182
+ replace: NOOP,
183
+ type: "element"
179
184
  };
180
- const IMAGE_BLOCK_TRANSFORMER = {
181
- dependencies: [],
182
- export: (node) => {
183
- if (node.getType() !== "image") return null;
184
- const j = node.exportJSON();
185
- const title = j.caption ? ` "${qA(j.caption)}"` : "";
186
- return `![${j.altText || ""}](${j.src}${title})`;
187
- },
188
- regExp: NEVER,
189
- replace: NOOP,
190
- type: "element"
185
+ var VIDEO_BLOCK_TRANSFORMER = {
186
+ dependencies: [],
187
+ export: (node) => {
188
+ if (node.getType() !== "video") return null;
189
+ const j = node.exportJSON();
190
+ const attrs = [`src="${qA(j.src)}"`];
191
+ if (j.poster) attrs.push(`poster="${qA(j.poster)}"`);
192
+ if (j.width) attrs.push(`width=${j.width}`);
193
+ if (j.height) attrs.push(`height=${j.height}`);
194
+ return `<video ${attrs.join(" ")} controls></video>`;
195
+ },
196
+ regExp: NEVER,
197
+ replace: NOOP,
198
+ type: "element"
191
199
  };
192
- const VIDEO_BLOCK_TRANSFORMER = {
193
- dependencies: [],
194
- export: (node) => {
195
- if (node.getType() !== "video") return null;
196
- const j = node.exportJSON();
197
- const attrs = [`src="${qA(j.src)}"`];
198
- if (j.poster) attrs.push(`poster="${qA(j.poster)}"`);
199
- if (j.width) attrs.push(`width=${j.width}`);
200
- if (j.height) attrs.push(`height=${j.height}`);
201
- return `<video ${attrs.join(" ")} controls></video>`;
202
- },
203
- regExp: NEVER,
204
- replace: NOOP,
205
- type: "element"
200
+ var CODE_BLOCK_NODE_TRANSFORMER = {
201
+ dependencies: [],
202
+ export: (node) => {
203
+ if (node.getType() !== "code-block") return null;
204
+ const j = node.exportJSON();
205
+ const f = fence(j.code || "");
206
+ return `${f}${j.language || ""}\n${j.code || ""}\n${f}`;
207
+ },
208
+ regExp: NEVER,
209
+ replace: NOOP,
210
+ type: "element"
206
211
  };
207
- const CODE_BLOCK_NODE_TRANSFORMER = {
208
- dependencies: [],
209
- export: (node) => {
210
- if (node.getType() !== "code-block") return null;
211
- const j = node.exportJSON();
212
- const f = fence(j.code || "");
213
- return `${f}${j.language || ""}
214
- ${j.code || ""}
215
- ${f}`;
216
- },
217
- regExp: NEVER,
218
- replace: NOOP,
219
- type: "element"
212
+ var LEGACY_CODE_NODE_TRANSFORMER = {
213
+ dependencies: [],
214
+ export: (node) => {
215
+ if (node.getType() !== "code") return null;
216
+ const lang = node.getLanguage?.() ?? "";
217
+ const text = node.getTextContent();
218
+ const f = fence(text);
219
+ return `${f}${lang}\n${text}\n${f}`;
220
+ },
221
+ regExp: NEVER,
222
+ replace: NOOP,
223
+ type: "element"
220
224
  };
221
- const LEGACY_CODE_NODE_TRANSFORMER = {
222
- dependencies: [],
223
- export: (node) => {
224
- if (node.getType() !== "code") return null;
225
- const lang = node.getLanguage?.() ?? "";
226
- const text = node.getTextContent();
227
- const f = fence(text);
228
- return `${f}${lang}
229
- ${text}
230
- ${f}`;
231
- },
232
- regExp: NEVER,
233
- replace: NOOP,
234
- type: "element"
225
+ var LINK_CARD_BLOCK_TRANSFORMER = {
226
+ dependencies: [],
227
+ export: (node) => {
228
+ if (node.getType() !== "link-card") return null;
229
+ const j = node.exportJSON();
230
+ return j.title ? `[${j.title}](${j.url})` : `<${j.url}>`;
231
+ },
232
+ regExp: NEVER,
233
+ replace: NOOP,
234
+ type: "element"
235
235
  };
236
- const LINK_CARD_BLOCK_TRANSFORMER = {
237
- dependencies: [],
238
- export: (node) => {
239
- if (node.getType() !== "link-card") return null;
240
- const j = node.exportJSON();
241
- return j.title ? `[${j.title}](${j.url})` : `<${j.url}>`;
242
- },
243
- regExp: NEVER,
244
- replace: NOOP,
245
- type: "element"
236
+ var MERMAID_BLOCK_TRANSFORMER = {
237
+ dependencies: [],
238
+ export: (node) => {
239
+ if (node.getType() !== "mermaid") return null;
240
+ const d = node.__diagram ?? "";
241
+ const f = fence(d);
242
+ return `${f}mermaid\n${d}\n${f}`;
243
+ },
244
+ regExp: NEVER,
245
+ replace: NOOP,
246
+ type: "element"
246
247
  };
247
- const MERMAID_BLOCK_TRANSFORMER = {
248
- dependencies: [],
249
- export: (node) => {
250
- if (node.getType() !== "mermaid") return null;
251
- const d = node.__diagram ?? "";
252
- const f = fence(d);
253
- return `${f}mermaid
254
- ${d}
255
- ${f}`;
256
- },
257
- regExp: NEVER,
258
- replace: NOOP,
259
- type: "element"
248
+ var NESTED_DOC_BLOCK_TRANSFORMER = {
249
+ dependencies: [],
250
+ export: (node) => {
251
+ if (node.getType() !== "nested-doc") return null;
252
+ return `<nested-doc>\n${stateText(node.__contentState)}\n</nested-doc>`;
253
+ },
254
+ regExp: NEVER,
255
+ replace: NOOP,
256
+ type: "element"
260
257
  };
261
- const NESTED_DOC_BLOCK_TRANSFORMER = {
262
- dependencies: [],
263
- export: (node) => {
264
- if (node.getType() !== "nested-doc") return null;
265
- return `<nested-doc>
266
- ${stateText(node.__contentState)}
267
- </nested-doc>`;
268
- },
269
- regExp: NEVER,
270
- replace: NOOP,
271
- type: "element"
258
+ var GRID_CONTAINER_BLOCK_TRANSFORMER = {
259
+ dependencies: [],
260
+ export: (node) => {
261
+ if (node.getType() !== "grid-container") return null;
262
+ const j = node.exportJSON();
263
+ const cells = (j.cells ?? []).map((c) => `::: cell\n${stateText(c)}\n:::`).join("\n");
264
+ return `::: grid{cols=${j.cols ?? 2} gap="${qA(String(j.gap ?? "16px"))}"}\n${cells}\n:::`;
265
+ },
266
+ regExp: NEVER,
267
+ replace: NOOP,
268
+ type: "element"
272
269
  };
273
- const GRID_CONTAINER_BLOCK_TRANSFORMER = {
274
- dependencies: [],
275
- export: (node) => {
276
- if (node.getType() !== "grid-container") return null;
277
- const j = node.exportJSON();
278
- const cells = (j.cells ?? []).map((c) => `::: cell
279
- ${stateText(c)}
280
- :::`).join("\n");
281
- return `::: grid{cols=${j.cols ?? 2} gap="${qA(String(j.gap ?? "16px"))}"}
282
- ${cells}
283
- :::`;
284
- },
285
- regExp: NEVER,
286
- replace: NOOP,
287
- type: "element"
270
+ var HORIZONTAL_RULE_BLOCK_TRANSFORMER = {
271
+ dependencies: [],
272
+ export: (node) => node.getType() === "horizontalrule" ? "---" : null,
273
+ regExp: NEVER,
274
+ replace: NOOP,
275
+ type: "element"
288
276
  };
289
- const HORIZONTAL_RULE_BLOCK_TRANSFORMER = {
290
- dependencies: [],
291
- export: (node) => node.getType() === "horizontalrule" ? "---" : null,
292
- regExp: NEVER,
293
- replace: NOOP,
294
- type: "element"
277
+ var TABLE_BLOCK_TRANSFORMER = {
278
+ dependencies: [],
279
+ export: (node) => {
280
+ if (!$isTableNode(node)) return null;
281
+ const rows = node.getChildren().filter($isTableRowNode);
282
+ if (rows.length === 0) return "| |\n| --- |";
283
+ const data = rows.map((row) => row.getChildren().filter($isTableCellNode).map((c) => escCell(c.getTextContent())));
284
+ const hdr = data[0];
285
+ const sep = hdr.map(() => "---");
286
+ return [
287
+ `| ${hdr.join(" | ")} |`,
288
+ `| ${sep.join(" | ")} |`,
289
+ ...data.slice(1).map((r) => `| ${r.join(" | ")} |`)
290
+ ].join("\n");
291
+ },
292
+ regExp: NEVER,
293
+ replace: NOOP,
294
+ type: "element"
295
295
  };
296
- const TABLE_BLOCK_TRANSFORMER = {
297
- dependencies: [],
298
- export: (node) => {
299
- if (!$isTableNode(node)) return null;
300
- const rows = node.getChildren().filter($isTableRowNode);
301
- if (rows.length === 0) return "| |\n| --- |";
302
- const data = rows.map(
303
- (row) => row.getChildren().filter($isTableCellNode).map((c) => escCell(c.getTextContent()))
304
- );
305
- const hdr = data[0];
306
- const sep = hdr.map(() => "---");
307
- return [
308
- `| ${hdr.join(" | ")} |`,
309
- `| ${sep.join(" | ")} |`,
310
- ...data.slice(1).map((r) => `| ${r.join(" | ")} |`)
311
- ].join("\n");
312
- },
313
- regExp: NEVER,
314
- replace: NOOP,
315
- type: "element"
296
+ var CODE_SNIPPET_BLOCK_TRANSFORMER = {
297
+ dependencies: [],
298
+ export: (node) => {
299
+ if (node.getType() !== "code-snippet") return null;
300
+ return `::: code-snippet\n${(node.exportJSON().files ?? []).map((f) => {
301
+ const fc = fence(f.code || "");
302
+ return [
303
+ `::: file{name="${qA(f.filename || "untitled")}" lang="${qA(f.language || "")}"}`,
304
+ `${fc}${f.language || ""}`,
305
+ f.code || "",
306
+ fc,
307
+ ":::"
308
+ ].join("\n");
309
+ }).join("\n")}\n:::`;
310
+ },
311
+ regExp: NEVER,
312
+ replace: NOOP,
313
+ type: "element"
316
314
  };
317
- const CODE_SNIPPET_BLOCK_TRANSFORMER = {
318
- dependencies: [],
319
- export: (node) => {
320
- if (node.getType() !== "code-snippet") return null;
321
- const j = node.exportJSON();
322
- const body = (j.files ?? []).map((f) => {
323
- const fc = fence(f.code || "");
324
- return [
325
- `::: file{name="${qA(f.filename || "untitled")}" lang="${qA(f.language || "")}"}`,
326
- `${fc}${f.language || ""}`,
327
- f.code || "",
328
- fc,
329
- ":::"
330
- ].join("\n");
331
- }).join("\n");
332
- return `::: code-snippet
333
- ${body}
334
- :::`;
335
- },
336
- regExp: NEVER,
337
- replace: NOOP,
338
- type: "element"
315
+ var EMBED_BLOCK_TRANSFORMER = {
316
+ dependencies: [],
317
+ export: (node) => node.getType() === "embed" ? `<${node.__url || ""}>` : null,
318
+ regExp: NEVER,
319
+ replace: NOOP,
320
+ type: "element"
339
321
  };
340
- const EMBED_BLOCK_TRANSFORMER = {
341
- dependencies: [],
342
- export: (node) => node.getType() === "embed" ? `<${node.__url || ""}>` : null,
343
- regExp: NEVER,
344
- replace: NOOP,
345
- type: "element"
322
+ var GALLERY_BLOCK_TRANSFORMER = {
323
+ dependencies: [],
324
+ export: (node) => {
325
+ if (node.getType() !== "gallery") return null;
326
+ return (node.exportJSON().images ?? []).map((i) => `![${i.alt || ""}](${i.src || ""})`).join("\n");
327
+ },
328
+ regExp: NEVER,
329
+ replace: NOOP,
330
+ type: "element"
346
331
  };
347
- const GALLERY_BLOCK_TRANSFORMER = {
348
- dependencies: [],
349
- export: (node) => {
350
- if (node.getType() !== "gallery") return null;
351
- const imgs = node.exportJSON().images ?? [];
352
- return imgs.map((i) => `![${i.alt || ""}](${i.src || ""})`).join("\n");
353
- },
354
- regExp: NEVER,
355
- replace: NOOP,
356
- type: "element"
332
+ var EXCALIDRAW_BLOCK_TRANSFORMER = {
333
+ dependencies: [],
334
+ export: (node) => node.getType() === "excalidraw" ? `<excalidraw>\n${node.__snapshot || ""}\n</excalidraw>` : null,
335
+ regExp: NEVER,
336
+ replace: NOOP,
337
+ type: "element"
357
338
  };
358
- const EXCALIDRAW_BLOCK_TRANSFORMER = {
359
- dependencies: [],
360
- export: (node) => node.getType() === "excalidraw" ? `<excalidraw>
361
- ${node.__snapshot || ""}
362
- </excalidraw>` : null,
363
- regExp: NEVER,
364
- replace: NOOP,
365
- type: "element"
339
+ var POLL_BLOCK_TRANSFORMER = {
340
+ dependencies: [],
341
+ export: (node) => {
342
+ if (node.getType() !== "poll") return null;
343
+ const j = node.exportJSON();
344
+ const meta = {
345
+ pollId: j.pollId,
346
+ mode: j.mode
347
+ };
348
+ if (j.closeAt) meta.closeAt = j.closeAt;
349
+ if (j.showResults) meta.showResults = j.showResults;
350
+ const optionLines = (j.options ?? []).map((option) => `- ${escHtml(option.label || "")} <!-- id=${option.id} -->`);
351
+ return [
352
+ `<!--haklex:poll ${JSON.stringify(meta)}-->`,
353
+ `**${escHtml(j.question || "")}**`,
354
+ "",
355
+ ...optionLines,
356
+ "<!--/haklex:poll-->"
357
+ ].join("\n");
358
+ },
359
+ regExp: NEVER,
360
+ replace: NOOP,
361
+ type: "element"
366
362
  };
367
- const allHeadlessTransformers = [
368
- // Inline
369
- SPOILER_TRANSFORMER,
370
- MENTION_TRANSFORMER,
371
- FOOTNOTE_TRANSFORMER,
372
- INSERT_TRANSFORMER,
373
- SUPERSCRIPT_TRANSFORMER,
374
- SUBSCRIPT_TRANSFORMER,
375
- RUBY_TRANSFORMER,
376
- KATEX_INLINE_TRANSFORMER,
377
- TAG_TRANSFORMER,
378
- COMMENT_TRANSFORMER,
379
- // Block (specific first)
380
- FOOTNOTE_SECTION_TRANSFORMER,
381
- CONTAINER_TRANSFORMER,
382
- GIT_ALERT_TRANSFORMER,
383
- CHECK_LIST,
384
- KATEX_BLOCK_TRANSFORMER,
385
- IMAGE_BLOCK_TRANSFORMER,
386
- VIDEO_BLOCK_TRANSFORMER,
387
- CODE_BLOCK_NODE_TRANSFORMER,
388
- LEGACY_CODE_NODE_TRANSFORMER,
389
- LINK_CARD_BLOCK_TRANSFORMER,
390
- MERMAID_BLOCK_TRANSFORMER,
391
- NESTED_DOC_BLOCK_TRANSFORMER,
392
- GRID_CONTAINER_BLOCK_TRANSFORMER,
393
- HORIZONTAL_RULE_BLOCK_TRANSFORMER,
394
- TABLE_BLOCK_TRANSFORMER,
395
- CODE_SNIPPET_BLOCK_TRANSFORMER,
396
- EMBED_BLOCK_TRANSFORMER,
397
- GALLERY_BLOCK_TRANSFORMER,
398
- EXCALIDRAW_BLOCK_TRANSFORMER,
399
- // Standard (heading, quote, list, code, bold, italic, strikethrough, link…)
400
- ...TRANSFORMERS
363
+ var allHeadlessTransformers = [
364
+ SPOILER_TRANSFORMER,
365
+ MENTION_TRANSFORMER,
366
+ FOOTNOTE_TRANSFORMER,
367
+ INSERT_TRANSFORMER,
368
+ SUPERSCRIPT_TRANSFORMER,
369
+ SUBSCRIPT_TRANSFORMER,
370
+ RUBY_TRANSFORMER,
371
+ KATEX_INLINE_TRANSFORMER,
372
+ TAG_TRANSFORMER,
373
+ COMMENT_TRANSFORMER,
374
+ FOOTNOTE_SECTION_TRANSFORMER,
375
+ CONTAINER_TRANSFORMER,
376
+ GIT_ALERT_TRANSFORMER,
377
+ CHECK_LIST,
378
+ KATEX_BLOCK_TRANSFORMER,
379
+ IMAGE_BLOCK_TRANSFORMER,
380
+ VIDEO_BLOCK_TRANSFORMER,
381
+ CODE_BLOCK_NODE_TRANSFORMER,
382
+ LEGACY_CODE_NODE_TRANSFORMER,
383
+ LINK_CARD_BLOCK_TRANSFORMER,
384
+ MERMAID_BLOCK_TRANSFORMER,
385
+ NESTED_DOC_BLOCK_TRANSFORMER,
386
+ GRID_CONTAINER_BLOCK_TRANSFORMER,
387
+ HORIZONTAL_RULE_BLOCK_TRANSFORMER,
388
+ TABLE_BLOCK_TRANSFORMER,
389
+ CODE_SNIPPET_BLOCK_TRANSFORMER,
390
+ EMBED_BLOCK_TRANSFORMER,
391
+ GALLERY_BLOCK_TRANSFORMER,
392
+ EXCALIDRAW_BLOCK_TRANSFORMER,
393
+ POLL_BLOCK_TRANSFORMER,
394
+ ...TRANSFORMERS
401
395
  ];
396
+ /** Call inside editor.read() to convert current state to Markdown. */
402
397
  function $toMarkdown() {
403
- return $convertToMarkdownString(allHeadlessTransformers);
398
+ return $convertToMarkdownString(allHeadlessTransformers);
404
399
  }
405
- export {
406
- $toMarkdown,
407
- CODE_BLOCK_NODE_TRANSFORMER,
408
- CODE_SNIPPET_BLOCK_TRANSFORMER,
409
- COMMENT_TRANSFORMER,
410
- CONTAINER_TRANSFORMER,
411
- EMBED_BLOCK_TRANSFORMER,
412
- EXCALIDRAW_BLOCK_TRANSFORMER,
413
- FOOTNOTE_SECTION_TRANSFORMER,
414
- FOOTNOTE_TRANSFORMER,
415
- GALLERY_BLOCK_TRANSFORMER,
416
- GIT_ALERT_TRANSFORMER,
417
- GRID_CONTAINER_BLOCK_TRANSFORMER,
418
- HORIZONTAL_RULE_BLOCK_TRANSFORMER,
419
- IMAGE_BLOCK_TRANSFORMER,
420
- INSERT_TRANSFORMER,
421
- KATEX_BLOCK_TRANSFORMER,
422
- KATEX_INLINE_TRANSFORMER,
423
- LEGACY_CODE_NODE_TRANSFORMER,
424
- LINK_CARD_BLOCK_TRANSFORMER,
425
- MENTION_TRANSFORMER,
426
- MERMAID_BLOCK_TRANSFORMER,
427
- NESTED_DOC_BLOCK_TRANSFORMER,
428
- RUBY_TRANSFORMER,
429
- SPOILER_TRANSFORMER,
430
- SUBSCRIPT_TRANSFORMER,
431
- SUPERSCRIPT_TRANSFORMER,
432
- TABLE_BLOCK_TRANSFORMER,
433
- TAG_TRANSFORMER,
434
- VIDEO_BLOCK_TRANSFORMER,
435
- allHeadlessTransformers
436
- };
400
+ //#endregion
401
+ export { $toMarkdown, CODE_BLOCK_NODE_TRANSFORMER, CODE_SNIPPET_BLOCK_TRANSFORMER, COMMENT_TRANSFORMER, CONTAINER_TRANSFORMER, EMBED_BLOCK_TRANSFORMER, EXCALIDRAW_BLOCK_TRANSFORMER, FOOTNOTE_SECTION_TRANSFORMER, FOOTNOTE_TRANSFORMER, GALLERY_BLOCK_TRANSFORMER, GIT_ALERT_TRANSFORMER, GRID_CONTAINER_BLOCK_TRANSFORMER, HORIZONTAL_RULE_BLOCK_TRANSFORMER, IMAGE_BLOCK_TRANSFORMER, INSERT_TRANSFORMER, KATEX_BLOCK_TRANSFORMER, KATEX_INLINE_TRANSFORMER, LEGACY_CODE_NODE_TRANSFORMER, LINK_CARD_BLOCK_TRANSFORMER, MENTION_TRANSFORMER, MERMAID_BLOCK_TRANSFORMER, NESTED_DOC_BLOCK_TRANSFORMER, POLL_BLOCK_TRANSFORMER, RUBY_TRANSFORMER, SPOILER_TRANSFORMER, SUBSCRIPT_TRANSFORMER, SUPERSCRIPT_TRANSFORMER, TABLE_BLOCK_TRANSFORMER, TAG_TRANSFORMER, VIDEO_BLOCK_TRANSFORMER, allHeadlessTransformers };