250 lines
6.9 KiB
JavaScript
250 lines
6.9 KiB
JavaScript
import {
|
|
Document,
|
|
Packer,
|
|
Paragraph,
|
|
TextRun,
|
|
HeadingLevel,
|
|
Table,
|
|
TableRow,
|
|
TableCell,
|
|
WidthType,
|
|
BorderStyle
|
|
} from 'docx';
|
|
import { saveAs } from 'file-saver';
|
|
import { marked } from 'marked';
|
|
|
|
/**
|
|
* Convert inline tokens (strong, em, del, codespan, link, text) to docx TextRuns
|
|
*/
|
|
function inlineTokensToRuns(tokens, options = {}) {
|
|
if (!tokens || !tokens.length) return [];
|
|
const runs = [];
|
|
|
|
for (const token of tokens) {
|
|
if (token.type === 'text') {
|
|
runs.push(new TextRun({
|
|
text: token.text,
|
|
bold: options.bold,
|
|
italics: options.italics,
|
|
strike: options.strike,
|
|
color: options.color
|
|
}));
|
|
} else if (token.type === 'strong') {
|
|
runs.push(...inlineTokensToRuns(token.tokens, { ...options, bold: true }));
|
|
} else if (token.type === 'em') {
|
|
runs.push(...inlineTokensToRuns(token.tokens, { ...options, italics: true }));
|
|
} else if (token.type === 'del') {
|
|
runs.push(...inlineTokensToRuns(token.tokens, { ...options, strike: true }));
|
|
} else if (token.type === 'codespan') {
|
|
runs.push(new TextRun({
|
|
text: token.text,
|
|
font: 'Consolas',
|
|
shading: { fill: 'F4F4F6' },
|
|
color: 'D03554'
|
|
}));
|
|
} else if (token.type === 'link') {
|
|
runs.push(new TextRun({
|
|
text: token.text || token.href,
|
|
underline: {},
|
|
color: '0969DA'
|
|
}));
|
|
} else if (token.tokens && token.tokens.length) {
|
|
runs.push(...inlineTokensToRuns(token.tokens, options));
|
|
} else if (token.raw) {
|
|
runs.push(new TextRun({
|
|
text: token.raw,
|
|
bold: options.bold,
|
|
italics: options.italics,
|
|
strike: options.strike
|
|
}));
|
|
}
|
|
}
|
|
|
|
return runs;
|
|
}
|
|
|
|
/**
|
|
* Convert GFM Markdown string to a native .docx File
|
|
*/
|
|
export async function exportMarkdownToDocx(markdown, filename = 'document.docx') {
|
|
if (!markdown || !markdown.trim()) {
|
|
markdown = '# Empty Document\n\nNo content provided.';
|
|
}
|
|
|
|
const tokens = marked.lexer(markdown);
|
|
const children = [];
|
|
|
|
for (const token of tokens) {
|
|
switch (token.type) {
|
|
case 'heading': {
|
|
const headingLevels = {
|
|
1: HeadingLevel.HEADING_1,
|
|
2: HeadingLevel.HEADING_2,
|
|
3: HeadingLevel.HEADING_3,
|
|
4: HeadingLevel.HEADING_4,
|
|
5: HeadingLevel.HEADING_5,
|
|
6: HeadingLevel.HEADING_6,
|
|
};
|
|
|
|
const runs = token.tokens ? inlineTokensToRuns(token.tokens) : [new TextRun({ text: token.text, bold: true })];
|
|
children.push(new Paragraph({
|
|
children: runs,
|
|
heading: headingLevels[token.depth] || HeadingLevel.HEADING_1,
|
|
spacing: { before: 240, after: 120 }
|
|
}));
|
|
break;
|
|
}
|
|
|
|
case 'paragraph': {
|
|
const runs = token.tokens ? inlineTokensToRuns(token.tokens) : [new TextRun({ text: token.text })];
|
|
children.push(new Paragraph({
|
|
children: runs,
|
|
spacing: { before: 60, after: 120 }
|
|
}));
|
|
break;
|
|
}
|
|
|
|
case 'list': {
|
|
const isOrdered = token.ordered;
|
|
token.items.forEach((item, idx) => {
|
|
const itemText = item.text || '';
|
|
const runs = item.tokens ? inlineTokensToRuns(item.tokens) : [new TextRun({ text: itemText })];
|
|
|
|
if (isOrdered) {
|
|
children.push(new Paragraph({
|
|
children: [new TextRun({ text: `${idx + 1}. ` }), ...runs],
|
|
spacing: { before: 40, after: 40 },
|
|
indent: { left: 360 }
|
|
}));
|
|
} else {
|
|
children.push(new Paragraph({
|
|
children: runs,
|
|
bullet: { level: 0 },
|
|
spacing: { before: 40, after: 40 }
|
|
}));
|
|
}
|
|
});
|
|
break;
|
|
}
|
|
|
|
case 'code': {
|
|
const lines = token.text.split('\n');
|
|
lines.forEach(line => {
|
|
children.push(new Paragraph({
|
|
children: [
|
|
new TextRun({
|
|
text: line || ' ',
|
|
font: 'Consolas',
|
|
color: '24292F',
|
|
size: 20
|
|
})
|
|
],
|
|
shading: { fill: 'F6F8FA' },
|
|
spacing: { before: 0, after: 0 }
|
|
}));
|
|
});
|
|
// Extra space after code block
|
|
children.push(new Paragraph({ spacing: { after: 120 } }));
|
|
break;
|
|
}
|
|
|
|
case 'blockquote': {
|
|
const bqRuns = token.tokens ? inlineTokensToRuns(token.tokens) : [new TextRun({ text: token.text, italics: true })];
|
|
children.push(new Paragraph({
|
|
children: bqRuns,
|
|
indent: { left: 720 },
|
|
border: {
|
|
left: {
|
|
color: '2AA198',
|
|
size: 24,
|
|
style: BorderStyle.SINGLE,
|
|
space: 120
|
|
}
|
|
},
|
|
spacing: { before: 120, after: 120 }
|
|
}));
|
|
break;
|
|
}
|
|
|
|
case 'table': {
|
|
const rows = [];
|
|
// Header row
|
|
if (token.header && token.header.length) {
|
|
rows.push(new TableRow({
|
|
tableHeader: true,
|
|
children: token.header.map(h => new TableCell({
|
|
shading: { fill: 'F6F8FA' },
|
|
children: [
|
|
new Paragraph({
|
|
children: inlineTokensToRuns(h.tokens, { bold: true }),
|
|
spacing: { before: 80, after: 80 }
|
|
})
|
|
]
|
|
}))
|
|
}));
|
|
}
|
|
|
|
// Table body rows
|
|
if (token.rows && token.rows.length) {
|
|
token.rows.forEach(row => {
|
|
rows.push(new TableRow({
|
|
children: row.map(cell => new TableCell({
|
|
children: [
|
|
new Paragraph({
|
|
children: inlineTokensToRuns(cell.tokens),
|
|
spacing: { before: 60, after: 60 }
|
|
})
|
|
]
|
|
}))
|
|
}));
|
|
});
|
|
}
|
|
|
|
if (rows.length) {
|
|
children.push(new Table({
|
|
rows,
|
|
width: { size: 100, type: WidthType.PERCENTAGE }
|
|
}));
|
|
children.push(new Paragraph({ spacing: { after: 120 } }));
|
|
}
|
|
break;
|
|
}
|
|
|
|
case 'hr': {
|
|
children.push(new Paragraph({
|
|
border: {
|
|
bottom: {
|
|
color: 'D0D7DE',
|
|
size: 12,
|
|
style: BorderStyle.SINGLE
|
|
}
|
|
},
|
|
spacing: { before: 240, after: 240 }
|
|
}));
|
|
break;
|
|
}
|
|
|
|
default:
|
|
if (token.text) {
|
|
children.push(new Paragraph({
|
|
children: [new TextRun({ text: token.text })],
|
|
spacing: { before: 60, after: 60 }
|
|
}));
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
const doc = new Document({
|
|
sections: [{
|
|
properties: {},
|
|
children: children.length ? children : [new Paragraph({ text: 'AboveTheMark Document' })]
|
|
}]
|
|
});
|
|
|
|
const blob = await Packer.toBlob(doc);
|
|
const safeFilename = filename.endsWith('.docx') ? filename : `${filename}.docx`;
|
|
saveAs(blob, safeFilename);
|
|
return safeFilename;
|
|
}
|