fix(word): strip HTML style blocks and alignment divs from DOCX input

Pre-process markdown before Word/DOCX export to remove raw HTML artifacts

(<style> blocks, HTML comments, and <div align=...> tags) that were

visible in the generated document. Applies to single and batch DOCX exports

via both Pandoc and WordTemplateExporter paths.
This commit is contained in:
2026-06-30 13:57:29 +05:30
parent d705cfc30b
commit e72b863362
3 changed files with 139 additions and 5 deletions
+22 -1
View File
@@ -15,6 +15,26 @@ class WordTemplateExporter {
this.pageSettings = pageSettings; // Page size and orientation settings
}
/**
* Strip HTML artifacts that Pandoc / Word cannot render and that would
* otherwise appear as visible text in DOCX output.
* Removes HTML comments, <style> blocks, and alignment <div> tags.
*/
static preprocessMarkdownForWordExport(markdown) {
if (typeof markdown !== 'string') return markdown;
return (
markdown
// HTML comments (multi-line)
.replace(/<!--[\s\S]*?-->/g, '')
// <style> blocks (case-insensitive, multi-line)
.replace(/<style\b[\s\S]*?<\/style>/gi, '')
// Opening <div align="..."> tags
.replace(/<div\b[^>]*?\balign\s*=\s*["'][^"']*["'][^>]*>/gi, '')
// Closing </div> tags
.replace(/<\/div\s*>/gi, '')
);
}
/**
* Convert markdown to Word document using template
*/
@@ -33,7 +53,8 @@ class WordTemplateExporter {
}
// Parse markdown and generate Word XML
const newContentXml = this.markdownToWordXml(markdownContent);
const cleanedContent = WordTemplateExporter.preprocessMarkdownForWordExport(markdownContent);
const newContentXml = this.markdownToWordXml(cleanedContent);
// Insert new content after the specified start page
const modifiedXml = this.insertContentAfterPage(documentXml, newContentXml, this.startPage);