|
|
@@ -51,9 +51,13 @@ export const markdownToDocxBlob = async (markdown: string, title?: string): Prom
|
|
|
const { Document, Packer, Paragraph, TextRun, HeadingLevel, Table, TableRow, TableCell, WidthType, AlignmentType, UnderlineType } =
|
|
|
(await import('docx')) as any;
|
|
|
|
|
|
+ // 上面解构出的是 any 值,不能直接当类型名使用(TS2749),这里给出等价的类型别名
|
|
|
+ type DocxRun = any;
|
|
|
+ type DocxBlock = any;
|
|
|
+
|
|
|
// 行内 token 递归渲染为 TextRun 序列(加粗/斜体/删除线/行内代码/链接)
|
|
|
- const renderInline = (tokens: any[], style: { bold?: boolean; italics?: boolean; strike?: boolean } = {}): TextRun[] => {
|
|
|
- const runs: TextRun[] = [];
|
|
|
+ const renderInline = (tokens: any[], style: { bold?: boolean; italics?: boolean; strike?: boolean } = {}): DocxRun[] => {
|
|
|
+ const runs: DocxRun[] = [];
|
|
|
for (const t of tokens || []) {
|
|
|
switch (t.type) {
|
|
|
case 'text':
|
|
|
@@ -93,7 +97,7 @@ export const markdownToDocxBlob = async (markdown: string, title?: string): Prom
|
|
|
return runs.length ? runs : [new TextRun({ text: '' })];
|
|
|
};
|
|
|
|
|
|
- const children: (Paragraph | Table)[] = [];
|
|
|
+ const children: DocxBlock[] = [];
|
|
|
|
|
|
if (title) {
|
|
|
children.push(new Paragraph({ text: title, heading: HeadingLevel.TITLE, alignment: AlignmentType.CENTER }));
|
|
|
@@ -134,7 +138,7 @@ export const markdownToDocxBlob = async (markdown: string, title?: string): Prom
|
|
|
}
|
|
|
case 'code': {
|
|
|
const lines = (t.text || '').split('\n');
|
|
|
- const runs: TextRun[] = [];
|
|
|
+ const runs: DocxRun[] = [];
|
|
|
lines.forEach((line: string, i: number) => {
|
|
|
runs.push(new TextRun({ text: line, font: { name: 'Consolas' } }));
|
|
|
if (i < lines.length - 1) runs.push(new TextRun({ break: 1 }));
|