index.ts 923 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import {
  2. Document,
  3. Packer,
  4. // Paragraph,
  5. // TableOfContents
  6. } from "docx"
  7. import globalConfig from "./globalConfig"
  8. import { handleElement, getTitle, getConfig } from "./handleElement"
  9. export async function exportDomToWord(domSelector: string = ".content") {
  10. const dom = document.querySelector(domSelector)
  11. if (!dom) {
  12. console.warn("未找到内容区域")
  13. return
  14. }
  15. const paragraphs = await handleElement(dom)
  16. // 构建 Word 文档
  17. const doc = new Document({
  18. title: getTitle(),
  19. ...globalConfig,
  20. ...getConfig(),
  21. sections: [{
  22. children: [
  23. // 暂时不使用目录
  24. // new Paragraph("文档目录如下:"),
  25. // new TableOfContents("目录", {
  26. // hyperlink: true,
  27. // headingStyleRange: "1-3", // 收录 Heading 1~3
  28. // }),
  29. ...paragraphs
  30. ]
  31. }],
  32. })
  33. const blob = await Packer.toBlob(doc)
  34. return blob
  35. }