markdown-paper 2.2.5 → 2.3.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 CHANGED
@@ -23,10 +23,24 @@
23
23
 
24
24
  正文
25
25
 
26
+ #### 1.1.2 数学公式
27
+
28
+ 段内数学公式: $c = \pm\sqrt{a^2 + b^2}$
29
+
30
+ 单独数学公式:
31
+
32
+ $$
33
+ c = \pm\sqrt{a^2 + b^2}
34
+ $$
35
+
36
+ #### 1.1.3 图片
37
+
26
38
  ![](图片路径)
27
39
 
28
40
  > 图片标题
29
41
 
42
+ #### 1.1.4 表格
43
+
30
44
  > 表格标题
31
45
 
32
46
  | 表头1 | 表头2 |
@@ -42,7 +56,7 @@
42
56
  ##### 附录
43
57
  ```
44
58
 
45
- > 详见下方的模板说明
59
+ > 数学公式语法详见 [https://katex.org](https://katex.org)
46
60
 
47
61
  ## 2 安装 `Bun`
48
62
 
@@ -60,7 +74,7 @@ bun add -g markdown-paper
60
74
 
61
75
  运行 `mdp` 命令以使用 `MarkdownPaper` 命令行工具从 `Markdown` 文件生成论文
62
76
 
63
- 如果您不熟悉命令行工具, 可以尝试我的另一个项目 [EasyPaper](https://github.com/LeafYeeXYZ/EasyPaper), 它基于 `MarkdownPaper` 并提供了图形界面
77
+ 如果您不熟悉命令行工具, 可以尝试我的另一个项目 [EasyPaper](https://github.com/LeafYeeXYZ/EasyPaper), 它实现了大部分 `MarkdownPaper` 的功能并提供了图形界面
64
78
 
65
79
  ```bash
66
80
  # 查看帮助信息
@@ -100,38 +114,11 @@ mdp example.md --outputDOCX
100
114
  | `--enPunctuation` | 将正文中的中文标点符号替换为英文标点符号, 默认不替换<br>仅替换 `PDF` 和 `DOCX` 文件 |
101
115
 
102
116
  ### 编写格式
103
- ```markdown
104
- # 中文标题
105
- #author# 作者信息
106
- #school# 单位信息
107
- #abstract# 摘要内容
108
- #keywords# 关键词内容
109
-
110
- ## 1 一级标题
111
- ### 1.1 二级标题
112
- #### 1.1.1 三级标题
113
- 正文
114
-
115
- ![](图片路径)
116
-
117
- > 图片标题
118
-
119
- > 表格标题
120
-
121
- | 表头1 | 表头2 |
122
- | :---: | :---: |
123
- | 内容1 | 内容2 |
124
-
125
- ##### 参考文献
126
- - 文献1
127
- - 文献2
128
- - 文献3
129
-
130
- ##### 附录
131
- ```
117
+ 同上
132
118
 
133
119
  # 更新日志
134
- - `2.2.0` (2024-08-26): 优化项目导入导出内容, 优化文档
120
+ - `2.3.0` (2024-08-31): 支持数学公式
121
+ - `2.2.0` (2024-08-26): 半重构, 优化导入导出, 优化文档
135
122
  - `2.1.1` (2024-07-12): 修复字体错误
136
123
  - `2.1.0` (2024-06-26): 支持 `MacOS` 系统, 改为在线加载字体
137
124
  - `2.0.0` (2024-06-20): 重构代码, 完善模板功能
package/bun.lockb CHANGED
Binary file
package/lib/main.ts CHANGED
@@ -3,9 +3,12 @@ import puppeteer from 'puppeteer'
3
3
  import fs from 'node:fs/promises'
4
4
  import path from 'node:path'
5
5
  import { readFileSync } from 'node:fs'
6
- import { APS, baseAPS } from '../theme/aps/aps'
6
+ import { APS } from '../theme/aps/aps'
7
7
  import type { MarkdownPaperTheme } from '../theme/theme'
8
8
  import type { PDFOptions } from 'puppeteer'
9
+ import markedKatex from 'marked-katex-extension'
10
+ // @ts-ignore
11
+ import katexCss from 'katex/dist/katex.css' with { type: 'text' }
9
12
 
