Skip to content

Commit

Permalink
feat: 支持数学公式
Browse files Browse the repository at this point in the history
  • Loading branch information
LeafYeeXYZ committed Aug 31, 2024
1 parent 98ad82a commit 0949982
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 200 deletions.
51 changes: 19 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,24 @@

正文

#### 1.1.2 数学公式

段内数学公式: $c = \pm\sqrt{a^2 + b^2}$

单独数学公式:

$$
c = \pm\sqrt{a^2 + b^2}
$$

#### 1.1.3 图片

![](图片路径)

> 图片标题

#### 1.1.4 表格

> 表格标题

| 表头1 | 表头2 |
Expand All @@ -42,7 +56,7 @@
##### 附录
```

> 详见下方的模板说明
> 数学公式语法详见 [https://katex.org](https://katex.org)
## 2 安装 `Bun`

Expand All @@ -60,7 +74,7 @@ bun add -g markdown-paper

运行 `mdp` 命令以使用 `MarkdownPaper` 命令行工具从 `Markdown` 文件生成论文

如果您不熟悉命令行工具, 可以尝试我的另一个项目 [EasyPaper](https://github.com/LeafYeeXYZ/EasyPaper), 它基于 `MarkdownPaper` 并提供了图形界面
如果您不熟悉命令行工具, 可以尝试我的另一个项目 [EasyPaper](https://github.com/LeafYeeXYZ/EasyPaper), 它实现了大部分 `MarkdownPaper` 的功能并提供了图形界面

```bash
# 查看帮助信息
Expand Down Expand Up @@ -100,38 +114,11 @@ mdp example.md --outputDOCX
| `--enPunctuation` | 将正文中的中文标点符号替换为英文标点符号, 默认不替换<br>仅替换 `PDF``DOCX` 文件 |

### 编写格式
```markdown
# 中文标题
#author# 作者信息
#school# 单位信息
#abstract# 摘要内容
#keywords# 关键词内容

## 1 一级标题
### 1.1 二级标题
#### 1.1.1 三级标题
正文

![](图片路径)

> 图片标题

> 表格标题

| 表头1 | 表头2 |
| :---: | :---: |
| 内容1 | 内容2 |

##### 参考文献
- 文献1
- 文献2
- 文献3

##### 附录
```
同上

# 更新日志
- `2.2.0` (2024-08-26): 优化项目导入导出内容, 优化文档
- `2.3.0` (2024-08-31): 支持数学公式
- `2.2.0` (2024-08-26): 半重构, 优化导入导出, 优化文档
- `2.1.1` (2024-07-12): 修复字体错误
- `2.1.0` (2024-06-26): 支持 `MacOS` 系统, 改为在线加载字体
- `2.0.0` (2024-06-20): 重构代码, 完善模板功能
Expand Down
Binary file modified bun.lockb
Binary file not shown.
10 changes: 8 additions & 2 deletions lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import puppeteer from 'puppeteer'
import fs from 'node:fs/promises'
import path from 'node:path'
import { readFileSync } from 'node:fs'
import { APS, baseAPS } from '../theme/aps/aps'
import { APS } from '../theme/aps/aps'
import type { MarkdownPaperTheme } from '../theme/theme'
import type { PDFOptions } from 'puppeteer'
import markedKatex from 'marked-katex-extension'
// @ts-ignore
import katexCss from 'katex/dist/katex.css' with { type: 'text' }

