@illusions-lab/mdi-to-docx 2.0.2 → 2.0.3

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/README.md CHANGED
@@ -3,3 +3,5 @@
3
3
  Maps MDI mdast directly to OOXML (native <w:ruby>, <w:eastAsianLayout>, section-level vertical writing) — does not go through hast/HTML.
4
4
 
5
5
  Part of the [mdi-js](https://github.com/illusions-lab/mdi-js) monorepo. See the root README for the full package architecture.
6
+
7
+ Documentation: https://illusions-lab.github.io/mdi-js/
package/dist/index.cjs CHANGED
@@ -20,11 +20,51 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
- MDI_SPEC_VERSION: () => MDI_SPEC_VERSION
23
+ MDI_SPEC_VERSION: () => MDI_SPEC_VERSION,
24
+ mdiToDocx: () => mdiToDocx
24
25
  });
25
26
  module.exports = __toCommonJS(index_exports);
27
+ var import_docx = require("docx");
26
28
  var MDI_SPEC_VERSION = "2.0";
29
+ async function mdiToDocx(tree) {
30
+ const children = tree.children.flatMap(block);
31
+ const vertical = tree.data?.frontmatter?.writingMode === "vertical";
32
+ const document = new import_docx.Document({ sections: [{ children, properties: vertical ? { page: { textDirection: import_docx.PageTextDirectionType.TOP_TO_BOTTOM_RIGHT_TO_LEFT } } : void 0 }], title: tree.data?.frontmatter?.title, creator: tree.data?.frontmatter?.author });
33
+ return import_docx.Packer.toBuffer(document);
34
+ }
35
+ function block(node) {
36
+ if (node.type === "heading") return [new import_docx.Paragraph({ heading: [import_docx.HeadingLevel.HEADING_1, import_docx.HeadingLevel.HEADING_2, import_docx.HeadingLevel.HEADING_3, import_docx.HeadingLevel.HEADING_4, import_docx.HeadingLevel.HEADING_5, import_docx.HeadingLevel.HEADING_6][node.depth - 1], children: inline(node.children) })];
37
+ if (node.type === "paragraph") return [new import_docx.Paragraph({ children: inline(node.children) })];
38
+ if (node.type === "list") return node.children.flatMap((item) => item.children.filter((n) => n.type === "paragraph").map((n) => new import_docx.Paragraph({ children: inline(n.children), bullet: { level: 0 } })));
39
+ if (node.type === "mdiPagebreak") return [new import_docx.Paragraph({ children: [new import_docx.PageBreak()] })];
40
+ if (node.type === "mdiBlank") return [new import_docx.Paragraph("")];
41
+ return [];
42
+ }
43
+ function inline(nodes) {
44
+ return nodes.flatMap((node) => {
45
+ if (node.type === "text") return [new import_docx.TextRun(node.value)];
46
+ if (node.type === "break" || node.type === "mdiBreak") return [new import_docx.TextRun({ break: 1 })];
47
+ if (node.type === "emphasis") return [new import_docx.TextRun({ italics: true, children: inline(node.children) })];
48
+ if (node.type === "strong") return [new import_docx.TextRun({ bold: true, children: inline(node.children) })];
49
+ if (node.type === "delete") return [new import_docx.TextRun({ strike: true, children: inline(node.children) })];
50
+ if (node.type === "mdiRuby") return [rawRun(rubyXml(node.base, node.ruby))];
51
+ if (node.type === "mdiTcy") return [rawRun(`<w:r><w:rPr><w:eastAsianLayout w:combine="1" w:combineBrackets="none"/></w:rPr><w:t>${xml(node.value)}</w:t></w:r>`)];
52
+ if ("children" in node) return inline(node.children);
53
+ return [];
54
+ });
55
+ }
56
+ function rubyXml(base, reading) {
57
+ const text = Array.isArray(reading) ? reading.join(".") : reading;
58
+ return `<w:ruby><w:rubyPr><w:rubyAlign w:val="center"/><w:hps w:val="12"/><w:hpsRaise w:val="18"/><w:hpsBaseText w:val="24"/></w:rubyPr><w:rt><w:r><w:t>${xml(text)}</w:t></w:r></w:rt><w:rubyBase><w:r><w:t>${xml(base)}</w:t></w:r></w:rubyBase></w:ruby>`;
59
+ }
60
+ function rawRun(xmlString) {
61
+ return import_docx.ImportedXmlComponent.fromXmlString(xmlString);
62
+ }
63
+ function xml(value) {
64
+ return value.replaceAll("&", "&amp;").replaceAll("<", "&lt;");
65
+ }
27
66
  // Annotate the CommonJS export names for ESM import in node:
28
67
  0 && (module.exports = {
29
- MDI_SPEC_VERSION
68
+ MDI_SPEC_VERSION,
69
+ mdiToDocx
30
70
  });
package/dist/index.d.cts CHANGED
@@ -1,3 +1,6 @@
1
+ import { Root } from 'mdast';
2
+
1
3
  declare const MDI_SPEC_VERSION = "2.0";
4
+ declare function mdiToDocx(tree: Root): Promise<Buffer>;
2
5
 
3
- export { MDI_SPEC_VERSION };
6
+ export { MDI_SPEC_VERSION, mdiToDocx };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,6 @@
1
+ import { Root } from 'mdast';
2
+
1
3
  declare const MDI_SPEC_VERSION = "2.0";
4
+ declare function mdiToDocx(tree: Root): Promise<Buffer>;
2
5
 
