@illusions-lab/mdi-to-docx 2.0.14 → 2.0.16

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/dist/index.cjs CHANGED
@@ -148,14 +148,13 @@ async function mdiToDocx(tree, profile) {
148
148
  return import_docx.Packer.toBuffer(document);
149
149
  }
150
150
  function headingStyle(options, level) {
151
- const scale = [1.8, 1.55, 1.35, 1.2, 1.1, 1][level - 1] ?? 1;
152
151
  const before = [360, 300, 240, 180, 150, 120][level - 1] ?? 120;
153
152
  return {
154
153
  run: {
155
154
  font: options.typesetting.fontFamily,
156
155
  color: "000000",
157
156
  bold: true,
158
- size: Math.round(22 * scale)
157
+ size: headingFontSize(level)
159
158
  },
160
159
  paragraph: {
161
160
  spacing: { before, after: 120 },
@@ -165,6 +164,10 @@ function headingStyle(options, level) {
165
164
  }
166
165
  };
167
166
  }
167
+ function headingFontSize(level) {
168
+ const scale = [1.8, 1.55, 1.35, 1.2, 1.1, 1][level - 1] ?? 1;
169
+ return Math.round(22 * scale);
170
+ }
168
171
  function customHeadingStyle(options, level) {
169
172
  const style = headingStyle(options, level);
170
173
  return {
@@ -205,7 +208,7 @@ function block(node, options, context) {
205
208
  import_docx.HeadingLevel.HEADING_5,
206
209
  import_docx.HeadingLevel.HEADING_6
207
210
  ][node.depth - 1],
208
- children: inline(node.children, context)
211
+ children: inline(node.children, context, headingFontSize(node.depth))
209
212
  })
210
213
  ];
211
214
  if (node.type === "paragraph") return [paragraph(node.children, options, void 0, context)];
@@ -298,17 +301,17 @@ function paragraph(nodes, options, style, context) {
298
301
  children: [...prefix, ...inline(nodes, context)]
299
302
  });
300
303
  }
