fix(word): harden DOCX preprocessing and temp-file cleanup

- Make the HTML preprocessor code-block and inline-code aware so code

  examples containing <style> / <div> / comments are preserved.

- Strip all raw <div> tags (not just alignment attributes) to avoid

  malformed output from unmatched closing tags.

- Handle uppercase tags and single/unquoted attributes.

- Create temporary DOCX input files inside private mkdtemp directories

  instead of predictable names in the shared temp directory.

- Wrap batch DOCX preprocessing in try/catch so one unreadable file

  does not abort the entire batch.

- Add regression tests for the edge cases above.
This commit is contained in:
2026-06-30 19:57:40 +05:30
parent 94906a068a
commit 2cac075c0e
3 changed files with 98 additions and 15 deletions
+56
View File
@@ -58,4 +58,60 @@ Centered content
const output = WordTemplateExporter.preprocessMarkdownForWordExport(input);
expect(output).toBe(input);
});
test('preserves HTML artifacts inside fenced code blocks', () => {
const input = `# Title
\`\`\`
<style>body{}</style>
<!-- comment -->
<div align="center">text</div>
\`\`\`
After code.
`;
const output = WordTemplateExporter.preprocessMarkdownForWordExport(input);
expect(output).toContain('<style>body{}</style>');
expect(output).toContain('<!-- comment -->');
expect(output).toContain('<div align="center">text</div>');
expect(output).toContain('After code.');
});
test('preserves HTML artifacts inside inline code', () => {
const input = 'Use `<div align="center">` for alignment.';
const output = WordTemplateExporter.preprocessMarkdownForWordExport(input);
expect(output).toContain('`<div align="center">`');
});
test('handles uppercase tags and unquoted attributes', () => {
const input = `<DIV align=center>
Centered
</DIV>
`;
const output = WordTemplateExporter.preprocessMarkdownForWordExport(input);
expect(output).not.toContain('<DIV');
expect(output).not.toContain('</DIV>');
expect(output).toContain('Centered');
});
test('handles single-quoted attributes', () => {
const input = `<div align='right'>Right</div>`;
const output = WordTemplateExporter.preprocessMarkdownForWordExport(input);
expect(output).not.toContain('<div');
expect(output).not.toContain('</div>');
expect(output).toContain('Right');
});
test('removes non-alignment div tags without leaving malformed HTML', () => {
const input = `<div class="note">Note text</div>`;
const output = WordTemplateExporter.preprocessMarkdownForWordExport(input);
expect(output).not.toContain('<div');
expect(output).not.toContain('</div>');
expect(output).toContain('Note text');
});
test('returns non-string input unchanged', () => {
expect(WordTemplateExporter.preprocessMarkdownForWordExport(null)).toBeNull();
expect(WordTemplateExporter.preprocessMarkdownForWordExport(123)).toBe(123);
});
});