Skip to content

Commit 1669966

Browse files
committed
Refactor
1 parent dcf2bdb commit 1669966

2 files changed

Lines changed: 11 additions & 19 deletions

File tree

src/mdast-util-to-docx.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import type {
3030
FootnoteRegistry,
3131
NodeBuilder,
3232
NodeBuilders,
33-
NumberingRegistry,
3433
RemarkDocxPlugin,
3534
Writeable,
3635
} from "./types";
@@ -81,14 +80,18 @@ type OrderedListFormat = {
8180
text: string;
8281
};
8382

83+
type NumberingRegistry = {
84+
createId: () => string;
85+
getIds: () => string[];
86+
};
8487
const createNumberingRegistry = (): NumberingRegistry => {
8588
let counter = 1;
8689

8790
return {
88-
create: () => {
91+
createId: () => {
8992
return `${ORDERED_LIST_REF}-${counter++}`;
9093
},
91-
toConfig: () => {
94+
getIds: () => {
9295
return Array.from(
9396
{ length: counter },
9497
(_, i) => `${ORDERED_LIST_REF}-${i}`,
@@ -240,7 +243,7 @@ export const mdastToDocx = async (
240243
indent: 0,
241244
definition: definition,
242245
footnote,
243-
numbering,
246+
orderedListId: numbering.createId,
244247
};
245248

246249
const sections: DocxContent[][] = [[]];
@@ -289,7 +292,7 @@ export const mdastToDocx = async (
289292
.map((s) => ({ children: s as DocxChild[] })),
290293
footnotes: footnote.toConfig(),
291294
numbering: {
292-
config: numbering.toConfig().map((ref) => ({
295+
config: numbering.getIds().map((ref) => ({
293296
reference: ref,
294297
levels,
295298
})),
@@ -386,7 +389,7 @@ const buildList: NodeBuilder<"list"> = ({ children, ordered }, ctx) => {
386389

387390
const reference =
388391
isTopLevel && ordered
389-
? ctx.numbering.create()
392+
? ctx.orderedListId()
390393
: ctx.list?.reference || ORDERED_LIST_REF;
391394

392395
return ctx.render(children, {

src/types.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import type {
2-
ILevelsOptions,
3-
Paragraph,
4-
ParagraphChild,
5-
Table,
6-
TableOfContents,
7-
} from "docx";
1+
import type { Paragraph, ParagraphChild, Table, TableOfContents } from "docx";
82
import type * as mdast from "mdast";
93
import type { GetDefinition } from "mdast-util-definitions";
104

@@ -46,11 +40,6 @@ export type FootnoteRegistry = {
4640
};
4741
};
4842

49-
export type NumberingRegistry = {
50-
create: () => string;
51-
toConfig: () => string[];
52-
};
53-
5443
export type Context = Readonly<{
5544
render: (node: readonly mdast.RootContent[], ctx?: Context) => DocxContent[];
5645
/**
@@ -76,7 +65,7 @@ export type Context = Readonly<{
7665
/**
7766
* @internal
7867
*/
79-
numbering: NumberingRegistry;
68+
orderedListId: () => string;
8069
}>;
8170

8271
export type NodeBuilder<T extends string> = (

0 commit comments

Comments
 (0)