diff options
Diffstat (limited to 'md2html.sh')
-rwxr-xr-x | md2html.sh | 47 |
1 files changed, 38 insertions, 9 deletions
@@ -8,21 +8,50 @@ | |||
8 | # Created Time : 2023-12-15 | 8 | # Created Time : 2023-12-15 |
9 | ########################################################################## | 9 | ########################################################################## |
10 | 10 | ||
11 | # origin是输入的第一个参数,指源文件名 | 11 | origin=$1 # 源文件名 |
12 | # src是原文件名前边加一个.,是源文件的复制 | 12 | src="."$origin # 源文件的复制 |
13 | # dst是原文件名的md后缀改成html | 13 | dst=${origin%.*}".html" # 目标文件 |
14 | origin=$1 | 14 | |
15 | src="."$origin | 15 | if [ $# -ne 2 ] || [ ${origin##*.} != "md" ]; then |
16 | dst=${origin%.*}".html" | 16 | echo "Usage: $0 <markdown file> <html title>" |
17 | exit 1 | ||
18 | elif [ ! -f $origin ]; then | ||
19 | echo "Error: $1 does not exist" | ||
20 | exit 1 | ||
21 | fi | ||
17 | 22 | ||
18 | cp $origin $src | 23 | cp $origin $src |
19 | # src中所有的“```...”替换成“```”,其中...指换行前的所有内容 | 24 | pandoc --no-highlight --mathjax=none -s $src -o $dst --metadata title="$2" |
20 | sed -i 's/```.*$/```/g' $src | ||
21 | pandoc -s $src -o $dst | ||
22 | rm $src | 25 | rm $src |
23 | 26 | ||
27 | # 处理多行代码块,将<pre class="xxx"><code>替换为<pre class="language-xxx"> | ||
28 | sed -i -E ':a;N;$!ba;s|<pre[^>]*class="([^"]+)"[^>]*>[[:space:]]*<code[^>]*>|<pre><code class="language-\1">|g' $dst | ||
24 | sed -i '/<style/,/<\/style>/d' $dst | 29 | sed -i '/<style/,/<\/style>/d' $dst |
30 | # 修改body的样式 | ||
25 | sed -i 's/<body>/<body>\n<div class="pandoc">\n<div class="main">/' $dst | 31 | sed -i 's/<body>/<body>\n<div class="pandoc">\n<div class="main">/' $dst |
32 | # 添加评论区 | ||
26 | sed -i 's/<\/body>/<script src="https:\/\/www.qin-juan-ge-zhu.top\/common\/js\/comment.js"><\/script>\n<\/div>\n<\/div>\n<\/body>/' $dst | 33 | sed -i 's/<\/body>/<script src="https:\/\/www.qin-juan-ge-zhu.top\/common\/js\/comment.js"><\/script>\n<\/div>\n<\/div>\n<\/body>/' $dst |
27 | sed -i 's/\t/ /g' $dst | 34 | sed -i 's/\t/ /g' $dst |
35 | # 添加样式 | ||
28 | sed -i 's/<\/head>/<link rel="stylesheet" href="https:\/\/www.qin-juan-ge-zhu.top\/common\/CSS\/pandoc.css">\n<script type="text\/javascript" src="https:\/\/www.qin-juan-ge-zhu.top\/common\/script4code.js"><\/script><\/head>/' $dst | 36 | sed -i 's/<\/head>/<link rel="stylesheet" href="https:\/\/www.qin-juan-ge-zhu.top\/common\/CSS\/pandoc.css">\n<script type="text\/javascript" src="https:\/\/www.qin-juan-ge-zhu.top\/common\/script4code.js"><\/script><\/head>/' $dst |
37 | |||
38 | # 检查是否有数学公式,有则添加mathjax | ||
39 | if grep -Eq "math inline|math display" $dst; then | ||
40 | sed -i 's|</head>|<script type="text/javascript" async\ | ||
41 | src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML"></script>\ | ||
42 | <script type="text/x-mathjax-config">\ | ||
43 | MathJax.Hub.Config({\ | ||
44 | tex2jax: {\ | ||
45 | inlineMath: [["$","$"], ["\\\\(","\\\\)"]],\ | ||
46 | processEscapes: true\ | ||
47 | }\ | ||
48 | });\ | ||
49 | </script>\ | ||
50 | </head>|' "$dst" | ||
51 | fi | ||
52 | |||
53 | # 检查是否有mermaid有则告警 | ||
54 | grep -n "language-mermaid" $dst | ||
55 | if [ $? -eq 0 ]; then | ||
56 | echo "Convertion Warning: mermaid is included, may you need to replace it with a picture???" | ||
57 | fi | ||