10
13
  /** 应用参数 */
11
14
  class MarkdownPaperOptions {
@@ -178,6 +181,8 @@ async function mdToHtml(
178
181
  theme: MarkdownPaperTheme,
179
182
  pageTitle: string = 'MarkdownPaper'
180
183
  ): Promise<string> {
184
+ // 设置 marked
185
+ marked.use(markedKatex({ throwOnError: false }))
181
186
  // 预处理 markdown
182
187
  let html = await theme.preParseMarkdown(md)
183
188
  // 转换 markdown 为 html
@@ -188,6 +193,7 @@ async function mdToHtml(
188
193
  <meta charset="UTF-8">
189
194
  <title>${pageTitle}</title>
190
195
  <style>${theme.css}</style>
196
+ <style>\n${katexCss}\n</style>
191
197
  </head>
192
198
  <body>
193
199
  ${await marked(html)}
@@ -206,7 +212,7 @@ export {
206
212
  htmlToPdf,
207
213
  pdfToDocx,
208
214
  mdToHtml,
209
- baseAPS
215
+ APS
210
216
  }
211
217
  export type {
212
218
  MarkdownPaperTheme
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "markdown-paper",
3
3
  "type": "module",
4
- "version": "2.2.5",
4
+ "version": "2.3.0",
5
5
  "author": {
6
6
  "name": "LeafYeeXYZ",
7
7
  "email": "xiaoyezi@leafyee.xyz"
@@ -24,6 +24,7 @@
24
24
  },
25
25
  "dependencies": {
26
26
  "marked": "^12.0.2",
27
+ "marked-katex-extension": "^5.1.1",
27
28
  "puppeteer": "^22.9.0"
28
29
  }
29
30
  }
package/theme/aps/aps.ts CHANGED
@@ -119,168 +119,4 @@ export class APS implements MarkdownPaperTheme {
119
119
  footerTemplate: hideFooter ? `<div></div>` : `<div style="font-size: 9px; font-family: 'SimSun'; color: #333; padding: 5px; margin: 0 auto;">第 <span class="pageNumber"></span> 页 / 共 <span class="totalPages"></span> 页</div>`,
120
120
  }
121
121
  }
122
- }
123
-
124
- export const baseAPS: {
125
- css: string
126
- preParseMarkdown: (md: string) => Promise<string>
127
- preParseHTML: (html: string) => Promise<string>
128
- } = {
129
- css: `
130
- * {
131
- font-family: 'Times', 'Times New Roman', '宋体', 'SimSun', '华文宋体', 'STSong'; /* 所有数字和英文字体都用 Times New Roman */
132
- line-height: 1.55em; /* 1.5倍行距 */
133
- margin: 0;
134
- }
135
-
136
- h1 { /* 中文题目: 二号黑体 */
137
- font-size: 29px;
138
- font-weight: normal;
139
- font-family: '黑体', 'SimHei', '华文黑体', 'STHeiti';
140
- text-align: center;
141
- margin-bottom: 9px;
142
- }
143
- .author { /* 作者姓名: 四号仿宋 */
144
- font-size: 18px;
145
- font-weight: normal;
146
- font-family: '仿宋', 'Fangsong', '华文仿宋', 'STFangsong';
147
- text-align: center;
148
- margin-bottom: 3px;
149
- }
150
- .school { /* 作者单位: 小五宋体 */
151
- font-size: 12px;
152
- text-align: center;
153
- margin-bottom: 38px;
154
- }
155
- .abstract { /* 摘要和关键词: 五号宋体 */
156
- font-size: 14px;
157
- text-align: justify;
158
- padding: 0 28px;
159
- &::before {
160
- content: '摘 要';
161
- font-weight: normal;
162
- font-family: '黑体', 'SimHei', '华文黑体', 'STHeiti';
163
- display: inline-block;
164
- margin-right: 14px;
165
- }
166
- }
167
- .keywords {
168
- font-size: 14px;
169
- margin-bottom: 31px;
170
- padding: 0 28px;
171
- &::before {
172
- content: '关键词';
173
- font-weight: normal;
174
- font-family: '黑体', 'SimHei', '华文黑体', 'STHeiti';
175
- display: inline-block;
176
- margin-right: 14px;
177
- }
178
- }
179
-
180
- h2 { /* 一级标题: 四号宋体 */
181
- font-size: 18px;
182
- font-weight: normal;
183
- margin: 7px 0;
184
- }
185
- h3 { /* 二级标题: 五号黑体 */
186
- font-size: 14px;
187
- font-weight: normal;
188
- font-family: '黑体', 'SimHei', '华文黑体', 'STHeiti';
189
- margin: 5px 0;
190
- }
191
- h4 { /* 三级标题: 五号黑体 */
192
- font-size: 14px;
193
- font-weight: normal;
194
- font-family: '黑体', 'SimHei', '华文黑体', 'STHeiti';
195
- margin: 3px 0;
196
- }
197
-
198
- p { /* 正文: 五号宋体 */
199
- font-size: 14px;
200
- text-indent: 24px;
201
- text-align: justify;
202
- }
203
-
204
- h5 { /* "参考文献": 五号黑体 */
205
- font-size: 14px;
206
- font-weight: normal;
207
- font-family: '黑体', 'SimHei', '华文黑体', 'STHeiti';
208
- text-align: center;
209
- margin-bottom: 7px;
210
- margin-top: 20px;
211
- }
212
- ul, ol { /* 参考文献的项目: 小五号宋体 */
213
- list-style-type: none;
214
- padding: 0;
215
- & > li {
216
- font-size: 12px;
217
- margin: 6px 0;
218
- text-align: justify;
219
- text-indent: -24px;
220
- padding-left: 24px;
221
- }
222
- & a {
223
- text-decoration: none;
224
- color: black;
225
- }
226
- }
227
-
228
- img {
229
- display: block;
230
- max-width: 100%;
231
- margin: 0 auto;
232
- margin-top: 10px;
233
- }
234
-
235
- blockquote, blockquote > p { /* 图片和表格的标题: 小五号宋体 */
236
- font-size: 12px;
237
- font-weight: normal;
238
- text-align: center;
239
- margin: 0;
240
- }
241
- blockquote > p { margin: 6px 0; }
242
- table { /* 表格: 小五号宋体 */
243
- font-size: 12px;
244
- position: relative;
245
- border-top: 1px solid black;
246
- border-bottom: 1px solid black;
247
- width: 100%;
248
- max-width: 100%;
249
- margin: 0 auto;
250
- margin-bottom: 10px;
251
- & th, & td {
252
- font-weight: normal;
253
- }
254
- & thead::after { /* 用来做三线表中间的横线 */
255
- content: '';
256
- display: block;
257
- position: absolute;
258
- border-top: 1px solid #00000060;
259
- width: 100%;
260
- }
261
- }
262
-
263
- b, strong { /* 加粗按黑体处理 */
264
- font-weight: normal;
265
- font-family: '黑体', 'SimHei', '华文黑体', 'STHeiti';
266
- }
267
- `,
268
- preParseMarkdown: async (md: string): Promise<string> => {
269
- // 作者
270
- md = md.replace(/#author# (.*)/mg, '<div class="author">$1</div>')
271
- // 单位
272
- md = md.replace(/#school# (.*)/mg, '<div class="school">$1</div>')
273
- // 关键词
274
- md = md.replace(/#keywords# (.*)/mg, '<div class="keywords">$1</div>')
275
- // 摘要
276
- md = md.replace(/#abstract# (.*)/mg, '<div class="abstract">$1</div>')
277
- // 返回处理后的字符串
278
- return md
279
- },
280
- preParseHTML: async (html: string): Promise<string> => {
281
- // 把包裹图片的 p 标签去掉
282
- html = html.replace(/<p><img (.*?)><\/p>/g, '<img $1>')
283
- // 返回处理后的字符串
284
- return html
285
- }
286
122
  }
package/theme/theme.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { PDFOptions } from 'puppeteer-core'
1
+ import type { PDFOptions } from 'puppeteer'
2
2
 
3
3
  export interface MarkdownPaperTheme {
4
4
  /**