-
-
Save jaxalo/bd23a8db85ddc7afc5c9ca668b13c898 to your computer and use it in GitHub Desktop.
Move TOC when converting html to docx after generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const originalhtml = ` | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
</head> | |
<h1>Title 1</h1> | |
<p>Some stuff 1</p> | |
<h2>Subtitle 1</h2> | |
pandoc-toc | |
<p>Stuff2</p> | |
</html> | |
`; | |
public static async moveTableOfContent(docxFile: Buffer): Promise<string> { | |
const zip = new JSZip(); | |
await zip.loadAsync(docxFile); | |
const docXml = await zip.file('word/document.xml').async('string'); | |
const docxWithRepositionedToc = this.replacePandocTocBracketWithToc(docXml); | |
zip.file('word/document.xml', docxWithRepositionedToc); | |
const zipFile = await zip.generateAsync({ type: 'nodebuffer' }); | |
const docxPath = await write(zipFile, { extension: '.docx' }); | |
return docxPath; | |
} | |
private static replacePandocTocBracketWithToc(docXml: string): string { | |
// eslint-disable-next-line max-len | |
const pandocTocBracketRegex = /<w:p><w:pPr><w:pStyle w:val="[a-zA-a ]+" \/><\/w:pPr><w:r><w:t xml:space="preserve">pandoc-toc<\/w:t><\/w:r><\/w:p>/g; | |
const tocRegex = new RegExp(`(<w:sdt>)(.*?)(</w:sdt>)`, 'g'); | |
const tocIndocXml = tocRegex.exec(docXml)[0]; | |
const pandocTocBracketXml = pandocTocBracketRegex.exec(docXml)[0]; | |
const docXmlWithMovedToc = docXml.replace(tocIndocXml, '').replace(pandocTocBracketXml, tocIndocXml); | |
return docXmlWithMovedToc; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment