documents.js 2.2.0 → 2.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.
Files changed (44) hide show
  1. package/README.md +30 -5
  2. package/dist/diagnostics-C5-bG09J.d.cts +18 -0
  3. package/dist/diagnostics-C5-bG09J.d.ts +18 -0
  4. package/dist/index.cjs +10 -0
  5. package/dist/index.d.cts +5 -1
  6. package/dist/index.d.ts +5 -1
  7. package/dist/index.js +5 -1
  8. package/dist/latex/diagnostics.cjs +21 -0
  9. package/dist/latex/diagnostics.d.cts +2 -0
  10. package/dist/latex/diagnostics.d.ts +2 -0
  11. package/dist/latex/diagnostics.js +19 -0
  12. package/dist/latex/lint.cjs +82 -0
  13. package/dist/latex/lint.d.cts +6 -0
  14. package/dist/latex/lint.d.ts +6 -0
  15. package/dist/latex/lint.js +81 -0
  16. package/dist/latex/lower.cjs +550 -0
  17. package/dist/latex/lower.d.cts +24 -0
  18. package/dist/latex/lower.d.ts +24 -0
  19. package/dist/latex/lower.js +548 -0
  20. package/dist/latex/rational.cjs +29 -0
  21. package/dist/latex/rational.d.cts +11 -0
  22. package/dist/latex/rational.d.ts +11 -0
  23. package/dist/latex/rational.js +27 -0
  24. package/dist/latex/symbols.cjs +128 -0
  25. package/dist/latex/symbols.d.cts +16 -0
  26. package/dist/latex/symbols.d.ts +16 -0
  27. package/dist/latex/symbols.js +124 -0
  28. package/dist/latex/temml.cjs +121 -0
  29. package/dist/latex/temml.d.cts +25 -0
  30. package/dist/latex/temml.d.ts +25 -0
  31. package/dist/latex/temml.js +93 -0
  32. package/dist/markdown/math.cjs +110 -0
  33. package/dist/markdown/math.d.cts +9 -0
  34. package/dist/markdown/math.d.ts +9 -0
  35. package/dist/markdown/math.js +109 -0
  36. package/dist/markdown/read.cjs +3 -2
  37. package/dist/markdown/read.d.cts +2 -1
  38. package/dist/markdown/read.d.ts +2 -1
  39. package/dist/markdown/read.js +3 -2
  40. package/dist/markdown/write.cjs +23 -4
  41. package/dist/markdown/write.js +23 -4
  42. package/dist/model/formula.cjs +1 -1
  43. package/dist/model/formula.js +1 -1
  44. package/package.json +2 -1
@@ -0,0 +1,109 @@
1
+ import { buildFormulaBlock } from "../model/formula.js";
2
+ import { extractSymbolDefinitionsFromProse } from "../latex/symbols.js";
3
+ import { latexToFormula } from "../latex/lower.js";
4
+ //#region src/markdown/math.ts
5
+ const MATH_BLOCK_STYLE_ID = "MathBlock";
6
+ const MATH_INLINE_FONT_MARKER = "Cambria Math";
7
+ const STAND_IN_FRAME = {
8
+ xPt: 0,
9
+ yPt: 0,
10
+ widthPt: 0,
11
+ heightPt: 22
12
+ };
13
+ const MATH_BLOCK_SOURCE = "markdown:math-block";
14
+ const MATH_INLINE_SOURCE = "markdown:math-inline";
15
+ function extractInlineMath(runs) {
16
+ const latexRuns = [];
17
+ const remainingRuns = [];
18
+ for (const run of runs) {
19
+ if (run.fontFamily === MATH_INLINE_FONT_MARKER && run.text.trim() !== "") {
20
+ latexRuns.push(run.text);
21
+ continue;
22
+ }
23
+ remainingRuns.push(run);
24
+ }
25
+ const consumed = latexRuns.length > 0 && remainingRuns.every((run) => run.text === "");
26
+ return {
27
+ latexRuns,
28
+ remainingRuns: consumed ? [] : remainingRuns,
29
+ consumed
30
+ };
31
+ }
32
+ function lowerMarkdownMath(document, options) {
33
+ if (document.kind !== "wordprocessing") return document;
34
+ const sink = options?.onDiagnostic;
35
+ const proseEntries = extractSymbolDefinitionsFromProse(document, sink);
36
+ const known = new Map(proseEntries.map((entry) => [entry.glyph, entry]));
37
+ const minted = /* @__PURE__ */ new Map();
38
+ const lowerOne = (latex, source) => {
39
+ if (latex.trim() === "") return;
40
+ const result = latexToFormula(latex, {
41
+ symbolEntries: [...known.values()],
42
+ source
43
+ });
44
+ for (const diagnostic of result.diagnostics) sink?.(diagnostic);
45
+ for (const entry of result.mintedSymbols) if (!known.has(entry.glyph) && !minted.has(entry.glyph)) minted.set(entry.glyph, entry);
46
+ return buildFormulaBlock(result.formula, STAND_IN_FRAME, source);
47
+ };
48
+ const lowerBlocks = (blocks) => {
49
+ const out = [];
50
+ for (const block of blocks) {
51
+ if (block.kind === "table") {
52
+ out.push({
53
+ ...block,
54
+ rows: block.rows.map((row) => ({
55
+ ...row,
56
+ cells: row.cells.map((cell) => ({
57
+ ...cell,
58
+ blocks: lowerBlocks(cell.blocks)
59
+ }))
60
+ }))
61
+ });
62
+ continue;
63
+ }
64
+ if (block.kind !== "paragraph") {
65
+ out.push(block);
66
+ continue;
67
+ }
68
+ if (block.styleId === MATH_BLOCK_STYLE_ID) {
69
+ const latex = block.runs.map((run) => run.text).join("");
70
+ const formulaBlock = lowerOne(latex, MATH_BLOCK_SOURCE);
71
+ out.push(formulaBlock ?? block);
72
+ continue;
73
+ }
74
+ const extraction = extractInlineMath(block.runs);
75
+ if (extraction.latexRuns.length === 0) {
76
+ out.push(block);
77
+ continue;
78
+ }
79
+ if (!extraction.consumed) out.push({
80
+ ...block,
81
+ runs: [...extraction.remainingRuns]
82
+ });
83
+ for (const latex of extraction.latexRuns) {
84
+ const formulaBlock = lowerOne(latex, MATH_INLINE_SOURCE);
85
+ if (formulaBlock !== void 0) out.push(formulaBlock);
86
+ }
87
+ }
88
+ return out;
89
+ };
90
+ const sections = document.sections.map((section) => ({
91
+ ...section,
92
+ blocks: lowerBlocks(section.blocks)
93
+ }));
94
+ const symbols = [...known.values(), ...minted.values()];
95
+ if (symbols.length === 0) return {
96
+ ...document,
97
+ sections
98
+ };
99
+ return {
100
+ ...document,
101
+ sections,
102
+ symbolTable: {
103
+ symbols,
104
+ units: []
105
+ }
106
+ };
107
+ }
108
+ //#endregion
109
+ export { lowerMarkdownMath };
@@ -1,13 +1,14 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_markdown_math = require("./math.cjs");
2
3
  let markdown_codec = require("markdown-codec");
