Skip to content

Commit

Permalink
feat: 导出 DOCX 不再依赖 Python
Browse files Browse the repository at this point in the history
  • Loading branch information
LeafYeeXYZ committed Dec 8, 2024
1 parent 8441f04 commit b70e90a
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 52 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ mdp example.md --out=D:/example.pdf
# 从当前工作目录下的 example.md 文件生成 PDF 和 HTML 文件
mdp example.md --outputHTML
# 从当前工作目录下的 example.md 文件生成 PDF 和 DOCX 文件
pip install pdf2docx # 仅生成 DOCX 文件时需要安装 pdf2docx, 只需安装一次
mdp example.md --outputDOCX
```

Expand All @@ -95,7 +94,7 @@ mdp example.md --outputDOCX
| `--out=xxx` | 输出文件相对路径<br>默认为源文件路径的同名 `PDF` 文件 |
| `--theme=xxx` | 论文模板, 默认为 `aps` (`Acta Psychologica Sinica`)<br>暂时没有其他模板, 欢迎贡献 |
| `--outputHTML` | 输出 `HTML` 文件, 默认不输出 |
| `--outputDOCX` | 输出 `DOCX` 文件, 默认不输出<br>**须先通过 `python` 安装依赖 `pdf2docx`**<br>使用时推荐开启 `--hideFooter` 参数 |
| `--outputDOCX` | 输出 `DOCX` 文件, 默认不输出<br>**导出后样式可能无法完全保留, 请自行调整** |

# 模板说明
`/theme/theme.ts` 中的 `MarkdownnPaperTheme` 接口定义了模板的样式, 按照类似于 `aps` 文件夹的结构可自定义模板; 模板可以提供自定义功能
Expand All @@ -117,6 +116,7 @@ mdp example.md --outputDOCX
同上

# 更新日志
- `2.4.0` (2024-12-08): 导出 `DOCX` 文件时, 不再依赖 `Python`
- `2.3.0` (2024-08-31): 支持数学公式
- `2.2.0` (2024-08-26): 半重构, 优化导入导出, 优化文档
- `2.1.1` (2024-07-12): 修复字体错误
Expand Down
Binary file modified bun.lockb
Binary file not shown.
17 changes: 0 additions & 17 deletions lib/docx.ts

This file was deleted.

39 changes: 7 additions & 32 deletions lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { PDFOptions } from 'puppeteer'
import markedKatex from 'marked-katex-extension'
// @ts-ignore
import katexCss from 'katex/dist/katex.css' with { type: 'text' }
import { asBlob } from 'html-docx-js-typescript'

/** 应用参数 */
class MarkdownPaperOptions {
Expand Down Expand Up @@ -101,8 +102,6 @@ async function renderMarkdown(
const raw = await fs.readFile(options.src, { encoding: 'utf-8' })
// 生成 html 文件
const html = await mdToHtml(raw, options.theme, path.basename(options.src).replace('.md', ''))
// 保存 html 文件
options.outputHTML && await fs.writeFile(options.out.replace('.pdf', '.html'), html)
// 保存 pdf 文件
await htmlToPdf(
html.replace(/<img src="(.+?)"/g, (match, p1) => {
Expand All @@ -120,8 +119,13 @@ async function renderMarkdown(
options.theme.pdfOptions,
options.theme.script
)
// 保存 html 文件
options.outputHTML && await fs.writeFile(options.out.replace('.pdf', '.html'), html)
// 保存 docx 文件
options.outputDOCX && await pdfToDocx(options.out)
const docx = await asBlob(html)
const docxBuffer = new Uint8Array(docx instanceof Blob ? await docx.arrayBuffer() : docx.buffer)
options.outputDOCX && await fs.writeFile(options.out.replace('.pdf', '.docx'), docxBuffer)
options.outputDOCX && console.warn('导出的 DOCX 文件可能存在格式丢失, 请手动调整\n')
}

/**
Expand All @@ -141,34 +145,6 @@ async function htmlToPdf(html: string, dist: string, options: PDFOptions, script
await browser.close()
}

/**
* 把 pdf 转换为 docx
* @param pdfPath pdf 文件绝对路径
*/
function pdfToDocx(pdfPath: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
const worker = new Worker(new URL('docx.ts', import.meta.url).href)
worker.onmessage = (e) => {
switch (e.data) {
case 'success': {
console.log('')
resolve()
break
}
case 'error': {
reject(Error('生成 docx 文件失败'))
break
}
default: {
reject(Error('调用 python 时发生未知错误'))
break
}
}
}
worker.postMessage(pdfPath)
})
}

/**
* 把 markdown 转换为 html
* @param md markdown 字符串
Expand Down Expand Up @@ -210,7 +186,6 @@ export {
MarkdownPaperOptions,
renderMarkdown,
htmlToPdf,
pdfToDocx,
mdToHtml,
APS
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "markdown-paper",
"type": "module",
"version": "2.3.1",
"version": "2.4.0",
"author": {
"name": "LeafYeeXYZ",
"email": "[email protected]"
Expand All @@ -23,6 +23,7 @@
"typescript": "^5.4.5"
},
"dependencies": {
"html-docx-js-typescript": "^0.1.5",
"marked": "^12.0.2",
"marked-katex-extension": "^5.1.3",
"puppeteer": "^22.15.0"
Expand Down

0 comments on commit b70e90a

Please sign in to comment.