markdown-paper 2.3.1 → 2.5.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 +12 -2
- package/lib/main.ts +7 -32
- package/package.json +2 -1
- package/theme/aps/aps.css +5 -0
- package/lib/docx.ts +0 -17
package/README.md
CHANGED
|
@@ -53,6 +53,10 @@ $$
|
|
|
53
53
|
- 文献2
|
|
54
54
|
- 文献3
|
|
55
55
|
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
(上面的是分页符)
|
|
59
|
+
|
|
56
60
|
##### 附录
|
|
57
61
|
```
|
|
58
62
|
|
|
@@ -86,7 +90,6 @@ mdp example.md --out=D:/example.pdf
|
|
|
86
90
|
# 从当前工作目录下的 example.md 文件生成 PDF 和 HTML 文件
|
|
87
91
|
mdp example.md --outputHTML
|
|
88
92
|
# 从当前工作目录下的 example.md 文件生成 PDF 和 DOCX 文件
|
|
89
|
-
pip install pdf2docx # 仅生成 DOCX 文件时需要安装 pdf2docx, 只需安装一次
|
|
90
93
|
mdp example.md --outputDOCX
|
|
91
94
|
```
|
|
92
95
|
|
|
@@ -95,9 +98,10 @@ mdp example.md --outputDOCX
|
|
|
95
98
|
| `--out=xxx` | 输出文件相对路径<br>默认为源文件路径的同名 `PDF` 文件 |
|
|
96
99
|
| `--theme=xxx` | 论文模板, 默认为 `aps` (`Acta Psychologica Sinica`)<br>暂时没有其他模板, 欢迎贡献 |
|
|
97
100
|
| `--outputHTML` | 输出 `HTML` 文件, 默认不输出 |
|
|
98
|
-
| `--outputDOCX` | 输出 `DOCX` 文件, 默认不输出<br
|
|
101
|
+
| `--outputDOCX` | 输出 `DOCX` 文件, 默认不输出<br>**导出后样式可能无法完全保留, 请自行调整** |
|
|
99
102
|
|
|
100
103
|
# 模板说明
|
|
104
|
+
|
|
101
105
|
`/theme/theme.ts` 中的 `MarkdownnPaperTheme` 接口定义了模板的样式, 按照类似于 `aps` 文件夹的结构可自定义模板; 模板可以提供自定义功能
|
|
102
106
|
|
|
103
107
|
模板制作完成后, 在 `/lib/main.ts` 中导入并添加到 `class MarkdownPaperOptions -> constructor -> case '--theme':` 中, 并在下方添加使用文档即可
|
|
@@ -105,7 +109,9 @@ mdp example.md --outputDOCX
|
|
|
105
109
|
推荐所有主题的文档和编写格式都尽量与 `aps` 主题保持一致
|
|
106
110
|
|
|
107
111
|
## APS 模板
|
|
112
|
+
|
|
108
113
|
### 额外命令行参数
|
|
114
|
+
|
|
109
115
|
| 参数 | 说明 |
|
|
110
116
|
| :---: | :---: |
|
|
111
117
|
| `--showTitle` | 在页眉显示文件名, 默认不显示 |
|
|
@@ -114,9 +120,13 @@ mdp example.md --outputDOCX
|
|
|
114
120
|
| `--enPunctuation` | 将正文中的中文标点符号替换为英文标点符号, 默认不替换<br>仅替换 `PDF` 和 `DOCX` 文件 |
|
|
115
121
|
|
|
116
122
|
### 编写格式
|
|
123
|
+
|
|
117
124
|
同上
|
|
118
125
|
|
|
119
126
|
# 更新日志
|
|
127
|
+
|
|
128
|
+
- `2.5.0` (2025-02-07): 支持分页符
|
|
129
|
+
- `2.4.0` (2024-12-08): 导出 `DOCX` 文件时, 不再依赖 `Python`
|
|
120
130
|
- `2.3.0` (2024-08-31): 支持数学公式
|
|
121
131
|
- `2.2.0` (2024-08-26): 半重构, 优化导入导出, 优化文档
|
|
122
132
|
- `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.5.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/theme/aps/aps.css
CHANGED
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
|
-
}
|