/** 应用参数 */
class MarkdownPaperOptions {
Expand Down Expand Up @@ -178,6 +181,8 @@ async function mdToHtml(
theme: MarkdownPaperTheme,
pageTitle: string = 'MarkdownPaper'
): Promise<string> {
// 设置 marked
marked.use(markedKatex({ throwOnError: false }))
// 预处理 markdown
let html = await theme.preParseMarkdown(md)
// 转换 markdown 为 html
Expand All @@ -188,6 +193,7 @@ async function mdToHtml(
<meta charset="UTF-8">
<title>${pageTitle}</title>
<style>${theme.css}</style>
<style>\n${katexCss}\n</style>
</head>
<body>
${await marked(html)}
Expand All @@ -206,7 +212,7 @@ export {
htmlToPdf,
pdfToDocx,
mdToHtml,
baseAPS
APS
}
export type {
MarkdownPaperTheme
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.2.5",
"version": "2.3.0",
"author": {
"name": "LeafYeeXYZ",
"email": "[email protected]"
Expand All @@ -24,6 +24,7 @@
},
"dependencies": {
"marked": "^12.0.2",
"marked-katex-extension": "^5.1.1",
"puppeteer": "^22.9.0"
}
}
164 changes: 0 additions & 164 deletions theme/aps/aps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,168 +119,4 @@ export class APS implements MarkdownPaperTheme {
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>`,
}
}
}

export const baseAPS: {
css: string
preParseMarkdown: (md: string) => Promise<string>
preParseHTML: (html: string) => Promise<string>
} = {
css: `
* {
font-family: 'Times', 'Times New Roman', '宋体', 'SimSun', '华文宋体', 'STSong'; /* 所有数字和英文字体都用 Times New Roman */
line-height: 1.55em; /* 1.5倍行距 */
margin: 0;
}
h1 { /* 中文题目: 二号黑体 */
font-size: 29px;
font-weight: normal;
font-family: '黑体', 'SimHei', '华文黑体', 'STHeiti';
text-align: center;
margin-bottom: 9px;
}
.author { /* 作者姓名: 四号仿宋 */
font-size: 18px;
font-weight: normal;
font-family: '仿宋', 'Fangsong', '华文仿宋', 'STFangsong';
text-align: center;
margin-bottom: 3px;
}
.school { /* 作者单位: 小五宋体 */
font-size: 12px;
text-align: center;
margin-bottom: 38px;
}
.abstract { /* 摘要和关键词: 五号宋体 */
font-size: 14px;
text-align: justify;
padding: 0 28px;
&::before {
content: '摘 要';
font-weight: normal;
font-family: '黑体', 'SimHei', '华文黑体', 'STHeiti';
display: inline-block;
margin-right: 14px;
}
}
.keywords {
font-size: 14px;
margin-bottom: 31px;
padding: 0 28px;
&::before {
content: '关键词';
font-weight: normal;
font-family: '黑体', 'SimHei', '华文黑体', 'STHeiti';
display: inline-block;
margin-right: 14px;
}
}
h2 { /* 一级标题: 四号宋体 */
font-size: 18px;
font-weight: normal;
margin: 7px 0;
}
h3 { /* 二级标题: 五号黑体 */
font-size: 14px;
font-weight: normal;
font-family: '黑体', 'SimHei', '华文黑体', 'STHeiti';
margin: 5px 0;
}
h4 { /* 三级标题: 五号黑体 */
font-size: 14px;
font-weight: normal;
font-family: '黑体', 'SimHei', '华文黑体', 'STHeiti';
margin: 3px 0;
}
p { /* 正文: 五号宋体 */
font-size: 14px;
text-indent: 24px;
text-align: justify;
}
h5 { /* "参考文献": 五号黑体 */
font-size: 14px;
font-weight: normal;
font-family: '黑体', 'SimHei', '华文黑体', 'STHeiti';
text-align: center;
margin-bottom: 7px;
margin-top: 20px;
}
ul, ol { /* 参考文献的项目: 小五号宋体 */
list-style-type: none;
padding: 0;
& > li {
font-size: 12px;
margin: 6px 0;
text-align: justify;
text-indent: -24px;
padding-left: 24px;
}
& a {
text-decoration: none;
color: black;
}
}
img {
display: block;
max-width: 100%;
margin: 0 auto;
margin-top: 10px;
}
blockquote, blockquote > p { /* 图片和表格的标题: 小五号宋体 */
font-size: 12px;
font-weight: normal;
text-align: center;
margin: 0;
}
blockquote > p { margin: 6px 0; }
table { /* 表格: 小五号宋体 */
font-size: 12px;
position: relative;
border-top: 1px solid black;
border-bottom: 1px solid black;
width: 100%;
max-width: 100%;
margin: 0 auto;
margin-bottom: 10px;
& th, & td {
font-weight: normal;
}
& thead::after { /* 用来做三线表中间的横线 */
content: '';
display: block;
position: absolute;
border-top: 1px solid #00000060;
width: 100%;
}
}
b, strong { /* 加粗按黑体处理 */
font-weight: normal;
font-family: '黑体', 'SimHei', '华文黑体', 'STHeiti';
}
`,
preParseMarkdown: async (md: string): Promise<string> => {
// 作者
md = md.replace(/#author# (.*)/mg, '<div class="author">$1</div>')
// 单位
md = md.replace(/#school# (.*)/mg, '<div class="school">$1</div>')
// 关键词
md = md.replace(/#keywords# (.*)/mg, '<div class="keywords">$1</div>')
// 摘要
md = md.replace(/#abstract# (.*)/mg, '<div class="abstract">$1</div>')
// 返回处理后的字符串
return md
},
preParseHTML: async (html: string): Promise<string> => {
// 把包裹图片的 p 标签去掉
html = html.replace(/<p><img (.*?)><\/p>/g, '<img $1>')
// 返回处理后的字符串
return html
}
}
2 changes: 1 addition & 1 deletion theme/theme.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PDFOptions } from 'puppeteer-core'
import type { PDFOptions } from 'puppeteer'

export interface MarkdownPaperTheme {
/**
Expand Down

0 comments on commit 0949982

Please sign in to comment.