3
4
  //#region src/markdown/read.ts
4
- function readMarkdownContent(text, options) {
5
+ function readMarkdownContent(text, options, math) {
5
6
  const { document } = (0, markdown_codec.readMarkdown)(text, {
6
7
  frontMatter: true,
7
8
  ...options
8
9
  });
9
10
  if (document.kind !== "wordprocessing") throw new Error("readMarkdown returned a non-wordprocessing ContentDocument");
10
- return document;
11
+ return require_markdown_math.lowerMarkdownMath(document, math);
11
12
  }
12
13
  //#endregion
13
14
  exports.readMarkdownContent = readMarkdownContent;
@@ -1,6 +1,7 @@
1
+ import { MarkdownMathLoweringOptions } from "./math.cjs";
1
2
  import { ContentDocument } from "document-schema.js";
2
3
  import { ReadMarkdownOptions } from "markdown-codec";
3
4
  //#region src/markdown/read.d.ts
4
- declare function readMarkdownContent(text: string, options?: ReadMarkdownOptions): ContentDocument;
5
+ declare function readMarkdownContent(text: string, options?: ReadMarkdownOptions, math?: MarkdownMathLoweringOptions): ContentDocument;
5
6
  //#endregion
6
7
  export { readMarkdownContent };
@@ -1,6 +1,7 @@
1
+ import { MarkdownMathLoweringOptions } from "./math.js";
1
2
  import { ContentDocument } from "document-schema.js";
2
3
  import { ReadMarkdownOptions } from "markdown-codec";
3
4
  //#region src/markdown/read.d.ts
4
- declare function readMarkdownContent(text: string, options?: ReadMarkdownOptions): ContentDocument;
5
+ declare function readMarkdownContent(text: string, options?: ReadMarkdownOptions, math?: MarkdownMathLoweringOptions): ContentDocument;
5
6
  //#endregion
6
7
  export { readMarkdownContent };
@@ -1,12 +1,13 @@
1
+ import { lowerMarkdownMath } from "./math.js";
1
2
  import { readMarkdown } from "markdown-codec";
2
3
  //#region src/markdown/read.ts
3
- function readMarkdownContent(text, options) {
4
+ function readMarkdownContent(text, options, math) {
4
5
  const { document } = readMarkdown(text, {
5
6
  frontMatter: true,
6
7
  ...options
7
8
  });
8
9
  if (document.kind !== "wordprocessing") throw new Error("readMarkdown returned a non-wordprocessing ContentDocument");
9
- return document;
10
+ return lowerMarkdownMath(document, math);
10
11
  }
11
12
  //#endregion
12
13
  export { readMarkdownContent };
@@ -2,6 +2,28 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_model_formula = require("../model/formula.cjs");
3
3
  let markdown_codec = require("markdown-codec");
4
4
  //#region src/markdown/write.ts
5
+ const MATH_BLOCK_STYLE_ID = "MathBlock";
6
+ const MATH_INLINE_FONT_MARKER = "Cambria Math";
7
+ const MATH_INLINE_SOURCE = "markdown:math-inline";
8
+ function formulaParagraph(formula) {
9
+ const latex = formula.presentation?.latex;
10
+ if (latex === void 0) return {
11
+ kind: "paragraph",
12
+ runs: [{ text: require_model_formula.formulaPlaceholderText(formula) }]
13
+ };
14
+ if (formula.provenance?.source === MATH_INLINE_SOURCE) return {
15
+ kind: "paragraph",
16
+ runs: [{
17
+ text: latex,
18
+ fontFamily: MATH_INLINE_FONT_MARKER
19
+ }]
20
+ };
21
+ return {
22
+ kind: "paragraph",
23
+ runs: [{ text: latex }],
24
+ styleId: MATH_BLOCK_STYLE_ID
25
+ };
26
+ }
5
27
  function markdownBlock(block) {
6
28
  if (block.kind === "table") return {
7
29
  ...block,
@@ -12,10 +34,7 @@ function markdownBlock(block) {
12
34
  };
13
35
  if (block.kind === "embeddedObject") {
14
36
  const formula = require_model_formula.formulaOfBlock(block);
15
- if (formula !== void 0) return {
16
- kind: "paragraph",
17
- runs: [{ text: require_model_formula.formulaPlaceholderText(formula) }]
18
- };
37
+ if (formula !== void 0) return formulaParagraph(formula);
19
38
  return block;
20
39
  }
21
40
  return block;
@@ -1,6 +1,28 @@
1
1
  import { formulaOfBlock, formulaPlaceholderText } from "../model/formula.js";
2
2
  import { MarkdownUnsupportedDocumentKindError, writeMarkdown } from "markdown-codec";
3
3
  //#region src/markdown/write.ts
4
+ const MATH_BLOCK_STYLE_ID = "MathBlock";
5
+ const MATH_INLINE_FONT_MARKER = "Cambria Math";
6
+ const MATH_INLINE_SOURCE = "markdown:math-inline";
7
+ function formulaParagraph(formula) {
8
+ const latex = formula.presentation?.latex;
9
+ if (latex === void 0) return {
10
+ kind: "paragraph",
11
+ runs: [{ text: formulaPlaceholderText(formula) }]
12
+ };
13
+ if (formula.provenance?.source === MATH_INLINE_SOURCE) return {
14
+ kind: "paragraph",
15
+ runs: [{
16
+ text: latex,
17
+ fontFamily: MATH_INLINE_FONT_MARKER
18
+ }]
19
+ };
20
+ return {
21
+ kind: "paragraph",
22
+ runs: [{ text: latex }],
23
+ styleId: MATH_BLOCK_STYLE_ID
24
+ };
25
+ }
4
26
  function markdownBlock(block) {
5
27
  if (block.kind === "table") return {
6
28
  ...block,
@@ -11,10 +33,7 @@ function markdownBlock(block) {
11
33
  };
12
34
  if (block.kind === "embeddedObject") {
13
35
  const formula = formulaOfBlock(block);
14
- if (formula !== void 0) return {
15
- kind: "paragraph",
16
- runs: [{ text: formulaPlaceholderText(formula) }]
17
- };
36
+ if (formula !== void 0) return formulaParagraph(formula);
18
37
  return block;
19
38
  }
20
39
  return block;
@@ -22,7 +22,7 @@ function formulaOfBlock(block) {
22
22
  return block.document.kind === "formula" ? block.document.formula : void 0;
23
23
  }
24
24
  function formulaPlaceholderText(formula) {
25
- return formula.starMath ?? "[formula]";
25
+ return formula.starMath ?? formula.presentation?.latex ?? "[formula]";
26
26
  }
27
27
  //#endregion
28
28
  exports.buildFormulaBlock = buildFormulaBlock;
@@ -21,7 +21,7 @@ function formulaOfBlock(block) {
21
21
  return block.document.kind === "formula" ? block.document.formula : void 0;
22
22
  }
23
23
  function formulaPlaceholderText(formula) {
24
- return formula.starMath ?? "[formula]";
24
+ return formula.starMath ?? formula.presentation?.latex ?? "[formula]";
25
25
  }
26
26
  //#endregion
27
27
  export { buildFormulaBlock, formulaDocument, formulaOfBlock, formulaPlaceholderText };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "documents.js",
3
- "version": "2.2.0",
3
+ "version": "2.3.0",
4
4
  "description": "Bidirectional docx/pptx <-> PDF conversion and a read+write editable OOXML document model, built on ooxml.js and Zod 4 codecs.",
5
5
  "type": "module",
6
6
  "repository": {
@@ -90,6 +90,7 @@
90
90
  "odf.js": "^3.0.1",
91
91
  "ooxml.js": "^2.16.0",
92
92
  "pdf-codec": "^2.2.35",
93
+ "temml": "0.13.4",
93
94
  "zod": "^4.4.3"
94
95
  },
95
96
  "devDependencies": {