301
- function inline(nodes, context) {
304
+ function inline(nodes, context, rubyBaseTextSize = 24) {
302
305
  return nodes.flatMap((node) => {
303
306
  if (node.type === "text") return [new import_docx.TextRun(node.value)];
304
307
  if (node.type === "break" || node.type === "mdiBreak")
305
308
  return [new import_docx.TextRun({ break: 1 })];
306
309
  if (node.type === "emphasis")
307
- return [new import_docx.TextRun({ italics: true, children: inline(node.children, context) })];
310
+ return [new import_docx.TextRun({ italics: true, children: inline(node.children, context, rubyBaseTextSize) })];
308
311
  if (node.type === "strong")
309
- return [new import_docx.TextRun({ bold: true, children: inline(node.children, context) })];
312
+ return [new import_docx.TextRun({ bold: true, children: inline(node.children, context, rubyBaseTextSize) })];
310
313
  if (node.type === "delete")
311
- return [new import_docx.TextRun({ strike: true, children: inline(node.children, context) })];
314
+ return [new import_docx.TextRun({ strike: true, children: inline(node.children, context, rubyBaseTextSize) })];
312
315
  if (node.type === "inlineCode")
313
316
  return [new import_docx.TextRun({ text: node.value, font: "Courier New" })];
314
317
  if (node.type === "image")
@@ -317,14 +320,14 @@ function inline(nodes, context) {
317
320
  return [
318
321
  new import_docx.ExternalHyperlink({
319
322
  link: node.url,
320
- children: inline(node.children, context)
323
+ children: inline(node.children, context, rubyBaseTextSize)
321
324
  })
322
325
  ];
323
326
  if (node.type === "footnoteReference") {
324
327
  const id = context.footnoteIds.get(node.identifier);
325
328
  return id ? [new import_docx.FootnoteReferenceRun(id)] : [];
326
329
  }
327
- if (node.type === "mdiRuby") return [rawRun(rubyXml(node.base, node.ruby))];
330
+ if (node.type === "mdiRuby") return [rawRun(rubyXml(node.base, node.ruby, rubyBaseTextSize))];
328
331
  if (node.type === "mdiTcy")
329
332
  return [
330
333
  rawRun(
@@ -333,7 +336,7 @@ function inline(nodes, context) {
333
336
  )}</w:t></w:r>`
334
337
  )
335
338
  ];
336
- if ("children" in node) return inline(node.children, context);
339
+ if ("children" in node) return inline(node.children, context, rubyBaseTextSize);
337
340
  return [];
338
341
  });
339
342
  }
@@ -343,9 +346,11 @@ function inlineText(nodes) {
343
346
  function mmToTwips(mm) {
344
347
  return Math.round(mm / 25.4 * 1440);
345
348
  }
346
- function rubyXml(base, reading) {
349
+ function rubyXml(base, reading, baseTextSize = 24) {
347
350
  const text = Array.isArray(reading) ? reading.join(".") : reading;
348
- 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(
351
+ const rubySize = Math.max(10, Math.round(baseTextSize / 2));
352
+ const rubyRaise = baseTextSize === 24 ? 18 : Math.max(18, Math.round(baseTextSize * 0.8));
353
+ return `<w:ruby><w:rubyPr><w:rubyAlign w:val="center"/><w:hps w:val="${rubySize}"/><w:hpsRaise w:val="${rubyRaise}"/><w:hpsBaseText w:val="${baseTextSize}"/></w:rubyPr><w:rt><w:r><w:t>${xml(
349
354
  text
350
355
  )}</w:t></w:r></w:rt><w:rubyBase><w:r><w:t>${xml(
351
356
  base
package/dist/index.js CHANGED
@@ -146,14 +146,13 @@ async function mdiToDocx(tree, profile) {
146
146
  return Packer.toBuffer(document);
147
147
  }
148
148
  function headingStyle(options, level) {
149
- const scale = [1.8, 1.55, 1.35, 1.2, 1.1, 1][level - 1] ?? 1;
150
149
  const before = [360, 300, 240, 180, 150, 120][level - 1] ?? 120;
151
150
  return {
152
151
  run: {
153
152
  font: options.typesetting.fontFamily,
154
153
  color: "000000",
155
154
  bold: true,
156
- size: Math.round(22 * scale)
155
+ size: headingFontSize(level)
157
156
  },
158
157
  paragraph: {
159
158
  spacing: { before, after: 120 },
@@ -163,6 +162,10 @@ function headingStyle(options, level) {
163
162
  }
164
163
  };
165
164
  }
165
+ function headingFontSize(level) {
166
+ const scale = [1.8, 1.55, 1.35, 1.2, 1.1, 1][level - 1] ?? 1;
167
+ return Math.round(22 * scale);
168
+ }
166
169
  function customHeadingStyle(options, level) {
167
170
  const style = headingStyle(options, level);
168
171
  return {
@@ -203,7 +206,7 @@ function block(node, options, context) {
203
206
  HeadingLevel.HEADING_5,
204
207
  HeadingLevel.HEADING_6
205
208
  ][node.depth - 1],
206
- children: inline(node.children, context)
209
+ children: inline(node.children, context, headingFontSize(node.depth))
207
210
  })
208
211
  ];
209
212
  if (node.type === "paragraph") return [paragraph(node.children, options, void 0, context)];
@@ -296,17 +299,17 @@ function paragraph(nodes, options, style, context) {
296
299
  children: [...prefix, ...inline(nodes, context)]
297
300
  });
298
301
  }
299
- function inline(nodes, context) {
302
+ function inline(nodes, context, rubyBaseTextSize = 24) {
300
303
  return nodes.flatMap((node) => {
301
304
  if (node.type === "text") return [new TextRun(node.value)];
302
305
  if (node.type === "break" || node.type === "mdiBreak")
303
306
  return [new TextRun({ break: 1 })];
304
307
  if (node.type === "emphasis")
305
- return [new TextRun({ italics: true, children: inline(node.children, context) })];
308
+ return [new TextRun({ italics: true, children: inline(node.children, context, rubyBaseTextSize) })];
306
309
  if (node.type === "strong")
307
- return [new TextRun({ bold: true, children: inline(node.children, context) })];
310
+ return [new TextRun({ bold: true, children: inline(node.children, context, rubyBaseTextSize) })];
308
311
  if (node.type === "delete")
309
- return [new TextRun({ strike: true, children: inline(node.children, context) })];
312
+ return [new TextRun({ strike: true, children: inline(node.children, context, rubyBaseTextSize) })];
310
313
  if (node.type === "inlineCode")
311
314
  return [new TextRun({ text: node.value, font: "Courier New" })];
312
315
  if (node.type === "image")
@@ -315,14 +318,14 @@ function inline(nodes, context) {
315
318
  return [
316
319
  new ExternalHyperlink({
317
320
  link: node.url,
318
- children: inline(node.children, context)
321
+ children: inline(node.children, context, rubyBaseTextSize)
319
322
  })
320
323
  ];
321
324
  if (node.type === "footnoteReference") {
322
325
  const id = context.footnoteIds.get(node.identifier);
323
326
  return id ? [new FootnoteReferenceRun(id)] : [];
324
327
  }
325
- if (node.type === "mdiRuby") return [rawRun(rubyXml(node.base, node.ruby))];
328
+ if (node.type === "mdiRuby") return [rawRun(rubyXml(node.base, node.ruby, rubyBaseTextSize))];
326
329
  if (node.type === "mdiTcy")
327
330
  return [
328
331
  rawRun(
@@ -331,7 +334,7 @@ function inline(nodes, context) {
331
334
  )}</w:t></w:r>`
332
335
  )
333
336
  ];
334
- if ("children" in node) return inline(node.children, context);
337
+ if ("children" in node) return inline(node.children, context, rubyBaseTextSize);
335
338
  return [];
336
339
  });
337
340
  }
@@ -341,9 +344,11 @@ function inlineText(nodes) {
341
344
  function mmToTwips(mm) {
342
345
  return Math.round(mm / 25.4 * 1440);
343
346
  }
344
- function rubyXml(base, reading) {
347
+ function rubyXml(base, reading, baseTextSize = 24) {
345
348
  const text = Array.isArray(reading) ? reading.join(".") : reading;
346
- 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(
349
+ const rubySize = Math.max(10, Math.round(baseTextSize / 2));
350
+ const rubyRaise = baseTextSize === 24 ? 18 : Math.max(18, Math.round(baseTextSize * 0.8));
351
+ return `<w:ruby><w:rubyPr><w:rubyAlign w:val="center"/><w:hps w:val="${rubySize}"/><w:hpsRaise w:val="${rubyRaise}"/><w:hpsBaseText w:val="${baseTextSize}"/></w:rubyPr><w:rt><w:r><w:t>${xml(
347
352
  text
348
353
  )}</w:t></w:r></w:rt><w:rubyBase><w:r><w:t>${xml(
349
354
  base
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@illusions-lab/mdi-to-docx",
3
- "version": "2.0.14",
3
+ "version": "2.0.16",
4
4
  "description": "Render MDI (.mdi) documents to DOCX",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -25,8 +25,8 @@
25
25
  "dependencies": {
26
26
  "docx": "^9.0.0",
27
27
  "@types/mdast": "^4.0.0",
28
- "@illusions-lab/mdi-export-profile": "2.0.9",
29
- "mdast-util-mdi": "2.0.13"
28
+ "@illusions-lab/mdi-export-profile": "2.0.11",
29
+ "mdast-util-mdi": "2.0.15"
30
30
  },
31
31
  "devDependencies": {
32
32
  "remark-parse": "^11.0.0",
@@ -36,7 +36,7 @@
36
36
  "typescript": "^5.6.0",
37
37
  "vitest": "^2.1.0",
38
38
  "@types/node": "^20.0.0",
39
- "@illusions-lab/mdi-remark": "2.0.14"
39
+ "@illusions-lab/mdi-remark": "2.0.16"
40
40
  },
41
41
  "scripts": {
42
42
  "build": "tsup src/index.ts --format esm,cjs --dts",