mirror of
https://github.com/amitwh/markdown-converter.git
synced 2026-08-24 07:20:16 +05:30
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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user