markdown-paper 2.3.1 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/lib/main.ts +7 -32
- package/package.json +2 -1
- package/lib/docx.ts +0 -17
package/README.md
CHANGED
|
@@ -86,7 +86,6 @@ mdp example.md --out=D:/example.pdf
|
|
|
86
86
|
# 从当前工作目录下的 example.md 文件生成 PDF 和 HTML 文件
|
|
87
87
|
mdp example.md --outputHTML
|
|
88
88
|
# 从当前工作目录下的 example.md 文件生成 PDF 和 DOCX 文件
|
|
89
|
-
pip install pdf2docx # 仅生成 DOCX 文件时需要安装 pdf2docx, 只需安装一次
|
|
90
89
|
mdp example.md --outputDOCX
|
|
91
90
|
```
|
|
92
91
|
|
|
@@ -95,7 +94,7 @@ mdp example.md --outputDOCX
|
|
|
95
94
|
| `--out=xxx` | 输出文件相对路径<br>默认为源文件路径的同名 `PDF` 文件 |
|
|
96
95
|
| `--theme=xxx` | 论文模板, 默认为 `aps` (`Acta Psychologica Sinica`)<br>暂时没有其他模板, 欢迎贡献 |
|
|
97
96
|
| `--outputHTML` | 输出 `HTML` 文件, 默认不输出 |
|
|
98
|
-
| `--outputDOCX` | 输出 `DOCX` 文件, 默认不输出<br
|
|
97
|
+
| `--outputDOCX` | 输出 `DOCX` 文件, 默认不输出<br>**导出后样式可能无法完全保留, 请自行调整** |
|
|
99
98
|
|
|
100
99
|
# 模板说明
|
|
101
100
|
`/theme/theme.ts` 中的 `MarkdownnPaperTheme` 接口定义了模板的样式, 按照类似于 `aps` 文件夹的结构可自定义模板; 模板可以提供自定义功能
|
|
@@ -117,6 +116,7 @@ mdp example.md --outputDOCX
|
|
|
117
116
|
同上
|
|
118
117
|
|
|
119
118
|
# 更新日志
|
|
119
|
+
- `2.4.0` (2024-12-08): 导出 `DOCX` 文件时, 不再依赖 `Python`
|
|
120
120
|
- `2.3.0` (2024-08-31): 支持数学公式
|
|
121
121
|
- `2.2.0` (2024-08-26): 半重构, 优化导入导出, 优化文档
|
|
122
122
|
- `2.1.1` (2024-07-12): 修复字体错误
|
package/lib/main.ts
CHANGED
|
@@ -9,6 +9,7 @@ import type { PDFOptions } from 'puppeteer'
|
|
|
9
9
|
import markedKatex from 'marked-katex-extension'
|
|
10
10
|
// @ts-ignore
|
|
11
11
|
import katexCss from 'katex/dist/katex.css' with { type: 'text' }
|
|
12
|
+
import { asBlob } from 'html-docx-js-typescript'
|
|
12
13
|
|
|
13
14
|
/** 应用参数 */
|
|
14
15
|
class MarkdownPaperOptions {
|
|
@@ -101,8 +102,6 @@ async function renderMarkdown(
|
|
|
101
102
|
const raw = await fs.readFile(options.src, { encoding: 'utf-8' })
|
|
102
103
|
// 生成 html 文件
|
|
103
104
|
const html = await mdToHtml(raw, options.theme, path.basename(options.src).replace('.md', ''))
|
|
104
|
-
// 保存 html 文件
|
|
105
|
-
options.outputHTML && await fs.writeFile(options.out.replace('.pdf', '.html'), html)
|
|
106
105
|
// 保存 pdf 文件
|
|
107
106
|
await htmlToPdf(
|
|
108
107
|
html.replace(/<img src="(.+?)"/g, (match, p1) => {
|
|
@@ -120,8 +119,13 @@ async function renderMarkdown(
|
|
|
120
119
|
options.theme.pdfOptions,
|
|
121
120
|
options.theme.script
|
|
122
121
|
)
|
|
122
|
+
// 保存 html 文件
|
|
123
|
+
options.outputHTML && await fs.writeFile(options.out.replace('.pdf', '.html'), html)
|
|
123
124
|
// 保存 docx 文件
|
|
124
|
-
|
|
125
|
+
const docx = await asBlob(html)
|
|
126
|
+
const docxBuffer = new Uint8Array(docx instanceof Blob ? await docx.arrayBuffer() : docx.buffer)
|
|
127
|
+
options.outputDOCX && await fs.writeFile(options.out.replace('.pdf', '.docx'), docxBuffer)
|
|
128
|
+
options.outputDOCX && console.warn('导出的 DOCX 文件可能存在格式丢失, 请手动调整\n')
|
|
125
129
|
}
|
|
126
130
|
|
|
127
131
|
/**
|
|
@@ -141,34 +145,6 @@ async function htmlToPdf(html: string, dist: string, options: PDFOptions, script
|
|
|
141
145
|
await browser.close()
|
|
142
146
|
}
|
|
143
147
|
|
|
144
|
-
/**
|
|
145
|
-
* 把 pdf 转换为 docx
|
|
146
|
-
* @param pdfPath pdf 文件绝对路径
|
|
147
|
-
*/
|
|
148
|
-
function pdfToDocx(pdfPath: string): Promise<void> {
|
|
149
|
-
return new Promise<void>((resolve, reject) => {
|
|
150
|
-
const worker = new Worker(new URL('docx.ts', import.meta.url).href)
|
|
151
|
-
worker.onmessage = (e) => {
|
|
152
|
-
switch (e.data) {
|
|
153
|
-
case 'success': {
|
|
154
|
-
console.log('')
|
|
155
|
-
resolve()
|
|
156
|
-
break
|
|
157
|
-
}
|
|
158
|
-
case 'error': {
|
|
159
|
-
reject(Error('生成 docx 文件失败'))
|
|
160
|
-
break
|
|
161
|
-
}
|
|
162
|
-
default: {
|
|
163
|
-
reject(Error('调用 python 时发生未知错误'))
|
|
164
|
-
break
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
worker.postMessage(pdfPath)
|
|
169
|
-
})
|
|
170
|
-
}
|
|
171
|
-
|
|
172
148
|
/**
|
|
173
149
|
* 把 markdown 转换为 html
|
|
174
150
|
* @param md markdown 字符串
|
|
@@ -210,7 +186,6 @@ export {
|
|
|
210
186
|
MarkdownPaperOptions,
|
|
211
187
|
renderMarkdown,
|
|
212
188
|
htmlToPdf,
|
|
213
|
-
pdfToDocx,
|
|
214
189
|
mdToHtml,
|
|
215
190
|
APS
|
|
216
191
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "markdown-paper",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.4.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "LeafYeeXYZ",
|
|
7
7
|
"email": "xiaoyezi@leafyee.xyz"
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"typescript": "^5.4.5"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
+
"html-docx-js-typescript": "^0.1.5",
|
|
26
27
|
"marked": "^12.0.2",
|
|
27
28
|
"marked-katex-extension": "^5.1.3",
|
|
28
29
|
"puppeteer": "^22.15.0"
|
package/lib/docx.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
declare var self: Worker
|
|
2
|
-
|
|
3
|
-
import { $ } from 'bun'
|
|
4
|
-
|
|
5
|
-
self.onmessage = async (e) => {
|
|
6
|
-
const pdfPath = e.data as string
|
|
7
|
-
await $`pdf2docx convert ${pdfPath}`
|
|
8
|
-
.then(() => {
|
|
9
|
-
postMessage('success')
|
|
10
|
-
})
|
|
11
|
-
.catch(() => {
|
|
12
|
-
postMessage('error')
|
|
13
|
-
})
|
|
14
|
-
.finally(() => {
|
|
15
|
-
process.exit(0)
|
|
16
|
-
})
|
|
17
|
-
}
|