@threadlabs/looma 0.45.2 → 0.46.1
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/EXPRESSIVE-MARKDOWN.md +23 -17
- package/components/ui-section/ui-section.html +18 -7
- package/components/ui-site-header/ui-site-header.html +25 -6
- package/components/ui-site-header/ui-site-header.js +4 -3
- package/dist/index.js +1 -1
- package/docs/catalog.json +3 -3
- package/editor/chunks/expressmark.js +1562 -0
- package/editor/chunks/extensions.js +327 -1051
- package/editor/expressmark.d.ts +17 -5
- package/editor/expressmark.js +1 -1037
- package/editor/extensions/footnote.d.ts +7 -12
- package/editor/extensions/index.d.ts +2 -2
- package/editor/extensions/index.js +3 -3
- package/editor/extensions/link-definition.d.ts +10 -0
- package/editor/index.js +3 -3
- package/markdown/fixtures.d.ts +1 -1
- package/markdown/fixtures.js +22 -16
- package/markdown/index.d.ts +2 -3
- package/markdown/index.js +2 -3
- package/package.json +2 -9
- package/styles/ui-section.css +12 -3
- package/styles/ui-site-header.css +54 -6
- package/vanilla/UiSection.js +6 -5
- package/vanilla/UiSiteHeader.js +29 -44
- package/vue/UiSection.vue +19 -12
- package/vue/UiSiteHeader.d.ts +2 -2
- package/vue/UiSiteHeader.vue +157 -72
- package/vue/chunks/UiSection.js +32 -29
- package/vue/chunks/UiSiteHeader.js +27 -49
- package/vue/components.css +151 -8
- package/vue/editor/LoomaEditor.d.ts +1 -1
- package/vue/editor/index.js +225 -186
- package/editor/chunks/reference-display.js +0 -453
- package/editor/extensions/markdown.d.ts +0 -16
- package/markdown/markdown-it.d.ts +0 -21
- package/markdown/markdown-it.js +0 -562
|
@@ -0,0 +1,1562 @@
|
|
|
1
|
+
import { Extension, Node, mergeAttributes } from "@tiptap/core";
|
|
2
|
+
import { NodeSelection, Plugin, PluginKey } from "@tiptap/pm/state";
|
|
3
|
+
import { parse, stringify } from "expressmark";
|
|
4
|
+
//#region src/markdown/index.ts
|
|
5
|
+
/**
|
|
6
|
+
* Callout tones a document stores, in the order the editor offers them. The Callout component also
|
|
7
|
+
* takes `neutral` and `success`, which Markdown and the editor store as `note`.
|
|
8
|
+
*/
|
|
9
|
+
var LOOMA_CALLOUT_TONES = [
|
|
10
|
+
"note",
|
|
11
|
+
"tip",
|
|
12
|
+
"info",
|
|
13
|
+
"important",
|
|
14
|
+
"warning",
|
|
15
|
+
"danger"
|
|
16
|
+
];
|
|
17
|
+
/** Names that read as a stored tone: `caution` is `danger`, and `neutral` is the same muted aside as `note`. */
|
|
18
|
+
var LOOMA_CALLOUT_TONE_ALIASES = {
|
|
19
|
+
caution: "danger",
|
|
20
|
+
neutral: "note"
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Maps a tone name or alias, in any case, onto a stored tone.
|
|
24
|
+
* @failure Unknown and non-string values become `note`.
|
|
25
|
+
*/
|
|
26
|
+
function normalizeLoomaCalloutTone(value) {
|
|
27
|
+
if (typeof value !== "string") return "note";
|
|
28
|
+
const tone = value.toLowerCase();
|
|
29
|
+
return LOOMA_CALLOUT_TONES.includes(tone) ? tone : LOOMA_CALLOUT_TONE_ALIASES[tone] ?? "note";
|
|
30
|
+
}
|
|
31
|
+
/** The hues chips and swatches take as their tone, written `:chip[Label]{tone=green}`. */
|
|
32
|
+
var LOOMA_CHIP_TONES = [
|
|
33
|
+
"neutral",
|
|
34
|
+
"blue",
|
|
35
|
+
"green",
|
|
36
|
+
"yellow",
|
|
37
|
+
"red",
|
|
38
|
+
"purple"
|
|
39
|
+
];
|
|
40
|
+
/**
|
|
41
|
+
* Keeps a chip tone within the palette.
|
|
42
|
+
* @failure Unknown values become `neutral`.
|
|
43
|
+
*/
|
|
44
|
+
function normalizeLoomaChipTone(value) {
|
|
45
|
+
return LOOMA_CHIP_TONES.includes(value) ? value : "neutral";
|
|
46
|
+
}
|
|
47
|
+
[...LOOMA_CALLOUT_TONES];
|
|
48
|
+
/**
|
|
49
|
+
* The anchor a heading's text derives: NFC, lowercase, keeping letters, numbers and combining
|
|
50
|
+
* marks, with every other run of characters one hyphen.
|
|
51
|
+
* @failure Text with no letters or numbers, such as only emoji, derives `heading`.
|
|
52
|
+
*/
|
|
53
|
+
function loomaHeadingSlug(text) {
|
|
54
|
+
return text.normalize("NFC").toLowerCase().replace(/[^\p{L}\p{N}\p{M}]+/gu, "-").replace(/^-+|-+$/g, "") || "heading";
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Gives every heading of a document its anchor, in document order.
|
|
58
|
+
* @contract A custom ID is kept unless an earlier heading already has it. Any other heading takes
|
|
59
|
+
* its slug, or the slug's first free suffix (`-1`, `-2`…), never one a later custom ID reserves.
|
|
60
|
+
*/
|
|
61
|
+
function assignLoomaHeadingIds(headings) {
|
|
62
|
+
const reserved = new Set(headings.map(({ id }) => id).filter((id) => Boolean(id)));
|
|
63
|
+
const used = /* @__PURE__ */ new Set();
|
|
64
|
+
return headings.map(({ text, id: custom }) => {
|
|
65
|
+
let id = custom || loomaHeadingSlug(text);
|
|
66
|
+
if (custom ? used.has(id) : used.has(id) || reserved.has(id)) {
|
|
67
|
+
const stem = id;
|
|
68
|
+
let suffix = 1;
|
|
69
|
+
while (used.has(`${stem}-${suffix}`) || reserved.has(`${stem}-${suffix}`)) suffix++;
|
|
70
|
+
id = `${stem}-${suffix}`;
|
|
71
|
+
}
|
|
72
|
+
used.add(id);
|
|
73
|
+
return id;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
/** Reads a fence's info string: ```` ```ts title="a.ts" {1,3-5} :line-numbers ````. */
|
|
77
|
+
function parseLoomaFenceInfo(info) {
|
|
78
|
+
const source = info.trim();
|
|
79
|
+
const language = /^[^\s={:]+(?=[\s{:]|$)/.exec(source)?.[0];
|
|
80
|
+
const meta = (language ? source.slice(language.length) : source).trim();
|
|
81
|
+
const title = /(?:^|\s)title="([^"]*)"/.exec(meta)?.[1];
|
|
82
|
+
const rest = meta.replace(/(?:^|\s)title="[^"]*"/, "");
|
|
83
|
+
const lines = /* @__PURE__ */ new Set();
|
|
84
|
+
for (const range of /\{([\d\s,-]+)\}/.exec(rest)?.[1].split(",") ?? []) {
|
|
85
|
+
const [from = NaN, to = from] = range.split("-").map((part) => Number.parseInt(part, 10));
|
|
86
|
+
for (let line = from; line <= to; line++) lines.add(line);
|
|
87
|
+
}
|
|
88
|
+
const numbers = /:(no-)?line-numbers(?![\w-])/.exec(rest);
|
|
89
|
+
return {
|
|
90
|
+
...language ? { language } : {},
|
|
91
|
+
meta,
|
|
92
|
+
...title !== void 0 ? { title } : {},
|
|
93
|
+
lines: [...lines].sort((a, b) => a - b),
|
|
94
|
+
...numbers ? { lineNumbers: numbers[1] === void 0 } : {}
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
var MAPPING_KEY = /^(?:[^\s#:'"\-?,[\]{}&*!|>%@`][^:\n]*?|"[^"\n]*"|'[^'\n]*')[ \t]*:(?:[ \t]|$)/;
|
|
98
|
+
/**
|
|
99
|
+
* Splits YAML front matter from the start of a document.
|
|
100
|
+
* @contract Front matter opens with a `---` line at the very start and closes at a `---` or `...`
|
|
101
|
+
* line, and what lies between is a YAML mapping: its first line that is not blank or a comment
|
|
102
|
+
* starts with a key, whose value may be a scalar, a list or a mapping. `source` is the block as
|
|
103
|
+
* written, delimiters included.
|
|
104
|
+
* @failure Returns null when the document has none.
|
|
105
|
+
*/
|
|
106
|
+
function splitLoomaFrontmatter(document) {
|
|
107
|
+
const match = /^---[ \t]*\r?\n([\s\S]*?)\r?\n(?:---|\.\.\.)[ \t]*(?:\r?\n|$)/.exec(document.slice(0, 8192));
|
|
108
|
+
const first = match?.[1].split(/\r?\n/).find((line) => line.trim() && !line.trimStart().startsWith("#"));
|
|
109
|
+
if (!match || !first || !MAPPING_KEY.test(first)) return null;
|
|
110
|
+
return {
|
|
111
|
+
source: match[0].trimEnd(),
|
|
112
|
+
yaml: match[1],
|
|
113
|
+
body: document.slice(match[0].length)
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
//#endregion
|
|
117
|
+
//#region src/editor/content.ts
|
|
118
|
+
/**
|
|
119
|
+
* A link destination the editor may store: a site-relative path, fragment, or query, or an
|
|
120
|
+
* absolute http, https, mailto, or tel URL.
|
|
121
|
+
*
|
|
122
|
+
* @failure Returns null for empty input, control characters, other schemes, and unparseable URLs.
|
|
123
|
+
*/
|
|
124
|
+
function validLinkHref(value) {
|
|
125
|
+
const href = value.trim();
|
|
126
|
+
if (!href || /[\u0000-\u001f\u007f]/.test(href)) return null;
|
|
127
|
+
if (/^(\/|#|\?)/.test(href)) return href;
|
|
128
|
+
try {
|
|
129
|
+
const url = new URL(href);
|
|
130
|
+
return [
|
|
131
|
+
"http:",
|
|
132
|
+
"https:",
|
|
133
|
+
"mailto:",
|
|
134
|
+
"tel:"
|
|
135
|
+
].includes(url.protocol) ? href : null;
|
|
136
|
+
} catch {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
//#endregion
|
|
141
|
+
//#region src/editor/reference-display.ts
|
|
142
|
+
var referenceDisplayKey = new PluginKey("loomaReferenceDisplay");
|
|
143
|
+
var referenceDisplayMeta = "loomaReferenceDisplay";
|
|
144
|
+
/**
|
|
145
|
+
* Replaces presentation without editing the document or adding an undo/history entry.
|
|
146
|
+
* @lifecycle Values last until the next replacement or editor destruction; hosts own persistence.
|
|
147
|
+
*/
|
|
148
|
+
function setReferenceDisplay(editor, references) {
|
|
149
|
+
if (editor.isDestroyed) return;
|
|
150
|
+
editor.storage.loomaReferencePresentation?.enableMentions?.();
|
|
151
|
+
editor.view.dispatch(editor.state.tr.setMeta(referenceDisplayMeta, references).setMeta("addToHistory", false));
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* The current replaceable presentation; absent entries use the saved fallback.
|
|
155
|
+
* @ownership The returned map belongs to the host; replace it rather than mutating it.
|
|
156
|
+
*/
|
|
157
|
+
function getReferenceDisplay(editor) {
|
|
158
|
+
return editor.storage.loomaReferencePresentation?.references ?? {};
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Reads current host presentation, with a durable insertion-time fallback for offline copies.
|
|
162
|
+
* @failure Unsafe cached destinations fall back to the saved safe URL or no destination.
|
|
163
|
+
*/
|
|
164
|
+
function referenceDisplay(node, references = {}) {
|
|
165
|
+
const kind = node.type.name === "mention" ? "mention" : node.attrs.kind;
|
|
166
|
+
const display = references[`${kind}:${node.attrs.id}`];
|
|
167
|
+
const label = typeof display?.label === "string" ? display.label : typeof node.attrs.label === "string" ? node.attrs.label : String(node.attrs.id ?? "Reference");
|
|
168
|
+
const href = typeof display?.href === "string" ? validLinkHref(display.href) : null;
|
|
169
|
+
return {
|
|
170
|
+
label,
|
|
171
|
+
...kind === "link" ? { href: href ?? (typeof node.attrs.href === "string" ? validLinkHref(node.attrs.href) : null) ?? void 0 } : {}
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* An automatically labelled link; custom wording remains an ordinary text link.
|
|
176
|
+
* @contract Identity and insertion-time fallback stay in JSON; cached presentation stays outside it.
|
|
177
|
+
*/
|
|
178
|
+
var LoomaReference = Node.create({
|
|
179
|
+
name: "loomaReference",
|
|
180
|
+
inline: true,
|
|
181
|
+
group: "inline",
|
|
182
|
+
atom: true,
|
|
183
|
+
addAttributes: () => ({
|
|
184
|
+
id: {
|
|
185
|
+
default: null,
|
|
186
|
+
renderHTML: () => ({})
|
|
187
|
+
},
|
|
188
|
+
kind: {
|
|
189
|
+
default: "link",
|
|
190
|
+
renderHTML: () => ({})
|
|
191
|
+
},
|
|
192
|
+
href: {
|
|
193
|
+
default: null,
|
|
194
|
+
renderHTML: () => ({})
|
|
195
|
+
},
|
|
196
|
+
label: {
|
|
197
|
+
default: null,
|
|
198
|
+
renderHTML: () => ({})
|
|
199
|
+
},
|
|
200
|
+
target: {
|
|
201
|
+
default: "_self",
|
|
202
|
+
renderHTML: () => ({})
|
|
203
|
+
}
|
|
204
|
+
}),
|
|
205
|
+
parseHTML: () => [{
|
|
206
|
+
tag: "a[data-looma-reference]",
|
|
207
|
+
priority: 1001,
|
|
208
|
+
getAttrs: (element) => ({
|
|
209
|
+
id: element.getAttribute("data-looma-reference"),
|
|
210
|
+
kind: "link",
|
|
211
|
+
href: element.getAttribute("href"),
|
|
212
|
+
label: element.textContent,
|
|
213
|
+
target: element.getAttribute("target") ?? "_self"
|
|
214
|
+
})
|
|
215
|
+
}],
|
|
216
|
+
renderHTML({ node, HTMLAttributes }) {
|
|
217
|
+
const display = referenceDisplay(node, this.editor ? getReferenceDisplay(this.editor) : {});
|
|
218
|
+
return [
|
|
219
|
+
"a",
|
|
220
|
+
mergeAttributes(HTMLAttributes, {
|
|
221
|
+
"data-looma-reference": node.attrs.id,
|
|
222
|
+
href: display.href,
|
|
223
|
+
target: node.attrs.target === "_blank" ? "_blank" : "_self",
|
|
224
|
+
rel: "noopener noreferrer"
|
|
225
|
+
}),
|
|
226
|
+
display.label
|
|
227
|
+
];
|
|
228
|
+
},
|
|
229
|
+
renderText({ node }) {
|
|
230
|
+
return referenceDisplay(node, this.editor ? getReferenceDisplay(this.editor) : {}).label;
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
/**
|
|
234
|
+
* Installs metadata-backed node views for references and existing stable-id mentions.
|
|
235
|
+
* @lifecycle Node views register until destroyed, including across editor reattachment.
|
|
236
|
+
*/
|
|
237
|
+
var LoomaReferencePresentation = Extension.create({
|
|
238
|
+
name: "loomaReferencePresentation",
|
|
239
|
+
addOptions: () => ({
|
|
240
|
+
references: {},
|
|
241
|
+
mentions: false
|
|
242
|
+
}),
|
|
243
|
+
addStorage() {
|
|
244
|
+
return { references: this.options.references };
|
|
245
|
+
},
|
|
246
|
+
addProseMirrorPlugins() {
|
|
247
|
+
const initial = this.options.references;
|
|
248
|
+
const refresh = /* @__PURE__ */ new Set();
|
|
249
|
+
const nodeView = (node, view, getPosition) => {
|
|
250
|
+
let current = node;
|
|
251
|
+
const mention = node.type.name === "mention";
|
|
252
|
+
const dom = document.createElement(mention ? "span" : "a");
|
|
253
|
+
if (mention) {
|
|
254
|
+
dom.className = "looma-mention";
|
|
255
|
+
dom.setAttribute("data-type", "mention");
|
|
256
|
+
} else dom.addEventListener("click", (event) => {
|
|
257
|
+
if (!(event instanceof MouseEvent) || !view.editable || event.metaKey || event.ctrlKey) return;
|
|
258
|
+
const position = getPosition();
|
|
259
|
+
if (position === void 0) return;
|
|
260
|
+
event.preventDefault();
|
|
261
|
+
view.dispatch(view.state.tr.setSelection(NodeSelection.create(view.state.doc, position)));
|
|
262
|
+
view.focus();
|
|
263
|
+
});
|
|
264
|
+
const render = () => {
|
|
265
|
+
const display = referenceDisplay(current, referenceDisplayKey.getState(view.state));
|
|
266
|
+
dom.textContent = `${mention ? "@" : ""}${display.label}`;
|
|
267
|
+
if (mention) {
|
|
268
|
+
dom.setAttribute("data-id", current.attrs.id ?? "");
|
|
269
|
+
dom.setAttribute("data-label", display.label);
|
|
270
|
+
}
|
|
271
|
+
if (!mention) {
|
|
272
|
+
dom.setAttribute("data-looma-reference", current.attrs.id ?? "");
|
|
273
|
+
if (display.href) dom.setAttribute("href", display.href);
|
|
274
|
+
else dom.removeAttribute("href");
|
|
275
|
+
dom.setAttribute("target", current.attrs.target === "_blank" ? "_blank" : "_self");
|
|
276
|
+
dom.setAttribute("rel", "noopener noreferrer");
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
refresh.add(render);
|
|
280
|
+
render();
|
|
281
|
+
return {
|
|
282
|
+
dom,
|
|
283
|
+
update(next) {
|
|
284
|
+
if (next.type !== current.type) return false;
|
|
285
|
+
current = next;
|
|
286
|
+
render();
|
|
287
|
+
return true;
|
|
288
|
+
},
|
|
289
|
+
destroy: () => {
|
|
290
|
+
refresh.delete(render);
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
};
|
|
294
|
+
const nodeViews = {
|
|
295
|
+
loomaReference: nodeView,
|
|
296
|
+
...this.options.mentions && this.editor.schema.nodes.mention ? { mention: nodeView } : {}
|
|
297
|
+
};
|
|
298
|
+
this.storage.enableMentions = () => {
|
|
299
|
+
if (nodeViews.mention || !this.editor.schema.nodes.mention) return;
|
|
300
|
+
nodeViews.mention = nodeView;
|
|
301
|
+
this.editor.view.setProps({ nodeViews: { ...this.editor.view.props.nodeViews } });
|
|
302
|
+
};
|
|
303
|
+
return [new Plugin({
|
|
304
|
+
key: referenceDisplayKey,
|
|
305
|
+
state: {
|
|
306
|
+
init: () => initial,
|
|
307
|
+
apply: (transaction, previous) => {
|
|
308
|
+
const next = transaction.getMeta(referenceDisplayMeta) ?? previous;
|
|
309
|
+
this.storage.references = next;
|
|
310
|
+
return next;
|
|
311
|
+
}
|
|
312
|
+
},
|
|
313
|
+
props: { nodeViews },
|
|
314
|
+
view: () => ({ update: (view, previous) => {
|
|
315
|
+
if (referenceDisplayKey.getState(view.state) !== referenceDisplayKey.getState(previous)) refresh.forEach((render) => render());
|
|
316
|
+
} })
|
|
317
|
+
})];
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
//#endregion
|
|
321
|
+
//#region src/editor/expressmark.ts
|
|
322
|
+
/**
|
|
323
|
+
* A conversion cannot faithfully represent the named input at this document location.
|
|
324
|
+
* @failure Callers retain the original document and can report the unsupported feature and path.
|
|
325
|
+
*/
|
|
326
|
+
var EditorMarkdownConversionError = class extends Error {
|
|
327
|
+
path;
|
|
328
|
+
feature;
|
|
329
|
+
constructor(path, feature) {
|
|
330
|
+
super(`Cannot convert ${feature} at ${path} without losing document data.`);
|
|
331
|
+
this.path = path;
|
|
332
|
+
this.feature = feature;
|
|
333
|
+
this.name = "EditorMarkdownConversionError";
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
var fail = (path, feature) => {
|
|
337
|
+
throw new EditorMarkdownConversionError(path, feature);
|
|
338
|
+
};
|
|
339
|
+
var fields = (node, names, path) => {
|
|
340
|
+
for (const [name, value] of Object.entries(node.attrs ?? {})) if (value !== null && value !== void 0 && !names.includes(name)) fail(path, `attribute ${name}`);
|
|
341
|
+
};
|
|
342
|
+
var string = (value, fallback = "") => typeof value === "string" ? value : fallback;
|
|
343
|
+
var title = (value) => ({
|
|
344
|
+
type: "title",
|
|
345
|
+
value
|
|
346
|
+
});
|
|
347
|
+
var content = (node) => node.body ?? node.children ?? (node.value !== void 0 ? [{
|
|
348
|
+
type: "text",
|
|
349
|
+
value: node.value
|
|
350
|
+
}] : []);
|
|
351
|
+
var text = (node) => node.value ?? content(node).map(text).join("");
|
|
352
|
+
var plainTitle = (node, path) => {
|
|
353
|
+
if (node === void 0) return "";
|
|
354
|
+
if (typeof node === "string" || node.type !== "title" || node.id || node.attributes || node.body || node.children?.some((child) => child.type !== "text" || child.id || child.attributes)) fail(path, "formatted or attributed title");
|
|
355
|
+
return text(node);
|
|
356
|
+
};
|
|
357
|
+
var AST_FIELDS = {
|
|
358
|
+
text: [],
|
|
359
|
+
paragraph: [],
|
|
360
|
+
heading: ["level"],
|
|
361
|
+
break: [],
|
|
362
|
+
softBreak: [],
|
|
363
|
+
horizontalRule: [],
|
|
364
|
+
blockquote: [],
|
|
365
|
+
list: [
|
|
366
|
+
"ordered",
|
|
367
|
+
"start",
|
|
368
|
+
"tight"
|
|
369
|
+
],
|
|
370
|
+
listItem: ["checked"],
|
|
371
|
+
code: [
|
|
372
|
+
"language",
|
|
373
|
+
"meta",
|
|
374
|
+
"title"
|
|
375
|
+
],
|
|
376
|
+
image: [
|
|
377
|
+
"url",
|
|
378
|
+
"alt",
|
|
379
|
+
"title"
|
|
380
|
+
],
|
|
381
|
+
inline: ["subType", "ref"],
|
|
382
|
+
container: [
|
|
383
|
+
"subType",
|
|
384
|
+
"title",
|
|
385
|
+
"spelling",
|
|
386
|
+
"titleSpelling"
|
|
387
|
+
],
|
|
388
|
+
leaf: ["subType"],
|
|
389
|
+
table: ["align", "title"],
|
|
390
|
+
tableRow: [],
|
|
391
|
+
tableCell: [
|
|
392
|
+
"header",
|
|
393
|
+
"colSpan",
|
|
394
|
+
"rowSpan"
|
|
395
|
+
],
|
|
396
|
+
strong: [],
|
|
397
|
+
emphasis: [],
|
|
398
|
+
delete: [],
|
|
399
|
+
underline: [],
|
|
400
|
+
highlight: [],
|
|
401
|
+
inlineCode: [],
|
|
402
|
+
link: ["url", "title"],
|
|
403
|
+
linkReference: ["label", "referenceType"],
|
|
404
|
+
footnoteReference: ["label"],
|
|
405
|
+
footnoteDefinition: ["label"],
|
|
406
|
+
definition: [
|
|
407
|
+
"label",
|
|
408
|
+
"url",
|
|
409
|
+
"title"
|
|
410
|
+
]
|
|
411
|
+
};
|
|
412
|
+
var referenceKey = (label) => label.trim().replace(/\s+/g, " ").toLowerCase().toUpperCase();
|
|
413
|
+
var LINK_DEFAULTS = {
|
|
414
|
+
target: "_blank",
|
|
415
|
+
rel: "noopener noreferrer"
|
|
416
|
+
};
|
|
417
|
+
var ALIGN = {
|
|
418
|
+
start: "left",
|
|
419
|
+
center: "center",
|
|
420
|
+
end: "right",
|
|
421
|
+
left: "left",
|
|
422
|
+
right: "right"
|
|
423
|
+
};
|
|
424
|
+
var has = (attrs, key, value) => {
|
|
425
|
+
if (typeof value === "string" || typeof value === "boolean") attrs[key] = value;
|
|
426
|
+
else if (typeof value === "number" && Number.isFinite(value)) attrs[key] = String(value);
|
|
427
|
+
};
|
|
428
|
+
var compact = (type, children, extra = {}) => children.length === 0 || children.every((node) => node.type === "text" && Object.keys(node).every((key) => key === "type" || key === "value")) ? {
|
|
429
|
+
type,
|
|
430
|
+
...extra,
|
|
431
|
+
value: children.map((node) => node.value).join("")
|
|
432
|
+
} : {
|
|
433
|
+
type,
|
|
434
|
+
...extra,
|
|
435
|
+
children
|
|
436
|
+
};
|
|
437
|
+
function findNode(document, predicate) {
|
|
438
|
+
if (predicate(document)) return document;
|
|
439
|
+
for (const child of document.body ?? document.children ?? []) {
|
|
440
|
+
const found = findNode(child, predicate);
|
|
441
|
+
if (found) return found;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
function safeBackground(value, path) {
|
|
445
|
+
if (typeof value !== "string") fail(path, "nonstring table background");
|
|
446
|
+
if (value.includes(";")) fail(path, "table background containing declarations");
|
|
447
|
+
const style = `background-color:${value}`;
|
|
448
|
+
const accepted = findNode(parse(stringify({
|
|
449
|
+
type: "document",
|
|
450
|
+
version: 1,
|
|
451
|
+
body: [{
|
|
452
|
+
type: "paragraph",
|
|
453
|
+
value: "color",
|
|
454
|
+
attributes: { style }
|
|
455
|
+
}]
|
|
456
|
+
}), { syntax: "compatible" }), (node) => typeof node.attributes?.style === "string")?.attributes?.style;
|
|
457
|
+
if (typeof accepted !== "string" || accepted.replace(/\s/g, "").replace(/;$/, "") !== style.replace(/\s/g, "")) fail(path, "unsafe or unsupported table background");
|
|
458
|
+
}
|
|
459
|
+
function safeNavigation(value, image, path) {
|
|
460
|
+
if (typeof value !== "string") fail(path, "nonstring navigation URL");
|
|
461
|
+
if (findNode(parse(stringify({
|
|
462
|
+
type: "document",
|
|
463
|
+
version: 1,
|
|
464
|
+
body: [{
|
|
465
|
+
type: "paragraph",
|
|
466
|
+
children: [image ? {
|
|
467
|
+
type: "image",
|
|
468
|
+
url: value,
|
|
469
|
+
alt: "image"
|
|
470
|
+
} : {
|
|
471
|
+
type: "link",
|
|
472
|
+
url: value,
|
|
473
|
+
value: "link"
|
|
474
|
+
}]
|
|
475
|
+
}]
|
|
476
|
+
}), { syntax: "compatible" }), (child) => child.type === (image ? "image" : "link"))?.url !== value) fail(path, "unsafe or unsupported navigation URL");
|
|
477
|
+
}
|
|
478
|
+
var MARKS = {
|
|
479
|
+
bold: "strong",
|
|
480
|
+
italic: "emphasis",
|
|
481
|
+
strike: "delete",
|
|
482
|
+
underline: "underline",
|
|
483
|
+
highlight: "highlight",
|
|
484
|
+
code: "inlineCode",
|
|
485
|
+
link: "link"
|
|
486
|
+
};
|
|
487
|
+
function legacyCodePresentation(code, path) {
|
|
488
|
+
let remaining = code.meta ?? "";
|
|
489
|
+
let unknown = "";
|
|
490
|
+
let translated = false;
|
|
491
|
+
const attributes = { ...code.attributes };
|
|
492
|
+
const result = {
|
|
493
|
+
...code,
|
|
494
|
+
attributes
|
|
495
|
+
};
|
|
496
|
+
while (remaining) {
|
|
497
|
+
const spacing = /^[ \t]+/.exec(remaining)?.[0];
|
|
498
|
+
if (spacing) {
|
|
499
|
+
unknown += spacing;
|
|
500
|
+
remaining = remaining.slice(spacing.length);
|
|
501
|
+
continue;
|
|
502
|
+
}
|
|
503
|
+
const token = /^(?:title="([^"]*)"|\{(\d+(?:[ \t]*-[ \t]*\d+)?(?:[ \t]*,[ \t]*\d+(?:[ \t]*-[ \t]*\d+)?)*)\}|:(no-)?line-numbers)(?=$|[ \t:{])/.exec(remaining);
|
|
504
|
+
if (token) {
|
|
505
|
+
const key = token[1] !== void 0 ? "title" : token[2] !== void 0 ? "highlight" : "showLineNumbers";
|
|
506
|
+
const value = token[1] ?? (token[2] !== void 0 ? token[2].replace(/[ \t]/g, "") : token[3] === void 0);
|
|
507
|
+
const previous = key === "title" ? result.title : attributes[key];
|
|
508
|
+
if (previous !== void 0 && previous !== value) fail(path, `conflicting code ${key} metadata`);
|
|
509
|
+
if (key === "title") result.title = value;
|
|
510
|
+
else attributes[key] = value;
|
|
511
|
+
remaining = remaining.slice(token[0].length);
|
|
512
|
+
translated = true;
|
|
513
|
+
continue;
|
|
514
|
+
}
|
|
515
|
+
const opaque = /^(?:[^"' \t]+|"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*')+/.exec(remaining)?.[0] ?? remaining[0];
|
|
516
|
+
unknown += opaque;
|
|
517
|
+
remaining = remaining.slice(opaque.length);
|
|
518
|
+
}
|
|
519
|
+
if (!translated) return code;
|
|
520
|
+
delete result.meta;
|
|
521
|
+
if (unknown.trim()) result.meta = unknown.trim();
|
|
522
|
+
if (!Object.keys(attributes).length) delete result.attributes;
|
|
523
|
+
return result;
|
|
524
|
+
}
|
|
525
|
+
var REVERSE_MARKS = Object.fromEntries(Object.entries(MARKS).map(([left, right]) => [right, left]));
|
|
526
|
+
var displayText = (node, references) => {
|
|
527
|
+
if (node.type === "loomaReference") return references[`link:${node.attrs?.id}`]?.label ?? string(node.attrs?.label, string(node.attrs?.id));
|
|
528
|
+
if (node.type === "mention") return `@${references[`mention:${node.attrs?.id}`]?.label ?? string(node.attrs?.label, string(node.attrs?.id))}`;
|
|
529
|
+
return node.text ?? (node.content ?? []).map((child) => displayText(child, references)).join("");
|
|
530
|
+
};
|
|
531
|
+
function headingsOf(document, references) {
|
|
532
|
+
const headings = [];
|
|
533
|
+
const visit = (node) => {
|
|
534
|
+
if (node.type === "heading") headings.push({
|
|
535
|
+
node,
|
|
536
|
+
text: displayText(node, references).trim(),
|
|
537
|
+
id: node.attrs?.id ? string(node.attrs.id) : void 0
|
|
538
|
+
});
|
|
539
|
+
else if (node.type !== "loomaTableOfContents") for (const child of node.content ?? []) visit(child);
|
|
540
|
+
};
|
|
541
|
+
visit(document);
|
|
542
|
+
return headings;
|
|
543
|
+
}
|
|
544
|
+
function customHeadings(headings) {
|
|
545
|
+
const marked = /* @__PURE__ */ new Set();
|
|
546
|
+
for (;;) {
|
|
547
|
+
const derived = assignLoomaHeadingIds(headings.map(({ text, id }, index) => ({
|
|
548
|
+
text,
|
|
549
|
+
id: marked.has(index) ? id : void 0
|
|
550
|
+
})));
|
|
551
|
+
const next = headings.findIndex(({ id }, index) => id && !marked.has(index) && derived[index] !== id);
|
|
552
|
+
if (next < 0) return new Set([...marked].map((index) => headings[index].node));
|
|
553
|
+
marked.add(next);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
function toNode(node, path, context) {
|
|
557
|
+
let result = toBareNode(node, path, context);
|
|
558
|
+
if (!node.marks?.length) return result;
|
|
559
|
+
if (![
|
|
560
|
+
"text",
|
|
561
|
+
"mention",
|
|
562
|
+
"loomaReference",
|
|
563
|
+
"loomaChip",
|
|
564
|
+
"loomaFootnoteReference",
|
|
565
|
+
"hardBreak"
|
|
566
|
+
].includes(node.type ?? "")) fail(path, "marks on a block node");
|
|
567
|
+
for (const mark of [...node.marks ?? []].reverse()) {
|
|
568
|
+
const type = MARKS[mark.type];
|
|
569
|
+
if (!type) fail(path, `mark ${mark.type}`);
|
|
570
|
+
if (mark.type === "highlight" && mark.attrs?.color != null) fail(path, "colored highlight (the default editor uses one highlight style)");
|
|
571
|
+
fields({ attrs: mark.attrs }, mark.type === "link" ? [
|
|
572
|
+
"href",
|
|
573
|
+
"title",
|
|
574
|
+
"target",
|
|
575
|
+
"rel",
|
|
576
|
+
"class",
|
|
577
|
+
"reference",
|
|
578
|
+
"referenceType"
|
|
579
|
+
] : mark.type === "highlight" ? ["color"] : [], path);
|
|
580
|
+
if (type === "link") {
|
|
581
|
+
const attrs = mark.attrs ?? {};
|
|
582
|
+
const definition = typeof attrs.reference === "string" ? context.definitions.get(referenceKey(attrs.reference)) : void 0;
|
|
583
|
+
if (definition && definition.attrs?.href === attrs.href && string(definition.attrs?.title) === string(attrs.title) && !attrs.class) {
|
|
584
|
+
const referenceType = (attrs.referenceType === "collapsed" || attrs.referenceType === "shortcut") && referenceKey(text(result)) !== referenceKey(attrs.reference) ? "full" : string(attrs.referenceType, "full");
|
|
585
|
+
result = compact("linkReference", [result], {
|
|
586
|
+
label: attrs.reference,
|
|
587
|
+
referenceType
|
|
588
|
+
});
|
|
589
|
+
continue;
|
|
590
|
+
}
|
|
591
|
+
const attributes = {};
|
|
592
|
+
for (const key of [
|
|
593
|
+
"target",
|
|
594
|
+
"rel",
|
|
595
|
+
"class"
|
|
596
|
+
]) if (attrs[key] !== LINK_DEFAULTS[key]) has(attributes, key, attrs[key]);
|
|
597
|
+
result = compact(type, [result], {
|
|
598
|
+
url: string(attrs.href),
|
|
599
|
+
...attrs.title ? { title: string(attrs.title) } : {},
|
|
600
|
+
...Object.keys(attributes).length ? { attributes } : {}
|
|
601
|
+
});
|
|
602
|
+
} else if (type === "inlineCode") {
|
|
603
|
+
if (result.type !== "text") fail(path, "code combined with another mark");
|
|
604
|
+
result = {
|
|
605
|
+
type,
|
|
606
|
+
value: result.value ?? ""
|
|
607
|
+
};
|
|
608
|
+
} else result = compact(type, [result], mark.attrs?.color ? { attributes: { color: string(mark.attrs.color) } } : {});
|
|
609
|
+
}
|
|
610
|
+
return result;
|
|
611
|
+
}
|
|
612
|
+
var codeLabel = (code) => (typeof code.title === "string" ? code.title : void 0) ?? parseLoomaFenceInfo(code.meta ?? "").title ?? code.language ?? "Code";
|
|
613
|
+
function toBareNode(node, path, context) {
|
|
614
|
+
const attrs = node.attrs ?? {};
|
|
615
|
+
const children = () => (node.content ?? []).map((child, index) => toNode(child, `${path}.body[${index}]`, context));
|
|
616
|
+
const blocks = () => children().map((child) => child.type === "image" ? {
|
|
617
|
+
type: "paragraph",
|
|
618
|
+
children: [child]
|
|
619
|
+
} : child);
|
|
620
|
+
const plain = (type, keys = [], extra = {}) => {
|
|
621
|
+
fields(node, keys, path);
|
|
622
|
+
return compact(type, children(), extra);
|
|
623
|
+
};
|
|
624
|
+
const block = (type, keys = [], extra = {}) => {
|
|
625
|
+
fields(node, keys, path);
|
|
626
|
+
return {
|
|
627
|
+
type,
|
|
628
|
+
...extra,
|
|
629
|
+
body: blocks()
|
|
630
|
+
};
|
|
631
|
+
};
|
|
632
|
+
switch (node.type) {
|
|
633
|
+
case "loomaReference": {
|
|
634
|
+
fields(node, [
|
|
635
|
+
"id",
|
|
636
|
+
"kind",
|
|
637
|
+
"href",
|
|
638
|
+
"label",
|
|
639
|
+
"target"
|
|
640
|
+
], path);
|
|
641
|
+
if (!attrs.id || attrs.kind !== "link") fail(path, "invalid link reference identity");
|
|
642
|
+
const display = context.references[`link:${attrs.id}`];
|
|
643
|
+
const href = display?.href ?? string(attrs.href);
|
|
644
|
+
safeNavigation(href, false, path);
|
|
645
|
+
return {
|
|
646
|
+
type: "link",
|
|
647
|
+
url: href,
|
|
648
|
+
value: display?.label ?? string(attrs.label, string(attrs.id)),
|
|
649
|
+
attributes: {
|
|
650
|
+
ref: string(attrs.id),
|
|
651
|
+
target: string(attrs.target, "_self"),
|
|
652
|
+
rel: "noopener noreferrer"
|
|
653
|
+
}
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
case "text":
|
|
657
|
+
fields(node, [], path);
|
|
658
|
+
return {
|
|
659
|
+
type: "text",
|
|
660
|
+
value: node.text ?? ""
|
|
661
|
+
};
|
|
662
|
+
case "paragraph": return plain("paragraph");
|
|
663
|
+
case "heading": return plain("heading", ["level", "id"], {
|
|
664
|
+
level: Number(attrs.level ?? 1),
|
|
665
|
+
...context.custom.has(node) ? { id: string(attrs.id) } : {}
|
|
666
|
+
});
|
|
667
|
+
case "blockquote": return block("blockquote");
|
|
668
|
+
case "horizontalRule":
|
|
669
|
+
fields(node, [], path);
|
|
670
|
+
return { type: "horizontalRule" };
|
|
671
|
+
case "hardBreak":
|
|
672
|
+
fields(node, [], path);
|
|
673
|
+
return { type: "break" };
|
|
674
|
+
case "bulletList":
|
|
675
|
+
case "orderedList":
|
|
676
|
+
case "taskList":
|
|
677
|
+
fields(node, ["start"], path);
|
|
678
|
+
return {
|
|
679
|
+
type: "list",
|
|
680
|
+
ordered: node.type === "orderedList",
|
|
681
|
+
...node.type === "orderedList" ? { start: Number(attrs.start ?? 1) } : {},
|
|
682
|
+
tight: true,
|
|
683
|
+
children: children()
|
|
684
|
+
};
|
|
685
|
+
case "listItem": return block("listItem");
|
|
686
|
+
case "taskItem": return block("listItem", ["checked"], { checked: attrs.checked === true });
|
|
687
|
+
case "codeBlock": {
|
|
688
|
+
fields(node, ["language", "meta"], path);
|
|
689
|
+
if ((node.content ?? []).some((child) => child.type !== "text" || child.marks?.length)) fail(path, "nonliteral code content");
|
|
690
|
+
if (/[\r\n]/.test(string(attrs.meta)) || /[\s`~]/.test(string(attrs.language))) fail(path, "code fence information");
|
|
691
|
+
const parsed = parse(`\`\`\`${string(attrs.language)} ${string(attrs.meta)}\n\`\`\``).body[0];
|
|
692
|
+
if (parsed?.type !== "code") fail(path, "code fence information");
|
|
693
|
+
return {
|
|
694
|
+
...legacyCodePresentation(parsed, path),
|
|
695
|
+
value: (node.content ?? []).map((child) => child.text ?? "").join("")
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
case "image": {
|
|
699
|
+
fields(node, [
|
|
700
|
+
"src",
|
|
701
|
+
"alt",
|
|
702
|
+
"title",
|
|
703
|
+
"width",
|
|
704
|
+
"height",
|
|
705
|
+
"placement",
|
|
706
|
+
"responsive"
|
|
707
|
+
], path);
|
|
708
|
+
const attributes = {};
|
|
709
|
+
for (const key of [
|
|
710
|
+
"width",
|
|
711
|
+
"height",
|
|
712
|
+
"placement",
|
|
713
|
+
"responsive"
|
|
714
|
+
]) if (!(key === "placement" && attrs[key] === "block")) has(attributes, key, attrs[key]);
|
|
715
|
+
return {
|
|
716
|
+
type: "image",
|
|
717
|
+
url: string(attrs.src),
|
|
718
|
+
alt: string(attrs.alt),
|
|
719
|
+
...attrs.title ? { title: string(attrs.title) } : {},
|
|
720
|
+
...Object.keys(attributes).length ? { attributes } : {}
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
case "mention":
|
|
724
|
+
fields(node, ["id", "label"], path);
|
|
725
|
+
return {
|
|
726
|
+
type: "inline",
|
|
727
|
+
subType: "mention",
|
|
728
|
+
value: context.references[`mention:${attrs.id}`]?.label ?? string(attrs.label, string(attrs.id)),
|
|
729
|
+
...attrs.id ? { ref: string(attrs.id) } : {}
|
|
730
|
+
};
|
|
731
|
+
case "loomaChip":
|
|
732
|
+
fields(node, ["label", "color"], path);
|
|
733
|
+
return {
|
|
734
|
+
type: "inline",
|
|
735
|
+
subType: "chip",
|
|
736
|
+
value: string(attrs.label),
|
|
737
|
+
...string(attrs.color, "neutral") !== "neutral" ? { attributes: { tone: string(attrs.color) } } : {}
|
|
738
|
+
};
|
|
739
|
+
case "loomaFootnoteReference":
|
|
740
|
+
fields(node, [
|
|
741
|
+
"label",
|
|
742
|
+
"number",
|
|
743
|
+
"occurrence"
|
|
744
|
+
], path);
|
|
745
|
+
return {
|
|
746
|
+
type: "footnoteReference",
|
|
747
|
+
label: string(attrs.label)
|
|
748
|
+
};
|
|
749
|
+
case "loomaFootnote": {
|
|
750
|
+
const note = block("footnoteDefinition", [
|
|
751
|
+
"label",
|
|
752
|
+
"count",
|
|
753
|
+
"number"
|
|
754
|
+
], { label: string(attrs.label) });
|
|
755
|
+
return note.body.length === 1 && note.body[0].type === "paragraph" && !note.body[0].value && !note.body[0].children ? {
|
|
756
|
+
...note,
|
|
757
|
+
body: []
|
|
758
|
+
} : note;
|
|
759
|
+
}
|
|
760
|
+
case "loomaLinkDefinition":
|
|
761
|
+
fields(node, [
|
|
762
|
+
"label",
|
|
763
|
+
"href",
|
|
764
|
+
"title"
|
|
765
|
+
], path);
|
|
766
|
+
return {
|
|
767
|
+
type: "definition",
|
|
768
|
+
label: string(attrs.label),
|
|
769
|
+
url: string(attrs.href),
|
|
770
|
+
...attrs.title ? { title: string(attrs.title) } : {}
|
|
771
|
+
};
|
|
772
|
+
case "loomaCallout": return block("container", ["tone", "heading"], {
|
|
773
|
+
subType: string(attrs.tone, "note"),
|
|
774
|
+
...attrs.heading ? { title: title(string(attrs.heading)) } : {}
|
|
775
|
+
});
|
|
776
|
+
case "loomaExpand": return block("container", ["summary", "open"], {
|
|
777
|
+
subType: "details",
|
|
778
|
+
title: title(string(attrs.summary, "Details")),
|
|
779
|
+
...attrs.open === true ? { attributes: { open: true } } : {}
|
|
780
|
+
});
|
|
781
|
+
case "loomaTabs": {
|
|
782
|
+
fields(node, [
|
|
783
|
+
"group",
|
|
784
|
+
"codeGroup",
|
|
785
|
+
"defaultValue",
|
|
786
|
+
"queryString",
|
|
787
|
+
"lazy"
|
|
788
|
+
], path);
|
|
789
|
+
for (const key of [
|
|
790
|
+
"group",
|
|
791
|
+
"defaultValue",
|
|
792
|
+
"queryString"
|
|
793
|
+
]) if (attrs[key] !== null && attrs[key] !== void 0 && typeof attrs[key] !== "string") fail(path, `nonstring tab ${key}`);
|
|
794
|
+
if (attrs.lazy !== null && attrs.lazy !== void 0 && typeof attrs.lazy !== "boolean") fail(path, "nonboolean tab lazy flag");
|
|
795
|
+
const panels = blocks();
|
|
796
|
+
const code = panels.map((panel) => panel.body?.length === 1 && panel.body[0].type === "code" && !panel.attributes ? panel.body[0] : void 0);
|
|
797
|
+
if (attrs.codeGroup === true && attrs.defaultValue == null && attrs.queryString == null && attrs.lazy == null && code.every((fence, index) => fence && plainTitle(panels[index].title, path) === codeLabel(fence))) return {
|
|
798
|
+
type: "container",
|
|
799
|
+
subType: "code-group",
|
|
800
|
+
...attrs.group && attrs.group !== "code-group" ? { attributes: { group: string(attrs.group) } } : {},
|
|
801
|
+
body: code
|
|
802
|
+
};
|
|
803
|
+
const attributes = {};
|
|
804
|
+
for (const key of [
|
|
805
|
+
"group",
|
|
806
|
+
"queryString",
|
|
807
|
+
"lazy"
|
|
808
|
+
]) if (attrs[key] !== "") has(attributes, key, attrs[key]);
|
|
809
|
+
has(attributes, "default", attrs.defaultValue);
|
|
810
|
+
return {
|
|
811
|
+
type: "container",
|
|
812
|
+
subType: "tabs",
|
|
813
|
+
...Object.keys(attributes).length ? { attributes } : {},
|
|
814
|
+
body: panels
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
case "loomaTab": return block("container", ["label", "value"], {
|
|
818
|
+
subType: "tab",
|
|
819
|
+
title: title(string(attrs.label)),
|
|
820
|
+
...attrs.value !== void 0 && attrs.value !== null ? { attributes: { value: string(attrs.value) } } : {}
|
|
821
|
+
});
|
|
822
|
+
case "loomaFigure": {
|
|
823
|
+
fields(node, [], path);
|
|
824
|
+
const image = node.content?.[0];
|
|
825
|
+
const caption = node.content?.[1];
|
|
826
|
+
if (node.content?.length !== 2 || image?.type !== "image" || caption?.type !== "loomaFigcaption") fail(path, "figure shape");
|
|
827
|
+
fields(caption, [], `${path}.caption`);
|
|
828
|
+
return {
|
|
829
|
+
type: "container",
|
|
830
|
+
subType: "figure",
|
|
831
|
+
title: compact("title", (caption.content ?? []).map((child, index) => toNode(child, `${path}.title[${index}]`, context))),
|
|
832
|
+
body: [{
|
|
833
|
+
type: "paragraph",
|
|
834
|
+
children: [toNode(image, `${path}.body[0]`, context)]
|
|
835
|
+
}]
|
|
836
|
+
};
|
|
837
|
+
}
|
|
838
|
+
case "table": {
|
|
839
|
+
fields(node, [], path);
|
|
840
|
+
const rows = children();
|
|
841
|
+
const align = rows[0]?.children?.map((_cell, column) => rows.every((row) => row.children?.length === rows[0].children.length && row.children.every((cell) => !cell.colSpan && !cell.rowSpan) && row.children[column].attributes?.textAlign === rows[0].children[column].attributes?.textAlign) ? rows[0].children[column].attributes?.textAlign : false);
|
|
842
|
+
if (!align?.length || align.includes(false) || align.every((value) => value === void 0)) return {
|
|
843
|
+
type: "table",
|
|
844
|
+
children: rows
|
|
845
|
+
};
|
|
846
|
+
for (const row of rows) for (const cell of row.children ?? []) {
|
|
847
|
+
if (!cell.attributes) continue;
|
|
848
|
+
delete cell.attributes.textAlign;
|
|
849
|
+
if (!Object.keys(cell.attributes).length) delete cell.attributes;
|
|
850
|
+
}
|
|
851
|
+
return {
|
|
852
|
+
type: "table",
|
|
853
|
+
align: align.map((value) => value === "left" ? "start" : value === "right" ? "end" : value === "center" ? "center" : null),
|
|
854
|
+
children: rows
|
|
855
|
+
};
|
|
856
|
+
}
|
|
857
|
+
case "tableRow":
|
|
858
|
+
fields(node, [], path);
|
|
859
|
+
return {
|
|
860
|
+
type: "tableRow",
|
|
861
|
+
children: children()
|
|
862
|
+
};
|
|
863
|
+
case "tableCell":
|
|
864
|
+
case "tableHeader": {
|
|
865
|
+
fields(node, [
|
|
866
|
+
"colspan",
|
|
867
|
+
"rowspan",
|
|
868
|
+
"colwidth",
|
|
869
|
+
"textAlign",
|
|
870
|
+
"backgroundColor"
|
|
871
|
+
], path);
|
|
872
|
+
const attributes = {};
|
|
873
|
+
for (const key of ["textAlign", "backgroundColor"]) has(attributes, key, attrs[key]);
|
|
874
|
+
if (attrs.colwidth != null) {
|
|
875
|
+
if (!Array.isArray(attrs.colwidth) || attrs.colwidth.some((value) => typeof value !== "number" || !Number.isFinite(value))) fail(path, "column widths");
|
|
876
|
+
attributes.columnWidths = attrs.colwidth.join(",");
|
|
877
|
+
}
|
|
878
|
+
const colSpan = Number(attrs.colspan ?? 1);
|
|
879
|
+
const rowSpan = Number(attrs.rowspan ?? 1);
|
|
880
|
+
const body = blocks();
|
|
881
|
+
return {
|
|
882
|
+
...body.length === 1 && body[0].type === "paragraph" ? {
|
|
883
|
+
...body[0],
|
|
884
|
+
type: "tableCell"
|
|
885
|
+
} : {
|
|
886
|
+
type: "tableCell",
|
|
887
|
+
body
|
|
888
|
+
},
|
|
889
|
+
...node.type === "tableHeader" ? { header: true } : {},
|
|
890
|
+
...colSpan !== 1 ? { colSpan } : {},
|
|
891
|
+
...rowSpan !== 1 ? { rowSpan } : {},
|
|
892
|
+
...Object.keys(attributes).length ? { attributes } : {}
|
|
893
|
+
};
|
|
894
|
+
}
|
|
895
|
+
case "loomaTableOfContents": {
|
|
896
|
+
fields(node, ["format", "depth"], path);
|
|
897
|
+
const depth = Number(attrs.depth ?? 3);
|
|
898
|
+
const format = string(attrs.format, "plain");
|
|
899
|
+
if (!node.content?.length) {
|
|
900
|
+
const attributes = {};
|
|
901
|
+
if (depth !== 3) attributes.depth = String(depth);
|
|
902
|
+
if (format !== "plain") attributes.format = format;
|
|
903
|
+
return {
|
|
904
|
+
type: "leaf",
|
|
905
|
+
subType: "toc",
|
|
906
|
+
...Object.keys(attributes).length ? { attributes } : {}
|
|
907
|
+
};
|
|
908
|
+
}
|
|
909
|
+
return {
|
|
910
|
+
type: "container",
|
|
911
|
+
subType: "toc",
|
|
912
|
+
attributes: {
|
|
913
|
+
depth: String(depth),
|
|
914
|
+
format
|
|
915
|
+
},
|
|
916
|
+
body: blocks()
|
|
917
|
+
};
|
|
918
|
+
}
|
|
919
|
+
default: return fail(path, `node ${node.type ?? "unknown"}`);
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
/**
|
|
923
|
+
* Maps an editor's saved JSON to the independent Expressive Markdown AST without DOM access.
|
|
924
|
+
* @contract Unsupported nodes, marks and durable attributes throw with their location; input is never
|
|
925
|
+
* mutated. Footnote and link definitions stay where they are, and a table of contents stays a
|
|
926
|
+
* placeholder unless it holds authored navigation.
|
|
927
|
+
*/
|
|
928
|
+
function editorDocumentToExpressmark(document, options = {}) {
|
|
929
|
+
if (document.type !== "doc") fail("document", "non-document root");
|
|
930
|
+
fields(document, [], "document");
|
|
931
|
+
const colors = /* @__PURE__ */ new Set();
|
|
932
|
+
const urls = /* @__PURE__ */ new Set();
|
|
933
|
+
const definitions = /* @__PURE__ */ new Map();
|
|
934
|
+
const visit = (node) => {
|
|
935
|
+
const attrs = node.attrs ?? {};
|
|
936
|
+
if (node.type === "tableCell" || node.type === "tableHeader") {
|
|
937
|
+
if (attrs.backgroundColor != null && !colors.has(attrs.backgroundColor)) {
|
|
938
|
+
safeBackground(attrs.backgroundColor, "document");
|
|
939
|
+
colors.add(attrs.backgroundColor);
|
|
940
|
+
}
|
|
941
|
+
if (attrs.textAlign != null && ![
|
|
942
|
+
"left",
|
|
943
|
+
"center",
|
|
944
|
+
"right"
|
|
945
|
+
].includes(String(attrs.textAlign))) fail("document", "unsupported table alignment");
|
|
946
|
+
}
|
|
947
|
+
if (node.type === "image") {
|
|
948
|
+
const key = `image:${attrs.src}`;
|
|
949
|
+
if (!urls.has(key)) {
|
|
950
|
+
safeNavigation(attrs.src, true, "document");
|
|
951
|
+
urls.add(key);
|
|
952
|
+
}
|
|
953
|
+
if (attrs.placement != null && ![
|
|
954
|
+
"block",
|
|
955
|
+
"center",
|
|
956
|
+
"wrap-left",
|
|
957
|
+
"wrap-right"
|
|
958
|
+
].includes(String(attrs.placement))) fail("document", "unsupported image placement");
|
|
959
|
+
if (attrs.responsive != null && typeof attrs.responsive !== "boolean") fail("document", "nonboolean responsive flag");
|
|
960
|
+
}
|
|
961
|
+
if (node.type === "loomaLinkDefinition") {
|
|
962
|
+
const key = `link:${attrs.href}`;
|
|
963
|
+
if (!urls.has(key)) {
|
|
964
|
+
safeNavigation(attrs.href, false, "document");
|
|
965
|
+
urls.add(key);
|
|
966
|
+
}
|
|
967
|
+
if (!definitions.has(referenceKey(string(attrs.label)))) definitions.set(referenceKey(string(attrs.label)), node);
|
|
968
|
+
}
|
|
969
|
+
for (const mark of node.marks ?? []) if (mark.type === "link") {
|
|
970
|
+
const key = `link:${mark.attrs?.href}`;
|
|
971
|
+
if (!urls.has(key)) {
|
|
972
|
+
safeNavigation(mark.attrs?.href, false, "document");
|
|
973
|
+
urls.add(key);
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
for (const child of node.content ?? []) visit(child);
|
|
977
|
+
};
|
|
978
|
+
visit(document);
|
|
979
|
+
const references = options.references ?? {};
|
|
980
|
+
const headings = headingsOf(document, references);
|
|
981
|
+
const authored = /* @__PURE__ */ new Set();
|
|
982
|
+
for (const { id } of headings) {
|
|
983
|
+
if (id && authored.has(id)) options.onDiagnostic?.({
|
|
984
|
+
code: "duplicate-id",
|
|
985
|
+
severity: "warning",
|
|
986
|
+
message: `Authored heading ID "${id}" is repeated and was preserved.`
|
|
987
|
+
});
|
|
988
|
+
if (id) authored.add(id);
|
|
989
|
+
}
|
|
990
|
+
const context = {
|
|
991
|
+
custom: customHeadings(headings),
|
|
992
|
+
definitions,
|
|
993
|
+
references
|
|
994
|
+
};
|
|
995
|
+
return {
|
|
996
|
+
type: "document",
|
|
997
|
+
version: 1,
|
|
998
|
+
syntax: options.syntax ?? "compatible",
|
|
999
|
+
body: (document.content ?? []).map((node, index) => {
|
|
1000
|
+
const child = toNode(node, `document.body[${index}]`, context);
|
|
1001
|
+
return child.type === "image" ? {
|
|
1002
|
+
type: "paragraph",
|
|
1003
|
+
children: [child]
|
|
1004
|
+
} : child;
|
|
1005
|
+
})
|
|
1006
|
+
};
|
|
1007
|
+
}
|
|
1008
|
+
function fromNode(node, path, context, marks = []) {
|
|
1009
|
+
const attrs = node.attributes ?? {};
|
|
1010
|
+
const allowed = /* @__PURE__ */ new Set([
|
|
1011
|
+
"type",
|
|
1012
|
+
"id",
|
|
1013
|
+
"attributes",
|
|
1014
|
+
"body",
|
|
1015
|
+
"children",
|
|
1016
|
+
"value",
|
|
1017
|
+
"position",
|
|
1018
|
+
"source",
|
|
1019
|
+
...AST_FIELDS[node.type] ?? []
|
|
1020
|
+
]);
|
|
1021
|
+
for (const key of Object.keys(node)) if (!allowed.has(key)) fail(path, `AST field ${key}`);
|
|
1022
|
+
if ([
|
|
1023
|
+
node.body,
|
|
1024
|
+
node.children,
|
|
1025
|
+
node.value
|
|
1026
|
+
].filter((value) => value !== void 0).length > 1) fail(path, "conflicting content fields");
|
|
1027
|
+
const check = (keys = []) => {
|
|
1028
|
+
for (const key of Object.keys(attrs)) if (!keys.includes(key)) fail(path, `attribute ${key}`);
|
|
1029
|
+
if (node.id && node.type !== "heading") fail(path, `ID on ${node.type}`);
|
|
1030
|
+
};
|
|
1031
|
+
const children = () => content(node).flatMap((child, index) => fromNode(child, `${path}.body[${index}]`, context));
|
|
1032
|
+
const inline = () => content(node).flatMap((child, index) => fromNode(child, `${path}.children[${index}]`, context, marks));
|
|
1033
|
+
const wrap = (type, attributes) => [{
|
|
1034
|
+
type,
|
|
1035
|
+
...attributes ? { attrs: attributes } : {},
|
|
1036
|
+
...content(node).length ? { content: children() } : {}
|
|
1037
|
+
}];
|
|
1038
|
+
if (node.type === "linkReference") {
|
|
1039
|
+
check();
|
|
1040
|
+
const definition = context.definitions.get(referenceKey(node.label ?? ""));
|
|
1041
|
+
if (!definition) fail(path, "link reference without a definition");
|
|
1042
|
+
const mark = {
|
|
1043
|
+
type: "link",
|
|
1044
|
+
attrs: {
|
|
1045
|
+
href: definition.url ?? "",
|
|
1046
|
+
...typeof definition.title === "string" ? { title: definition.title } : {},
|
|
1047
|
+
reference: node.label,
|
|
1048
|
+
referenceType: node.referenceType ?? "full"
|
|
1049
|
+
}
|
|
1050
|
+
};
|
|
1051
|
+
return content(node).flatMap((child, index) => fromNode(child, `${path}.children[${index}]`, context, [...marks, mark]));
|
|
1052
|
+
}
|
|
1053
|
+
if (REVERSE_MARKS[node.type]) {
|
|
1054
|
+
check(node.type === "link" ? [
|
|
1055
|
+
"target",
|
|
1056
|
+
"rel",
|
|
1057
|
+
"class",
|
|
1058
|
+
"ref"
|
|
1059
|
+
] : []);
|
|
1060
|
+
if (node.type === "link" && attrs.ref !== void 0) {
|
|
1061
|
+
if (typeof attrs.ref !== "string" || !attrs.ref || node.title || attrs.class || content(node).some((child) => child.type !== "text" || child.attributes || child.id)) fail(path, "formatted or invalid link reference");
|
|
1062
|
+
return [{
|
|
1063
|
+
type: "loomaReference",
|
|
1064
|
+
attrs: {
|
|
1065
|
+
id: attrs.ref,
|
|
1066
|
+
kind: "link",
|
|
1067
|
+
href: node.url ?? "",
|
|
1068
|
+
label: text(node),
|
|
1069
|
+
target: attrs.target ?? "_self"
|
|
1070
|
+
},
|
|
1071
|
+
...marks.length ? { marks } : {}
|
|
1072
|
+
}];
|
|
1073
|
+
}
|
|
1074
|
+
const mark = { type: REVERSE_MARKS[node.type] };
|
|
1075
|
+
if (node.type === "link") mark.attrs = {
|
|
1076
|
+
href: node.url ?? "",
|
|
1077
|
+
...attrs,
|
|
1078
|
+
...typeof node.title === "string" ? { title: node.title } : {}
|
|
1079
|
+
};
|
|
1080
|
+
else if (Object.keys(attrs).length) mark.attrs = { ...attrs };
|
|
1081
|
+
return content(node).flatMap((child, index) => fromNode(child, `${path}.children[${index}]`, context, [...marks, mark]));
|
|
1082
|
+
}
|
|
1083
|
+
switch (node.type) {
|
|
1084
|
+
case "text":
|
|
1085
|
+
check();
|
|
1086
|
+
return node.value ? [{
|
|
1087
|
+
type: "text",
|
|
1088
|
+
text: node.value,
|
|
1089
|
+
...marks.length ? { marks } : {}
|
|
1090
|
+
}] : [];
|
|
1091
|
+
case "paragraph": {
|
|
1092
|
+
check();
|
|
1093
|
+
const items = content(node);
|
|
1094
|
+
if (items.length === 1 && items[0]?.type === "image") return fromNode(items[0], `${path}.children[0]`, context);
|
|
1095
|
+
if (items.some((child) => child.type === "image")) fail(path, "image mixed with inline text (the default editor uses block images)");
|
|
1096
|
+
return [{
|
|
1097
|
+
type: "paragraph",
|
|
1098
|
+
...items.length ? { content: inline() } : {}
|
|
1099
|
+
}];
|
|
1100
|
+
}
|
|
1101
|
+
case "heading":
|
|
1102
|
+
check();
|
|
1103
|
+
return [{
|
|
1104
|
+
type: "heading",
|
|
1105
|
+
attrs: {
|
|
1106
|
+
level: node.level ?? 1,
|
|
1107
|
+
...node.id ? { id: node.id } : {}
|
|
1108
|
+
},
|
|
1109
|
+
...content(node).length ? { content: inline() } : {}
|
|
1110
|
+
}];
|
|
1111
|
+
case "break":
|
|
1112
|
+
check();
|
|
1113
|
+
return [{
|
|
1114
|
+
type: "hardBreak",
|
|
1115
|
+
...marks.length ? { marks } : {}
|
|
1116
|
+
}];
|
|
1117
|
+
case "softBreak":
|
|
1118
|
+
check();
|
|
1119
|
+
return [{
|
|
1120
|
+
type: "text",
|
|
1121
|
+
text: "\n",
|
|
1122
|
+
...marks.length ? { marks } : {}
|
|
1123
|
+
}];
|
|
1124
|
+
case "horizontalRule":
|
|
1125
|
+
check();
|
|
1126
|
+
return [{ type: "horizontalRule" }];
|
|
1127
|
+
case "blockquote":
|
|
1128
|
+
check();
|
|
1129
|
+
return wrap("blockquote");
|
|
1130
|
+
case "list":
|
|
1131
|
+
check();
|
|
1132
|
+
return [{
|
|
1133
|
+
type: node.children?.some((child) => child.checked !== void 0) ? "taskList" : node.ordered ? "orderedList" : "bulletList",
|
|
1134
|
+
...node.ordered ? { attrs: { start: node.start ?? 1 } } : {},
|
|
1135
|
+
content: children()
|
|
1136
|
+
}];
|
|
1137
|
+
case "listItem":
|
|
1138
|
+
check();
|
|
1139
|
+
return wrap(node.checked !== void 0 ? "taskItem" : "listItem", node.checked !== void 0 ? { checked: node.checked } : void 0);
|
|
1140
|
+
case "code": {
|
|
1141
|
+
check(Object.keys(attrs));
|
|
1142
|
+
const meta = (stringify({
|
|
1143
|
+
type: "document",
|
|
1144
|
+
version: 1,
|
|
1145
|
+
body: [{
|
|
1146
|
+
...node,
|
|
1147
|
+
value: ""
|
|
1148
|
+
}]
|
|
1149
|
+
}).split("\n")[0] ?? "").replace(/^`+|^~+/, "").slice(node.language?.length ?? 0).trim();
|
|
1150
|
+
return [{
|
|
1151
|
+
type: "codeBlock",
|
|
1152
|
+
attrs: {
|
|
1153
|
+
language: node.language ?? null,
|
|
1154
|
+
...meta ? { meta } : {}
|
|
1155
|
+
},
|
|
1156
|
+
...node.value ? { content: [{
|
|
1157
|
+
type: "text",
|
|
1158
|
+
text: node.value
|
|
1159
|
+
}] } : {}
|
|
1160
|
+
}];
|
|
1161
|
+
}
|
|
1162
|
+
case "image": {
|
|
1163
|
+
check([
|
|
1164
|
+
"width",
|
|
1165
|
+
"height",
|
|
1166
|
+
"placement",
|
|
1167
|
+
"responsive"
|
|
1168
|
+
]);
|
|
1169
|
+
if (attrs.placement !== void 0 && ![
|
|
1170
|
+
"block",
|
|
1171
|
+
"center",
|
|
1172
|
+
"wrap-left",
|
|
1173
|
+
"wrap-right"
|
|
1174
|
+
].includes(String(attrs.placement))) fail(path, "unsupported image placement");
|
|
1175
|
+
if (attrs.responsive !== void 0 && typeof attrs.responsive !== "boolean") fail(path, "nonboolean responsive flag");
|
|
1176
|
+
const result = {
|
|
1177
|
+
src: node.url ?? "",
|
|
1178
|
+
alt: node.alt ?? "",
|
|
1179
|
+
title: typeof node.title === "string" ? node.title : null
|
|
1180
|
+
};
|
|
1181
|
+
for (const key of ["width", "height"]) if (attrs[key] !== void 0) {
|
|
1182
|
+
const value = Number(attrs[key]);
|
|
1183
|
+
if (!Number.isFinite(value) || value <= 0) fail(path, `image ${key}`);
|
|
1184
|
+
result[key] = value;
|
|
1185
|
+
}
|
|
1186
|
+
for (const key of ["placement", "responsive"]) if (attrs[key] !== void 0) result[key] = attrs[key];
|
|
1187
|
+
return [{
|
|
1188
|
+
type: "image",
|
|
1189
|
+
attrs: result
|
|
1190
|
+
}];
|
|
1191
|
+
}
|
|
1192
|
+
case "footnoteReference":
|
|
1193
|
+
check();
|
|
1194
|
+
return [{
|
|
1195
|
+
type: "loomaFootnoteReference",
|
|
1196
|
+
attrs: { label: node.label ?? "" },
|
|
1197
|
+
...marks.length ? { marks } : {}
|
|
1198
|
+
}];
|
|
1199
|
+
case "footnoteDefinition":
|
|
1200
|
+
check();
|
|
1201
|
+
return [{
|
|
1202
|
+
type: "loomaFootnote",
|
|
1203
|
+
attrs: { label: node.label ?? "" },
|
|
1204
|
+
content: content(node).length ? children() : [{ type: "paragraph" }]
|
|
1205
|
+
}];
|
|
1206
|
+
case "definition":
|
|
1207
|
+
check();
|
|
1208
|
+
return [{
|
|
1209
|
+
type: "loomaLinkDefinition",
|
|
1210
|
+
attrs: {
|
|
1211
|
+
label: node.label ?? "",
|
|
1212
|
+
href: node.url ?? "",
|
|
1213
|
+
title: typeof node.title === "string" ? node.title : null
|
|
1214
|
+
}
|
|
1215
|
+
}];
|
|
1216
|
+
case "inline":
|
|
1217
|
+
if (node.children?.some((child) => child.type !== "text" || child.id || child.attributes)) fail(path, "formatted inline label");
|
|
1218
|
+
if (node.subType === "mention") {
|
|
1219
|
+
check();
|
|
1220
|
+
return [{
|
|
1221
|
+
type: "mention",
|
|
1222
|
+
attrs: {
|
|
1223
|
+
id: node.ref ?? null,
|
|
1224
|
+
label: text(node)
|
|
1225
|
+
},
|
|
1226
|
+
...marks.length ? { marks } : {}
|
|
1227
|
+
}];
|
|
1228
|
+
}
|
|
1229
|
+
if (node.subType === "chip") {
|
|
1230
|
+
check(["tone"]);
|
|
1231
|
+
if (attrs.tone !== void 0 && !LOOMA_CHIP_TONES.includes(attrs.tone)) fail(path, "unsupported chip tone");
|
|
1232
|
+
return [{
|
|
1233
|
+
type: "loomaChip",
|
|
1234
|
+
attrs: {
|
|
1235
|
+
label: text(node),
|
|
1236
|
+
color: attrs.tone ?? "neutral"
|
|
1237
|
+
},
|
|
1238
|
+
...marks.length ? { marks } : {}
|
|
1239
|
+
}];
|
|
1240
|
+
}
|
|
1241
|
+
return fail(path, `inline type ${node.subType}`);
|
|
1242
|
+
case "leaf":
|
|
1243
|
+
if (node.subType !== "toc") return fail(path, `leaf type ${node.subType}`);
|
|
1244
|
+
check(["depth", "format"]);
|
|
1245
|
+
return [{
|
|
1246
|
+
type: "loomaTableOfContents",
|
|
1247
|
+
attrs: tocSettings(attrs, path)
|
|
1248
|
+
}];
|
|
1249
|
+
case "container": {
|
|
1250
|
+
const label = node.subType === "figure" ? "" : plainTitle(node.title, `${path}.title`);
|
|
1251
|
+
const tone = LOOMA_CALLOUT_TONES.includes(node.subType ?? "") ? node.subType : LOOMA_CALLOUT_TONE_ALIASES[node.subType ?? ""];
|
|
1252
|
+
if (node.subType === "details") {
|
|
1253
|
+
check(["open"]);
|
|
1254
|
+
if (attrs.open !== void 0 && typeof attrs.open !== "boolean") fail(path, "nonboolean disclosure flag");
|
|
1255
|
+
return wrap("loomaExpand", {
|
|
1256
|
+
summary: label || "Details",
|
|
1257
|
+
open: attrs.open === true
|
|
1258
|
+
});
|
|
1259
|
+
}
|
|
1260
|
+
if (tone) {
|
|
1261
|
+
check();
|
|
1262
|
+
return wrap("loomaCallout", {
|
|
1263
|
+
tone,
|
|
1264
|
+
heading: label
|
|
1265
|
+
});
|
|
1266
|
+
}
|
|
1267
|
+
if (node.subType === "tabs") {
|
|
1268
|
+
check([
|
|
1269
|
+
"group",
|
|
1270
|
+
"default",
|
|
1271
|
+
"queryString",
|
|
1272
|
+
"lazy"
|
|
1273
|
+
]);
|
|
1274
|
+
for (const key of [
|
|
1275
|
+
"group",
|
|
1276
|
+
"default",
|
|
1277
|
+
"queryString"
|
|
1278
|
+
]) if (attrs[key] !== void 0 && typeof attrs[key] !== "string") fail(path, `nonstring tab ${key}`);
|
|
1279
|
+
if (attrs.lazy !== void 0 && typeof attrs.lazy !== "boolean") fail(path, "nonboolean tab lazy flag");
|
|
1280
|
+
if (node.title) fail(path, "tab-group title");
|
|
1281
|
+
return wrap("loomaTabs", {
|
|
1282
|
+
group: attrs.group ?? "",
|
|
1283
|
+
codeGroup: false,
|
|
1284
|
+
...attrs.default !== void 0 ? { defaultValue: attrs.default } : {},
|
|
1285
|
+
...attrs.queryString !== void 0 ? { queryString: attrs.queryString } : {},
|
|
1286
|
+
...attrs.lazy !== void 0 ? { lazy: attrs.lazy } : {}
|
|
1287
|
+
});
|
|
1288
|
+
}
|
|
1289
|
+
if (node.subType === "code-group") {
|
|
1290
|
+
check(["group"]);
|
|
1291
|
+
if (attrs.group !== void 0 && typeof attrs.group !== "string") fail(path, "nonstring tab group");
|
|
1292
|
+
if (node.title || !node.body?.length || node.body.some((child) => child.type !== "code")) fail(path, "code group other than fences");
|
|
1293
|
+
return [{
|
|
1294
|
+
type: "loomaTabs",
|
|
1295
|
+
attrs: {
|
|
1296
|
+
group: attrs.group ?? "code-group",
|
|
1297
|
+
codeGroup: true
|
|
1298
|
+
},
|
|
1299
|
+
content: node.body.map((code, index) => ({
|
|
1300
|
+
type: "loomaTab",
|
|
1301
|
+
attrs: { label: codeLabel(code) },
|
|
1302
|
+
content: fromNode(code, `${path}.body[${index}]`, context)
|
|
1303
|
+
}))
|
|
1304
|
+
}];
|
|
1305
|
+
}
|
|
1306
|
+
if (node.subType === "tab") {
|
|
1307
|
+
check(["value"]);
|
|
1308
|
+
if (attrs.value !== void 0 && typeof attrs.value !== "string") fail(path, "nonstring tab value");
|
|
1309
|
+
return wrap("loomaTab", {
|
|
1310
|
+
label,
|
|
1311
|
+
...attrs.value !== void 0 ? { value: attrs.value } : {}
|
|
1312
|
+
});
|
|
1313
|
+
}
|
|
1314
|
+
if (node.subType === "figure") {
|
|
1315
|
+
check();
|
|
1316
|
+
const only = node.body?.[0];
|
|
1317
|
+
const image = only?.type === "paragraph" && !only.id && !only.attributes && content(only).length === 1 ? content(only)[0] : only;
|
|
1318
|
+
if (node.body?.length !== 1 || image?.type !== "image") fail(path, "figure body other than one image");
|
|
1319
|
+
return [{
|
|
1320
|
+
type: "loomaFigure",
|
|
1321
|
+
content: [...children(), {
|
|
1322
|
+
type: "loomaFigcaption",
|
|
1323
|
+
...typeof node.title === "object" && content(node.title).length ? { content: content(node.title).flatMap((child, index) => fromNode(child, `${path}.title[${index}]`, context)) } : {}
|
|
1324
|
+
}]
|
|
1325
|
+
}];
|
|
1326
|
+
}
|
|
1327
|
+
if (node.subType === "toc") {
|
|
1328
|
+
check(["depth", "format"]);
|
|
1329
|
+
if (node.title) fail(path, "table-of-contents title");
|
|
1330
|
+
return wrap("loomaTableOfContents", tocSettings(attrs, path));
|
|
1331
|
+
}
|
|
1332
|
+
return fail(path, `container type ${node.subType}`);
|
|
1333
|
+
}
|
|
1334
|
+
case "table": {
|
|
1335
|
+
check();
|
|
1336
|
+
if (node.title) fail(path, "table caption");
|
|
1337
|
+
if (!node.align) return wrap("table");
|
|
1338
|
+
let column = 0;
|
|
1339
|
+
return [{
|
|
1340
|
+
type: "table",
|
|
1341
|
+
content: (node.children ?? []).map((row) => {
|
|
1342
|
+
column = 0;
|
|
1343
|
+
return {
|
|
1344
|
+
...row,
|
|
1345
|
+
children: row.children?.map((cell) => {
|
|
1346
|
+
const align = node.align?.[column];
|
|
1347
|
+
column += cell.colSpan ?? 1;
|
|
1348
|
+
return {
|
|
1349
|
+
...cell,
|
|
1350
|
+
...align && !cell.attributes?.textAlign ? { attributes: {
|
|
1351
|
+
...cell.attributes,
|
|
1352
|
+
textAlign: ALIGN[align]
|
|
1353
|
+
} } : {}
|
|
1354
|
+
};
|
|
1355
|
+
})
|
|
1356
|
+
};
|
|
1357
|
+
}).flatMap((row, index) => fromNode(row, `${path}.children[${index}]`, context))
|
|
1358
|
+
}];
|
|
1359
|
+
}
|
|
1360
|
+
case "tableRow":
|
|
1361
|
+
check();
|
|
1362
|
+
return wrap("tableRow");
|
|
1363
|
+
case "tableCell": {
|
|
1364
|
+
check([
|
|
1365
|
+
"columnWidths",
|
|
1366
|
+
"textAlign",
|
|
1367
|
+
"backgroundColor",
|
|
1368
|
+
"style"
|
|
1369
|
+
]);
|
|
1370
|
+
const style = cellStyle(attrs.style, path);
|
|
1371
|
+
const textAlign = attrs.textAlign ?? style["text-align"];
|
|
1372
|
+
const backgroundColor = attrs.backgroundColor ?? style["background-color"];
|
|
1373
|
+
if (textAlign !== void 0 && !ALIGN[String(textAlign)]) fail(path, "unsupported table alignment");
|
|
1374
|
+
if (backgroundColor !== void 0) safeBackground(backgroundColor, path);
|
|
1375
|
+
if (attrs.columnWidths !== void 0 && (typeof attrs.columnWidths !== "string" || !/^\d+(?:,\d+)*$/.test(attrs.columnWidths))) fail(path, "column widths");
|
|
1376
|
+
const widths = typeof attrs.columnWidths === "string" ? attrs.columnWidths.split(",").map(Number) : null;
|
|
1377
|
+
if (widths?.some((value) => !Number.isFinite(value) || value < 0)) fail(path, "column widths");
|
|
1378
|
+
return [{
|
|
1379
|
+
type: node.header ? "tableHeader" : "tableCell",
|
|
1380
|
+
attrs: {
|
|
1381
|
+
colspan: node.colSpan ?? 1,
|
|
1382
|
+
rowspan: node.rowSpan ?? 1,
|
|
1383
|
+
colwidth: widths,
|
|
1384
|
+
textAlign: textAlign === void 0 ? null : ALIGN[String(textAlign)],
|
|
1385
|
+
backgroundColor: backgroundColor ?? null
|
|
1386
|
+
},
|
|
1387
|
+
content: node.body ? children() : [{
|
|
1388
|
+
type: "paragraph",
|
|
1389
|
+
...content(node).length ? { content: inline() } : {}
|
|
1390
|
+
}]
|
|
1391
|
+
}];
|
|
1392
|
+
}
|
|
1393
|
+
default: return fail(path, `AST type ${node.type}`);
|
|
1394
|
+
}
|
|
1395
|
+
}
|
|
1396
|
+
function tocSettings(attrs, path) {
|
|
1397
|
+
const depth = Number(attrs.depth ?? 3);
|
|
1398
|
+
const format = attrs.format ?? "plain";
|
|
1399
|
+
if (![
|
|
1400
|
+
1,
|
|
1401
|
+
2,
|
|
1402
|
+
3
|
|
1403
|
+
].includes(depth) || ![
|
|
1404
|
+
"plain",
|
|
1405
|
+
"bulleted",
|
|
1406
|
+
"numbered"
|
|
1407
|
+
].includes(String(format))) fail(path, "table-of-contents settings");
|
|
1408
|
+
return {
|
|
1409
|
+
depth,
|
|
1410
|
+
format
|
|
1411
|
+
};
|
|
1412
|
+
}
|
|
1413
|
+
function cellStyle(style, path) {
|
|
1414
|
+
if (style === void 0) return {};
|
|
1415
|
+
if (typeof style !== "string") return fail(path, "nonstring cell style");
|
|
1416
|
+
const declarations = {};
|
|
1417
|
+
for (const declaration of style.split(";").map((part) => part.trim()).filter(Boolean)) {
|
|
1418
|
+
const [, name, value] = /^(text-align|background-color)\s*:\s*(.+)$/i.exec(declaration) ?? [];
|
|
1419
|
+
if (!name) fail(path, `cell style ${declaration}`);
|
|
1420
|
+
declarations[name.toLowerCase()] = value.trim();
|
|
1421
|
+
}
|
|
1422
|
+
return declarations;
|
|
1423
|
+
}
|
|
1424
|
+
function readBlocks(document) {
|
|
1425
|
+
const definitions = /* @__PURE__ */ new Map();
|
|
1426
|
+
const collect = (node) => {
|
|
1427
|
+
if (node.type === "definition" && !definitions.has(referenceKey(node.label ?? ""))) definitions.set(referenceKey(node.label ?? ""), node);
|
|
1428
|
+
for (const child of node.body ?? node.children ?? []) collect(child);
|
|
1429
|
+
};
|
|
1430
|
+
collect(document);
|
|
1431
|
+
const blocks = document.body.map((node, index) => fromNode(node, `document.body[${index}]`, { definitions }));
|
|
1432
|
+
const headings = headingsOf({
|
|
1433
|
+
type: "doc",
|
|
1434
|
+
content: blocks.flat()
|
|
1435
|
+
}, {});
|
|
1436
|
+
assignLoomaHeadingIds(headings).forEach((id, index) => {
|
|
1437
|
+
headings[index].node.attrs.id ??= id;
|
|
1438
|
+
});
|
|
1439
|
+
return blocks;
|
|
1440
|
+
}
|
|
1441
|
+
/**
|
|
1442
|
+
* Maps supported Expressive Markdown content into the Looma editor's saved JSON shape.
|
|
1443
|
+
* @contract The editor schema cannot represent arbitrary format extensions; unsupported meanings fail
|
|
1444
|
+
* explicitly. A heading without an authored ID gets the anchor its text derives.
|
|
1445
|
+
*/
|
|
1446
|
+
function expressmarkToEditorDocument(document) {
|
|
1447
|
+
if (document.type !== "document" || document.version !== 1) fail("document", "unsupported document version");
|
|
1448
|
+
if (document.title || document.id || Object.keys(document.metadata ?? {}).length || Object.keys(document.attributes ?? {}).length) fail("document", "document metadata (store it alongside editor content)");
|
|
1449
|
+
try {
|
|
1450
|
+
stringify(document);
|
|
1451
|
+
} catch (error) {
|
|
1452
|
+
fail("document", `invalid Expressive Markdown AST (${error instanceof Error ? error.message : "conversion failed"})`);
|
|
1453
|
+
}
|
|
1454
|
+
const colors = /* @__PURE__ */ new Set();
|
|
1455
|
+
const urls = /* @__PURE__ */ new Set();
|
|
1456
|
+
const visit = (node) => {
|
|
1457
|
+
if (node.type === "tableCell" && node.attributes?.backgroundColor !== void 0 && !colors.has(node.attributes.backgroundColor)) {
|
|
1458
|
+
safeBackground(node.attributes.backgroundColor, "document");
|
|
1459
|
+
colors.add(node.attributes.backgroundColor);
|
|
1460
|
+
}
|
|
1461
|
+
if (node.type === "link" || node.type === "image" || node.type === "definition") {
|
|
1462
|
+
const key = `${node.type === "image" ? "image" : "link"}:${node.url}`;
|
|
1463
|
+
if (!urls.has(key)) {
|
|
1464
|
+
safeNavigation(node.url, node.type === "image", "document");
|
|
1465
|
+
urls.add(key);
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
for (const child of node.body ?? node.children ?? []) visit(child);
|
|
1469
|
+
if (typeof node.title === "object") visit(node.title);
|
|
1470
|
+
};
|
|
1471
|
+
visit(document);
|
|
1472
|
+
return {
|
|
1473
|
+
type: "doc",
|
|
1474
|
+
content: readBlocks(document).flat()
|
|
1475
|
+
};
|
|
1476
|
+
}
|
|
1477
|
+
function respell(node, original) {
|
|
1478
|
+
if (node.type !== original.type || node.subType !== original.subType) return node;
|
|
1479
|
+
const result = { ...node };
|
|
1480
|
+
if (original.source && !result.source) result.source = original.source;
|
|
1481
|
+
if (original.spelling && !result.spelling) result.spelling = original.spelling;
|
|
1482
|
+
if (original.titleSpelling && !result.titleSpelling) result.titleSpelling = original.titleSpelling;
|
|
1483
|
+
if (original.tight === false && result.tight) result.tight = false;
|
|
1484
|
+
for (const slot of ["body", "children"]) {
|
|
1485
|
+
const before = original[slot];
|
|
1486
|
+
if (result[slot] && before) result[slot] = result[slot].map((child, index) => before[index] ? respell(child, before[index]) : child);
|
|
1487
|
+
}
|
|
1488
|
+
if (typeof result.title === "object" && typeof original.title === "object") result.title = respell(result.title, original.title);
|
|
1489
|
+
return result;
|
|
1490
|
+
}
|
|
1491
|
+
function keepAuthored(source, edited, options) {
|
|
1492
|
+
const read = readBlocks(source);
|
|
1493
|
+
const own = editorDocumentToExpressmark({
|
|
1494
|
+
type: "doc",
|
|
1495
|
+
content: read.flat()
|
|
1496
|
+
}, {
|
|
1497
|
+
...options,
|
|
1498
|
+
onDiagnostic: void 0
|
|
1499
|
+
}).body.map((node) => JSON.stringify(node));
|
|
1500
|
+
let offset = 0;
|
|
1501
|
+
const keys = read.map((blocks) => own.slice(offset, offset += blocks.length));
|
|
1502
|
+
const current = edited.body.map((node) => JSON.stringify(node));
|
|
1503
|
+
const body = [];
|
|
1504
|
+
let used = -1;
|
|
1505
|
+
for (let index = 0; index < current.length;) {
|
|
1506
|
+
const found = keys.findIndex((key, block) => block > used && key.length > 0 && key.every((value, at) => current[index + at] === value));
|
|
1507
|
+
if (found >= 0) {
|
|
1508
|
+
body.push(source.body[found]);
|
|
1509
|
+
index += keys[found].length;
|
|
1510
|
+
used = found;
|
|
1511
|
+
continue;
|
|
1512
|
+
}
|
|
1513
|
+
const replaced = source.body[used + 1];
|
|
1514
|
+
body.push(replaced ? respell(edited.body[index], replaced) : edited.body[index]);
|
|
1515
|
+
index++;
|
|
1516
|
+
}
|
|
1517
|
+
return {
|
|
1518
|
+
...source,
|
|
1519
|
+
body
|
|
1520
|
+
};
|
|
1521
|
+
}
|
|
1522
|
+
/**
|
|
1523
|
+
* Writes an editor document using Expressmark's shared parser/writer and safe HTML policy.
|
|
1524
|
+
* @contract With `original`, an unedited document is written back byte for byte, and an edit
|
|
1525
|
+
* leaves the source of every other block, the front matter and the spacing between them alone.
|
|
1526
|
+
* @failure Unsupported editor data raises EditorMarkdownConversionError before any input is changed.
|
|
1527
|
+
*/
|
|
1528
|
+
function editorDocumentToMarkdown(document, options = {}) {
|
|
1529
|
+
const conversion = {
|
|
1530
|
+
syntax: "compatible",
|
|
1531
|
+
...options
|
|
1532
|
+
};
|
|
1533
|
+
const edited = editorDocumentToExpressmark(document, conversion);
|
|
1534
|
+
return stringify(conversion.original === void 0 ? edited : keepAuthored(parse(conversion.original, conversion), edited, conversion), conversion);
|
|
1535
|
+
}
|
|
1536
|
+
/**
|
|
1537
|
+
* Serializes the current editor, including its current display cache.
|
|
1538
|
+
* @failure Unsupported content throws EditorMarkdownConversionError without changing the document.
|
|
1539
|
+
*/
|
|
1540
|
+
function editorToMarkdown(editor, options = {}) {
|
|
1541
|
+
return editorDocumentToMarkdown(editor.getJSON(), {
|
|
1542
|
+
...options,
|
|
1543
|
+
references: getReferenceDisplay(editor)
|
|
1544
|
+
});
|
|
1545
|
+
}
|
|
1546
|
+
/**
|
|
1547
|
+
* Parses Expressive Markdown into editable JSON for the supported editor schema.
|
|
1548
|
+
* @contract Front matter stays in the Markdown: pass the Markdown as `original` when writing to keep it.
|
|
1549
|
+
* @failure Unsupported content stops conversion instead of being silently flattened.
|
|
1550
|
+
*/
|
|
1551
|
+
function markdownToEditorDocument(markdown, options = {}) {
|
|
1552
|
+
const document = parse(markdown, {
|
|
1553
|
+
syntax: "compatible",
|
|
1554
|
+
...options
|
|
1555
|
+
});
|
|
1556
|
+
delete document.title;
|
|
1557
|
+
delete document.id;
|
|
1558
|
+
delete document.metadata;
|
|
1559
|
+
return expressmarkToEditorDocument(document);
|
|
1560
|
+
}
|
|
1561
|
+
//#endregion
|
|
1562
|
+
export { normalizeLoomaChipTone as _, expressmarkToEditorDocument as a, LoomaReferencePresentation as c, setReferenceDisplay as d, LOOMA_CALLOUT_TONES as f, normalizeLoomaCalloutTone as g, loomaHeadingSlug as h, editorToMarkdown as i, getReferenceDisplay as l, assignLoomaHeadingIds as m, editorDocumentToExpressmark as n, markdownToEditorDocument as o, LOOMA_CHIP_TONES as p, editorDocumentToMarkdown as r, LoomaReference as s, EditorMarkdownConversionError as t, referenceDisplay as u, splitLoomaFrontmatter as v };
|