Skip to content

Commit 8be2aeb

Browse files
committed
[update] add a file which can merge markdowns
1 parent 33e2e7c commit 8be2aeb

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

merge_markdowns_under_dir.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import os
2+
import subprocess
3+
import shutil
4+
5+
def markdown_to_pdf(directory):
6+
# 检查 Pandoc 是否可用
7+
if not shutil.which("pandoc"):
8+
raise FileNotFoundError("Pandoc 未安装或未在 PATH 中找到。")
9+
10+
# 获取目录下的所有 Markdown 文件(递归)
11+
markdown_files = []
12+
for root, _, files in os.walk(directory):
13+
for file in files:
14+
if file.endswith('.md'):
15+
markdown_files.append(os.path.join(root, file))
16+
17+
# 排序文件(可选)
18+
markdown_files.sort()
19+
20+
# 创建临时的合并 Markdown 文件
21+
temp_md_file = os.path.join(directory, 'merged.md')
22+
23+
with open(temp_md_file, 'w', encoding='utf-8') as outfile:
24+
for md_file in markdown_files:
25+
with open(md_file, 'r', encoding='utf-8') as infile:
26+
outfile.write(infile.read() + '\n\n')
27+
28+
markdown_to_pdf(directory="./docs/notes/zh")

0 commit comments

Comments
 (0)