Skip to content

Commit a135ac4

Browse files
committed
Refactor
1 parent 9cfef5d commit a135ac4

2 files changed

Lines changed: 50 additions & 36 deletions

File tree

src/mdast-util-to-docx.ts

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import {
1717
type IPropertiesOptions,
1818
sectionPageSizeDefaults,
1919
sectionMarginDefaults,
20+
type IRunOptions,
21+
type IParagraphOptions,
2022
} from "docx";
2123
import type * as mdast from "mdast";
2224
import { warnOnce } from "./utils";
@@ -30,6 +32,7 @@ import type {
3032
NodeBuilders,
3133
NumberingRegistry,
3234
RemarkDocxPlugin,
35+
Writeable,
3336
} from "./types";
3437

3538
const CONTENT_WIDTH =
@@ -305,37 +308,40 @@ const buildParagraph: NodeBuilder<"paragraph"> = ({ children }, ctx) => {
305308
const list = ctx.list;
306309
const nodes = ctx.render(children);
307310

308-
if (list && list.checked != null) {
309-
nodes.unshift(
310-
new CheckBox({
311-
checked: list.checked,
312-
checkedState: { value: "2611" },
313-
uncheckedState: { value: "2610" },
314-
}),
315-
);
316-
}
317-
return new Paragraph({
311+
const options: Writeable<IParagraphOptions> = {
318312
children: nodes,
319-
indent:
320-
ctx.indent > 0
321-
? {
322-
start: convertInchesToTwip(INDENT * ctx.indent),
323-
}
324-
: undefined,
325-
...(list &&
326-
(list.ordered
327-
? {
328-
numbering: {
329-
reference: list.reference,
330-
level: list.level,
331-
},
332-
}
333-
: {
334-
bullet: {
335-
level: list.level,
336-
},
337-
})),
338-
});
313+
};
314+
315+
if (ctx.indent > 0) {
316+
options.indent = {
317+
start: convertInchesToTwip(INDENT * ctx.indent),
318+
};
319+
}
320+
321+
if (list) {
322+
if (list.checked) {
323+
nodes.unshift(
324+
new CheckBox({
325+
checked: list.checked,
326+
checkedState: { value: "2611" },
327+
uncheckedState: { value: "2610" },
328+
}),
329+
);
330+
}
331+
332+
if (list.ordered) {
333+
options.numbering = {
334+
reference: list.reference,
335+
level: list.level,
336+
};
337+
} else {
338+
options.bullet = {
339+
level: list.level,
340+
};
341+
}
342+
}
343+
344+
return new Paragraph(options);
339345
};
340346

341347
const buildHeading: NodeBuilder<"heading"> = ({ children, depth }, ctx) => {
@@ -443,16 +449,17 @@ const buildTable: NodeBuilder<"table"> = ({ children, align }, ctx) => {
443449
};
444450

445451
const buildText: NodeBuilder<"text"> = ({ value }, { deco }) => {
446-
return new TextRun({
452+
const options: Writeable<IRunOptions> = {
447453
text: value,
448454
bold: deco.bold,
449455
italics: deco.italic,
450456
strike: deco.strike,
451-
...(deco.link && {
452-
color: "#0563c1",
453-
underline: { type: "single" },
454-
}),
455-
});
457+
};
458+
if (deco.link) {
459+
options.color = "#0563c1";
460+
options.underline = { type: "single" };
461+
}
462+
return new TextRun(options);
456463
};
457464

458465
const buildEmphasis: NodeBuilder<"emphasis"> = ({ children }, ctx) => {

src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ import type {
88
import type * as mdast from "mdast";
99
import type { GetDefinition } from "mdast-util-definitions";
1010

11+
/**
12+
* @internal
13+
*/
14+
export type Writeable<T> = {
15+
-readonly [key in keyof T]: T[key];
16+
};
17+
1118
export type DocxChild = Paragraph | Table | TableOfContents;
1219
export type DocxContent = DocxChild | ParagraphChild;
1320

0 commit comments

Comments
 (0)