markdown-paper 2.2.3 → 2.2.5

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/lib/main.ts CHANGED
@@ -3,7 +3,7 @@ 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 } from '../theme/aps/aps'
6
+ import { APS, baseAPS } from '../theme/aps/aps'
7
7
  import type { MarkdownPaperTheme } from '../theme/theme'
8
8
  import type { PDFOptions } from 'puppeteer'
9
9
 
@@ -114,7 +114,8 @@ async function renderMarkdown(
114
114
  }
115
115
  }),
116
116
  options.out,
117
- options.theme.pdfOptions
117
+ options.theme.pdfOptions,
118
+ options.theme.script
118
119
  )
119
120
  // 保存 docx 文件
120
121
  options.outputDOCX && await pdfToDocx(options.out)
@@ -125,11 +126,14 @@ async function renderMarkdown(
125
126
  * @param html html 字符串, 图片为 base64
126
127
  * @param dist pdf 文件绝对路径
127
128
  * @param options pdf 参数, 无需设置路径
129
+ * @param script 在网页中要执行的函数
128
130
  */
129
- async function htmlToPdf(html: string, dist: string, options: PDFOptions): Promise<void> {
131
+ async function htmlToPdf(html: string, dist: string, options: PDFOptions, script: () => void): Promise<void> {
130
132
  const browser = await puppeteer.launch()
131
133
  const page = await browser.newPage()
132
134
  await page.setContent(html)
135
+ // 执行脚本
136
+ await page.evaluate(script)
133
137
  await page.pdf({ path: dist, ...options })
134
138
  await browser.close()
135
139
  }
@@ -164,7 +168,6 @@ function pdfToDocx(pdfPath: string): Promise<void> {
164
168
 
165
169
  /**
166
170
  * 把 markdown 转换为 html
167
- * @important 浏览器环境中可用
168
171
  * @param md markdown 字符串
169
172
  * @param theme 论文模板
170
173
  * @param pageTitle 页面标题
@@ -203,7 +206,7 @@ export {
203
206
  htmlToPdf,
204
207
  pdfToDocx,
205
208
  mdToHtml,
206
- APS
209
+ baseAPS
207
210
  }
208
211
  export type {
209
212
  MarkdownPaperTheme
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "markdown-paper",
3
3
  "type": "module",
4
- "version": "2.2.3",
4
+ "version": "2.2.5",
5
5
  "author": {
6
6
  "name": "LeafYeeXYZ",
7
7
  "email": "xiaoyezi@leafyee.xyz"
package/theme/aps/aps.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import type { MarkdownPaperTheme } from '../theme'
2
2
  import type { PDFOptions } from 'puppeteer'
3
+ import fs from 'node:fs'
4
+ import path from 'node:path'
3
5
 
4
6
  export class APS implements MarkdownPaperTheme {
5
7
 
@@ -29,145 +31,7 @@ export class APS implements MarkdownPaperTheme {
29
31
  })
30
32
 
31
33
  // css
32
- this.css = `
33
- * {
34
- font-family: 'Times', 'Times New Roman', '宋体', 'SimSun', '华文宋体', 'STSong'; /* 所有数字和英文字体都用 Times New Roman */
35
- line-height: 1.55em; /* 1.5倍行距 */
36
- margin: 0;
37
- }
38
-
39
- h1 { /* 中文题目: 二号黑体 */
40
- font-size: 29px;
41
- font-weight: normal;
42
- font-family: '黑体', 'SimHei', '华文黑体', 'STHeiti';
43
- text-align: center;
44
- margin-bottom: 9px;
45
- }
46
- .author { /* 作者姓名: 四号仿宋 */
47
- font-size: 18px;
48
- font-weight: normal;
49
- font-family: '仿宋', 'Fangsong', '华文仿宋', 'STFangsong';
50
- text-align: center;
51
- margin-bottom: 3px;
52
- }
53
- .school { /* 作者单位: 小五宋体 */
54
- font-size: 12px;
55
- text-align: center;
56
- margin-bottom: 38px;
57
- }
58
- .abstract { /* 摘要和关键词: 五号宋体 */
59
- font-size: 14px;
60
- text-align: justify;
61
- padding: 0 28px;
62
- &::before {
63
- content: '摘 要';
64
- font-weight: normal;
65
- font-family: '黑体', 'SimHei', '华文黑体', 'STHeiti';
66
- display: inline-block;
67
- margin-right: 14px;
68
- }
69
- }
70
- .keywords {
71
- font-size: 14px;
72
- margin-bottom: 31px;
73
- padding: 0 28px;
74
- &::before {
75
- content: '关键词';
76
- font-weight: normal;
77
- font-family: '黑体', 'SimHei', '华文黑体', 'STHeiti';
78
- display: inline-block;
79
- margin-right: 14px;
80
- }
81
- }
82
-
83
- h2 { /* 一级标题: 四号宋体 */
84
- font-size: 18px;
85
- font-weight: normal;
86
- margin: 7px 0;
87
- }
88
- h3 { /* 二级标题: 五号黑体 */
89
- font-size: 14px;
90
- font-weight: normal;
91
- font-family: '黑体', 'SimHei', '华文黑体', 'STHeiti';
92
- margin: 5px 0;
93
- }
94
- h4 { /* 三级标题: 五号黑体 */
95
- font-size: 14px;
96
- font-weight: normal;
97
- font-family: '黑体', 'SimHei', '华文黑体', 'STHeiti';
98
- margin: 3px 0;
99
- }
100
-
101
- p { /* 正文: 五号宋体 */
102
- font-size: 14px;
103
- text-indent: 24px;
104
- text-align: justify;
105
- }
106
-
107
- h5 { /* "参考文献": 五号黑体 */
108
- font-size: 14px;
109
- font-weight: normal;
110
- font-family: '黑体', 'SimHei', '华文黑体', 'STHeiti';
111
- text-align: center;
112
- margin-bottom: 7px;
113
- margin-top: 20px;
114
- }
115
- ul, ol { /* 参考文献的项目: 小五号宋体 */
116
- list-style-type: none;
117
- padding: 0;
118
- & > li {
119
- font-size: 12px;
120
- margin: 6px 0;
121
- text-align: justify;
122
- text-indent: -24px;
123
- padding-left: 24px;
124
- }
125
- & a {
126
- text-decoration: none;
127
- color: black;
128
- }
129
- }
130
-
131
- img {
132
- display: block;
133
- max-width: 100%;
134
- margin: 0 auto;
135
- margin-top: 10px;
136
- }
137
-
138
- blockquote, blockquote > p { /* 图片和表格的标题: 小五号宋体 */
139
- font-size: 12px;
140
- font-weight: normal;
141
- text-align: center;
142
- margin: 0;
143
- }
144
- blockquote > p { margin: 6px 0; }
145
- table { /* 表格: 小五号宋体 */
146
- font-size: 12px;
147
- position: relative;
148
- border-top: 1px solid black;
149
- border-bottom: 1px solid black;
150
- width: 100%;
151
- max-width: 100%;
152
- margin: 0 auto;
153
- margin-bottom: 10px;
154
- & th, & td {
155
- font-weight: normal;
156
- }
157
- & thead::after { /* 用来做三线表中间的横线 */
158
- content: '';
159
- display: block;
160
- position: absolute;
161
- border-top: 1px solid #00000060;
162
- width: 100%;
163
- }
164
- }
165
-
166
- b, strong { /* 加粗按黑体处理 */
167
- font-weight: normal;
168
- font-family: '黑体', 'SimHei', '华文黑体', 'STHeiti';
169
- }
170
- `
34
+ this.css = fs.readFileSync(path.resolve(import.meta.dirname, 'aps.css'), 'utf-8')
171
35
 
172
36
  // preParseMarkdown
173
37
  this.preParseMarkdown = async (md: string): Promise<string> => {
@@ -255,4 +119,168 @@ export class APS implements MarkdownPaperTheme {
255
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>`,
256
120
  }
257
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
+ }
258
286
  }