@threadlabs/looma 0.46.2 → 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 CHANGED
@@ -463,7 +463,7 @@
463
463
  ],
464
464
  "schemaVersion": 1,
465
465
  "package": "@threadlabs/looma",
466
- "version": "0.46.2",
466
+ "version": "0.46.4",
467
467
  "groups": [
468
468
  {
469
469
  "id": "layout",
@@ -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,11 +536,53 @@ 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, ids) {
545
+ const roots = [];
546
+ const stack = [];
547
+ headings.forEach((heading) => {
548
+ const level = Number(heading.node.attrs?.level ?? 1);
549
+ if (level > depth || !heading.text) return;
550
+ while (stack.length && stack[stack.length - 1].level >= level) stack.pop();
551
+ const item = {
552
+ type: "listItem",
553
+ body: [{
554
+ type: "paragraph",
555
+ children: [{
556
+ type: "link",
557
+ url: `#${ids.get(heading.node)}`,
558
+ value: heading.text,
559
+ attributes: {
560
+ target: "_self",
561
+ rel: "noopener noreferrer"
562
+ }
563
+ }]
564
+ }]
565
+ };
566
+ const parent = stack[stack.length - 1]?.item;
567
+ const destination = parent ? parent.body ??= [] : roots;
568
+ let list = destination[destination.length - 1];
569
+ if (list?.type !== "list") {
570
+ list = {
571
+ type: "list",
572
+ ordered,
573
+ ...ordered ? { start: 1 } : {},
574
+ children: []
575
+ };
576
+ destination.push(list);
577
+ }
578
+ list.children.push(item);
579
+ stack.push({
580
+ level,
581
+ item
582
+ });
583
+ });
584
+ return roots;
585
+ }
544
586
  function customHeadings(headings) {
545
587
  const marked = /* @__PURE__ */ new Set();
546
588
  for (;;) {
@@ -662,7 +704,7 @@ function toBareNode(node, path, context) {
662
704
  case "paragraph": return plain("paragraph");
663
705
  case "heading": return plain("heading", ["level", "id"], {
664
706
  level: Number(attrs.level ?? 1),
665
- ...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) } : {}
666
708
  });
667
709
  case "blockquote": return block("blockquote");
668
710
  case "horizontalRule":
@@ -896,7 +938,7 @@ function toBareNode(node, path, context) {
896
938
  fields(node, ["format", "depth"], path);
897
939
  const depth = Number(attrs.depth ?? 3);
898
940
  const format = string(attrs.format, "plain");
899
- if (!node.content?.length) {
941
+ if (!node.content?.length && context.tableOfContents !== "entries") {
900
942
  const attributes = {};
901
943
  if (depth !== 3) attributes.depth = String(depth);
902
944
  if (format !== "plain") attributes.format = format;
@@ -913,7 +955,7 @@ function toBareNode(node, path, context) {
913
955
  depth: String(depth),
914
956
  format
915
957
  },
916
- body: blocks()
958
+ body: node.content?.length ? blocks() : tocBody(context.headings, depth, format === "numbered", context.headingIds)
917
959
  };
918
960
  }
919
961
  default: return fail(path, `node ${node.type ?? "unknown"}`);
@@ -923,7 +965,7 @@ function toBareNode(node, path, context) {
923
965
  * Maps an editor's saved JSON to the independent Expressive Markdown AST without DOM access.
924
966
  * @contract Unsupported nodes, marks and durable attributes throw with their location; input is never
925
967
  * mutated. Footnote and link definitions stay where they are, and a table of contents stays a
926
- * placeholder unless it holds authored navigation.
968
+ * placeholder unless it holds authored navigation or `tableOfContents: "entries"` is requested.
927
969
  */
928
970
  function editorDocumentToExpressmark(document, options = {}) {
929
971
  if (document.type !== "doc") fail("document", "non-document root");
@@ -978,19 +1020,25 @@ function editorDocumentToExpressmark(document, options = {}) {
978
1020
  visit(document);
979
1021
  const references = options.references ?? {};
980
1022
  const headings = headingsOf(document, references);
1023
+ const anchored = options.tableOfContents === "entries" ? headingsOf(document, references, true) : [];
981
1024
  const authored = /* @__PURE__ */ new Set();
982
- for (const { id } of headings) {
1025
+ for (const { id } of options.tableOfContents === "entries" ? anchored : headings) {
983
1026
  if (id && authored.has(id)) options.onDiagnostic?.({
984
1027
  code: "duplicate-id",
985
1028
  severity: "warning",
986
- 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.`
987
1030
  });
988
1031
  if (id) authored.add(id);
989
1032
  }
1033
+ const ids = assignLoomaHeadingIds(anchored);
1034
+ const headingIds = new WeakMap(anchored.map((heading, index) => [heading.node, ids[index]]));
990
1035
  const context = {
991
1036
  custom: customHeadings(headings),
992
1037
  definitions,
993
- references
1038
+ references,
1039
+ headings,
1040
+ headingIds,
1041
+ tableOfContents: options.tableOfContents
994
1042
  };
995
1043
  return {
996
1044
  type: "document",
@@ -1505,7 +1553,9 @@ function keepAuthored(source, edited, options) {
1505
1553
  for (let index = 0; index < current.length;) {
1506
1554
  const found = keys.findIndex((key, block) => block > used && key.length > 0 && key.every((value, at) => current[index + at] === value));
1507
1555
  if (found >= 0) {
1508
- body.push(source.body[found]);
1556
+ const original = source.body[found];
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);
1509
1559
  index += keys[found].length;
1510
1560
  used = found;
1511
1561
  continue;
@@ -1523,6 +1573,7 @@ function keepAuthored(source, edited, options) {
1523
1573
  * Writes an editor document using Expressmark's shared parser/writer and safe HTML policy.
1524
1574
  * @contract With `original`, an unedited document is written back byte for byte, and an edit
1525
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`.
1526
1577
  * @failure Unsupported editor data raises EditorMarkdownConversionError before any input is changed.
1527
1578
  */
1528
1579
  function editorDocumentToMarkdown(document, options = {}) {
@@ -4,6 +4,8 @@ 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 and stable heading IDs for portable exports; authored navigation is preserved. Defaults to a reader-built placeholder. */
8
+ tableOfContents?: "placeholder" | "entries";
7
9
  /**
8
10
  * The Markdown the document was read from. Its front matter, every block the editor left
9
11
  * unchanged and the spacing between them are written back byte for byte; an edited block keeps
@@ -24,7 +26,7 @@ export declare class EditorMarkdownConversionError extends Error {
24
26
  * Maps an editor's saved JSON to the independent Expressive Markdown AST without DOM access.
25
27
  * @contract Unsupported nodes, marks and durable attributes throw with their location; input is never
26
28
  * mutated. Footnote and link definitions stay where they are, and a table of contents stays a
27
- * placeholder unless it holds authored navigation.
29
+ * placeholder unless it holds authored navigation or `tableOfContents: "entries"` is requested.
28
30
  */
29
31
  export declare function editorDocumentToExpressmark(document: JSONContent, options?: EditorMarkdownConversionOptions): Document;
30
32
  /**
@@ -37,6 +39,7 @@ export declare function expressmarkToEditorDocument(document: Document): JSONCon
37
39
  * Writes an editor document using Expressmark's shared parser/writer and safe HTML policy.
38
40
  * @contract With `original`, an unedited document is written back byte for byte, and an edit
39
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`.
40
43
  * @failure Unsupported editor data raises EditorMarkdownConversionError before any input is changed.
41
44
  */
42
45
  export declare function editorDocumentToMarkdown(document: JSONContent, options?: EditorMarkdownConversionOptions): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@threadlabs/looma",
3
- "version": "0.46.2",
3
+ "version": "0.46.4",
4
4
  "type": "module",
5
5
  "description": "Looma components as HTML, Vue, and plain DOM, with design tokens.",
6
6
  "license": "MIT",
@@ -113,7 +113,7 @@
113
113
  "highlight.js": "11.11.2",
114
114
  "lowlight": "^3.3.0",
115
115
  "lucide": "~0.554.0",
116
- "expressmark": ">=0.4.0 <1",
116
+ "expressmark": ">=0.5.1 <1",
117
117
  "parse5": "^8.0.1"
118
118
  },
119
119
  "peerDependencies": {