3
- export { MDI_SPEC_VERSION };
6
+ export { MDI_SPEC_VERSION, mdiToDocx };
package/dist/index.js CHANGED
@@ -1,5 +1,44 @@
1
1
  // src/index.ts
2
+ import { Document, HeadingLevel, ImportedXmlComponent, Packer, PageBreak, PageTextDirectionType, Paragraph, TextRun } from "docx";
2
3
  var MDI_SPEC_VERSION = "2.0";
4
+ async function mdiToDocx(tree) {
5
+ const children = tree.children.flatMap(block);
6
+ const vertical = tree.data?.frontmatter?.writingMode === "vertical";
7
+ const document = new Document({ sections: [{ children, properties: vertical ? { page: { textDirection: PageTextDirectionType.TOP_TO_BOTTOM_RIGHT_TO_LEFT } } : void 0 }], title: tree.data?.frontmatter?.title, creator: tree.data?.frontmatter?.author });
8
+ return Packer.toBuffer(document);
9
+ }
10
+ function block(node) {
11
+ if (node.type === "heading") return [new Paragraph({ heading: [HeadingLevel.HEADING_1, HeadingLevel.HEADING_2, HeadingLevel.HEADING_3, HeadingLevel.HEADING_4, HeadingLevel.HEADING_5, HeadingLevel.HEADING_6][node.depth - 1], children: inline(node.children) })];
12
+ if (node.type === "paragraph") return [new Paragraph({ children: inline(node.children) })];
13
+ if (node.type === "list") return node.children.flatMap((item) => item.children.filter((n) => n.type === "paragraph").map((n) => new Paragraph({ children: inline(n.children), bullet: { level: 0 } })));
14
+ if (node.type === "mdiPagebreak") return [new Paragraph({ children: [new PageBreak()] })];
15
+ if (node.type === "mdiBlank") return [new Paragraph("")];
16
+ return [];
17
+ }
18
+ function inline(nodes) {
19
+ return nodes.flatMap((node) => {
20
+ if (node.type === "text") return [new TextRun(node.value)];
21
+ if (node.type === "break" || node.type === "mdiBreak") return [new TextRun({ break: 1 })];
22
+ if (node.type === "emphasis") return [new TextRun({ italics: true, children: inline(node.children) })];
23
+ if (node.type === "strong") return [new TextRun({ bold: true, children: inline(node.children) })];
24
+ if (node.type === "delete") return [new TextRun({ strike: true, children: inline(node.children) })];
25
+ if (node.type === "mdiRuby") return [rawRun(rubyXml(node.base, node.ruby))];
26
+ if (node.type === "mdiTcy") return [rawRun(`<w:r><w:rPr><w:eastAsianLayout w:combine="1" w:combineBrackets="none"/></w:rPr><w:t>${xml(node.value)}</w:t></w:r>`)];
27
+ if ("children" in node) return inline(node.children);
28
+ return [];
29
+ });
30
+ }
31
+ function rubyXml(base, reading) {
32
+ const text = Array.isArray(reading) ? reading.join(".") : reading;
33
+ return `<w:ruby><w:rubyPr><w:rubyAlign w:val="center"/><w:hps w:val="12"/><w:hpsRaise w:val="18"/><w:hpsBaseText w:val="24"/></w:rubyPr><w:rt><w:r><w:t>${xml(text)}</w:t></w:r></w:rt><w:rubyBase><w:r><w:t>${xml(base)}</w:t></w:r></w:rubyBase></w:ruby>`;
34
+ }
35
+ function rawRun(xmlString) {
36
+ return ImportedXmlComponent.fromXmlString(xmlString);
37
+ }
38
+ function xml(value) {
39
+ return value.replaceAll("&", "&amp;").replaceAll("<", "&lt;");
40
+ }
3
41
  export {
4
- MDI_SPEC_VERSION
42
+ MDI_SPEC_VERSION,
43
+ mdiToDocx
5
44
  };
package/package.json CHANGED
@@ -1,8 +1,15 @@
1
1
  {
2
2
  "name": "@illusions-lab/mdi-to-docx",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
4
4
  "description": "Render MDI (.mdi) documents to DOCX",
5
5
  "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/illusions-lab/mdi-js.git",
9
+ "directory": "packages/to-docx"
10
+ },
11
+ "homepage": "https://illusions-lab.github.io/mdi-js/",
12
+ "bugs": "https://github.com/illusions-lab/mdi-js/issues",
6
13
  "type": "module",
7
14
  "main": "./dist/index.js",
8
15
  "types": "./dist/index.d.ts",
@@ -17,12 +24,18 @@
17
24
  ],
18
25
  "dependencies": {
19
26
  "docx": "^9.0.0",
20
- "mdast-util-mdi": "2.0.1"
27
+ "@types/mdast": "^4.0.0",
28
+ "mdast-util-mdi": "2.0.2"
21
29
  },
22
30
  "devDependencies": {
31
+ "remark-parse": "^11.0.0",
32
+ "unified": "^11.0.0",
33
+ "jszip": "^3.10.0",
23
34
  "tsup": "^8.3.0",
24
35
  "typescript": "^5.6.0",
25
- "vitest": "^2.1.0"
36
+ "vitest": "^2.1.0",
37
+ "@types/node": "^20.0.0",
38
+ "@illusions-lab/mdi-remark": "2.0.3"
26
39
  },
27
40
  "scripts": {
28
41
  "build": "tsup src/index.ts --format esm,cjs --dts",