@threadlabs/looma 0.46.3 → 0.46.4
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/docs/catalog.json +1 -1
- package/editor/chunks/expressmark.js +16 -11
- package/editor/expressmark.d.ts +2 -1
- package/package.json +1 -1
package/docs/catalog.json
CHANGED
|
@@ -528,7 +528,7 @@ var displayText = (node, references) => {
|
|
|
528
528
|
if (node.type === "mention") return `@${references[`mention:${node.attrs?.id}`]?.label ?? string(node.attrs?.label, string(node.attrs?.id))}`;
|
|
529
529
|
return node.text ?? (node.content ?? []).map((child) => displayText(child, references)).join("");
|
|
530
530
|
};
|
|
531
|
-
function headingsOf(document, references) {
|
|
531
|
+
function headingsOf(document, references, includeNavigation = false) {
|
|
532
532
|
const headings = [];
|
|
533
533
|
const visit = (node) => {
|
|
534
534
|
if (node.type === "heading") headings.push({
|
|
@@ -536,16 +536,15 @@ function headingsOf(document, references) {
|
|
|
536
536
|
text: displayText(node, references).trim(),
|
|
537
537
|
id: node.attrs?.id ? string(node.attrs.id) : void 0
|
|
538
538
|
});
|
|
539
|
-
else if (node.type !== "loomaTableOfContents") for (const child of node.content ?? []) visit(child);
|
|
539
|
+
else if (includeNavigation || node.type !== "loomaTableOfContents") for (const child of node.content ?? []) visit(child);
|
|
540
540
|
};
|
|
541
541
|
visit(document);
|
|
542
542
|
return headings;
|
|
543
543
|
}
|
|
544
|
-
function tocBody(headings, depth, ordered) {
|
|
545
|
-
const ids = assignLoomaHeadingIds(headings);
|
|
544
|
+
function tocBody(headings, depth, ordered, ids) {
|
|
546
545
|
const roots = [];
|
|
547
546
|
const stack = [];
|
|
548
|
-
headings.forEach((heading
|
|
547
|
+
headings.forEach((heading) => {
|
|
549
548
|
const level = Number(heading.node.attrs?.level ?? 1);
|
|
550
549
|
if (level > depth || !heading.text) return;
|
|
551
550
|
while (stack.length && stack[stack.length - 1].level >= level) stack.pop();
|
|
@@ -555,7 +554,7 @@ function tocBody(headings, depth, ordered) {
|
|
|
555
554
|
type: "paragraph",
|
|
556
555
|
children: [{
|
|
557
556
|
type: "link",
|
|
558
|
-
url: `#${ids
|
|
557
|
+
url: `#${ids.get(heading.node)}`,
|
|
559
558
|
value: heading.text,
|
|
560
559
|
attributes: {
|
|
561
560
|
target: "_self",
|
|
@@ -705,7 +704,7 @@ function toBareNode(node, path, context) {
|
|
|
705
704
|
case "paragraph": return plain("paragraph");
|
|
706
705
|
case "heading": return plain("heading", ["level", "id"], {
|
|
707
706
|
level: Number(attrs.level ?? 1),
|
|
708
|
-
...context.custom.has(node) ? { id: string(attrs.id) } : {}
|
|
707
|
+
...context.headingIds.has(node) ? { id: context.headingIds.get(node) } : context.custom.has(node) ? { id: string(attrs.id) } : {}
|
|
709
708
|
});
|
|
710
709
|
case "blockquote": return block("blockquote");
|
|
711
710
|
case "horizontalRule":
|
|
@@ -956,7 +955,7 @@ function toBareNode(node, path, context) {
|
|
|
956
955
|
depth: String(depth),
|
|
957
956
|
format
|
|
958
957
|
},
|
|
959
|
-
body: node.content?.length ? blocks() : tocBody(context.headings, depth, format === "numbered")
|
|
958
|
+
body: node.content?.length ? blocks() : tocBody(context.headings, depth, format === "numbered", context.headingIds)
|
|
960
959
|
};
|
|
961
960
|
}
|
|
962
961
|
default: return fail(path, `node ${node.type ?? "unknown"}`);
|
|
@@ -1021,20 +1020,24 @@ function editorDocumentToExpressmark(document, options = {}) {
|
|
|
1021
1020
|
visit(document);
|
|
1022
1021
|
const references = options.references ?? {};
|
|
1023
1022
|
const headings = headingsOf(document, references);
|
|
1023
|
+
const anchored = options.tableOfContents === "entries" ? headingsOf(document, references, true) : [];
|
|
1024
1024
|
const authored = /* @__PURE__ */ new Set();
|
|
1025
|
-
for (const { id } of headings) {
|
|
1025
|
+
for (const { id } of options.tableOfContents === "entries" ? anchored : headings) {
|
|
1026
1026
|
if (id && authored.has(id)) options.onDiagnostic?.({
|
|
1027
1027
|
code: "duplicate-id",
|
|
1028
1028
|
severity: "warning",
|
|
1029
|
-
message: `Authored heading ID "${id}" is repeated and was preserved.`
|
|
1029
|
+
message: options.tableOfContents === "entries" ? `Authored heading ID "${id}" is repeated; portable output assigns a unique anchor.` : `Authored heading ID "${id}" is repeated and was preserved.`
|
|
1030
1030
|
});
|
|
1031
1031
|
if (id) authored.add(id);
|
|
1032
1032
|
}
|
|
1033
|
+
const ids = assignLoomaHeadingIds(anchored);
|
|
1034
|
+
const headingIds = new WeakMap(anchored.map((heading, index) => [heading.node, ids[index]]));
|
|
1033
1035
|
const context = {
|
|
1034
1036
|
custom: customHeadings(headings),
|
|
1035
1037
|
definitions,
|
|
1036
1038
|
references,
|
|
1037
1039
|
headings,
|
|
1040
|
+
headingIds,
|
|
1038
1041
|
tableOfContents: options.tableOfContents
|
|
1039
1042
|
};
|
|
1040
1043
|
return {
|
|
@@ -1551,7 +1554,8 @@ function keepAuthored(source, edited, options) {
|
|
|
1551
1554
|
const found = keys.findIndex((key, block) => block > used && key.length > 0 && key.every((value, at) => current[index + at] === value));
|
|
1552
1555
|
if (found >= 0) {
|
|
1553
1556
|
const original = source.body[found];
|
|
1554
|
-
|
|
1557
|
+
if (options.tableOfContents === "entries" && findNode(original, (node) => node.type === "heading" || node.type === "leaf" && node.subType === "toc")) body.push(...edited.body.slice(index, index + keys[found].length).map((node) => respell(node, original)));
|
|
1558
|
+
else body.push(original);
|
|
1555
1559
|
index += keys[found].length;
|
|
1556
1560
|
used = found;
|
|
1557
1561
|
continue;
|
|
@@ -1569,6 +1573,7 @@ function keepAuthored(source, edited, options) {
|
|
|
1569
1573
|
* Writes an editor document using Expressmark's shared parser/writer and safe HTML policy.
|
|
1570
1574
|
* @contract With `original`, an unedited document is written back byte for byte, and an edit
|
|
1571
1575
|
* leaves the source of every other block, the front matter and the spacing between them alone.
|
|
1576
|
+
* Portable entries deliberately expand placeholders and pin heading IDs even with `original`.
|
|
1572
1577
|
* @failure Unsupported editor data raises EditorMarkdownConversionError before any input is changed.
|
|
1573
1578
|
*/
|
|
1574
1579
|
function editorDocumentToMarkdown(document, options = {}) {
|
package/editor/expressmark.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { type LoomaReferenceMap } from "./reference-display.js";
|
|
|
4
4
|
/** Conversion-time display snapshot, independent from the authored editor document. */
|
|
5
5
|
export interface EditorMarkdownConversionOptions extends ConversionOptions {
|
|
6
6
|
references?: LoomaReferenceMap;
|
|
7
|
-
/** Write current links for portable exports; authored navigation is
|
|
7
|
+
/** Write current links and stable heading IDs for portable exports; authored navigation is preserved. Defaults to a reader-built placeholder. */
|
|
8
8
|
tableOfContents?: "placeholder" | "entries";
|
|
9
9
|
/**
|
|
10
10
|
* The Markdown the document was read from. Its front matter, every block the editor left
|
|
@@ -39,6 +39,7 @@ export declare function expressmarkToEditorDocument(document: Document): JSONCon
|
|
|
39
39
|
* Writes an editor document using Expressmark's shared parser/writer and safe HTML policy.
|
|
40
40
|
* @contract With `original`, an unedited document is written back byte for byte, and an edit
|
|
41
41
|
* leaves the source of every other block, the front matter and the spacing between them alone.
|
|
42
|
+
* Portable entries deliberately expand placeholders and pin heading IDs even with `original`.
|
|
42
43
|
* @failure Unsupported editor data raises EditorMarkdownConversionError before any input is changed.
|
|
43
44
|
*/
|
|
44
45
|
export declare function editorDocumentToMarkdown(document: JSONContent, options?: EditorMarkdownConversionOptions): string;
|