summaryrefslogtreecommitdiffstats
path: root/md2html.sh
diff options
context:
space:
mode:
authorWe-unite <3205135446@qq.com>2025-01-07 12:36:05 +0800
committerWe-unite <3205135446@qq.com>2025-01-07 12:36:05 +0800
commit4d88ef666eee1b6f191f6e85b00acf8d5a2d1899 (patch)
tree68391846bae84f9546b0d089c012afc336a6e6bd /md2html.sh
parent11e64c5804b696f170b9d5d881befbabc4a4e85c (diff)
downloadmyweb-new_highlightjs.tar.gz
myweb-new_highlightjs.zip
highlight don't use auto-detect but given languagenew_highlightjs
In this commit, lot's of things is changed. Hope they all runs currectly. Now highlight.js is supporting more and more proguam languages, but the auto detection always go wrong, even for common languages like c, bash, python, makefile. Use Given Language ------------------ As you know, I always write markdown and convert to html by pandoc. In the old, "```cpp" in markdown will be deleted first to keep the embeded code clean and not highlighted, then I can use highlight.js. But this causes that html document doesn't know the language. This time, md2html.sh is changed: pandoc use "--no-highlight" argument to keep code clean, and it will output like this: ```html <pre class="cpp"><code>...</code></pre> ``` Although there may be other tags between `<code></code>`, it's clear that `<pre class="xxx"><code>` is nested tightly, except some space characters or \n. Then, sed deal with the whole doc(not line by line), replace `<pre class="xxx"><code>` with `<pre><code class="language-xxx">`. That's it! Math Formula ------------ Math formular is also a problem during convertion by pandoc. In the old it's dealed menually. Now pandoc use "--mathjax=none", then formula is no longer showed by pandoc, but only `<span class="math xxx">\( formula \)</span>`. And the math tool I used will deal with it. Mermaid picture ---------------- pandoc doesn't support convert mermaid in markdown to html picture. Let's have a warning!
Diffstat (limited to 'md2html.sh')
-rwxr-xr-xmd2html.sh47
1 files changed, 38 insertions, 9 deletions
diff --git a/md2html.sh b/md2html.sh
index 7aaee04..13f2670 100755
--- a/md2html.sh
+++ b/md2html.sh
@@ -8,21 +8,50 @@
8# Created Time : 2023-12-15 8# Created Time : 2023-12-15
9########################################################################## 9##########################################################################
10 10
11# origin是输入的第一个参数,指源文件名 11origin=$1 # 源文件名
12# src是原文件名前边加一个.,是源文件的复制 12src="."$origin # 源文件的复制
13# dst是原文件名的md后缀改成html 13dst=${origin%.*}".html" # 目标文件
14origin=$1 14
15src="."$origin 15if [ $# -ne 2 ] || [ ${origin##*.} != "md" ]; then
16dst=${origin%.*}".html" 16 echo "Usage: $0 <markdown file> <html title>"
17 exit 1
18elif [ ! -f $origin ]; then
19 echo "Error: $1 does not exist"
20 exit 1
21fi
17 22
18cp $origin $src 23cp $origin $src
19# src中所有的“```...”替换成“```”,其中...指换行前的所有内容 24pandoc --no-highlight --mathjax=none -s $src -o $dst --metadata title="$2"
20sed -i 's/```.*$/```/g' $src
21pandoc -s $src -o $dst
22rm $src 25rm $src
23 26
27# 处理多行代码块,将<pre class="xxx"><code>替换为<pre class="language-xxx">
28sed -i -E ':a;N;$!ba;s|<pre[^>]*class="([^"]+)"[^>]*>[[:space:]]*<code[^>]*>|<pre><code class="language-\1">|g' $dst
24sed -i '/<style/,/<\/style>/d' $dst 29sed -i '/<style/,/<\/style>/d' $dst
30# 修改body的样式
25sed -i 's/<body>/<body>\n<div class="pandoc">\n<div class="main">/' $dst 31sed -i 's/<body>/<body>\n<div class="pandoc">\n<div class="main">/' $dst
32# 添加评论区
26sed -i 's/<\/body>/<script src="https:\/\/www.qin-juan-ge-zhu.top\/common\/js\/comment.js"><\/script>\n<\/div>\n<\/div>\n<\/body>/' $dst 33sed -i 's/<\/body>/<script src="https:\/\/www.qin-juan-ge-zhu.top\/common\/js\/comment.js"><\/script>\n<\/div>\n<\/div>\n<\/body>/' $dst
27sed -i 's/\t/ /g' $dst 34sed -i 's/\t/ /g' $dst
35# 添加样式
28sed -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 36sed -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
39if 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"
51fi
52
53# 检查是否有mermaid有则告警
54grep -n "language-mermaid" $dst
55if [ $? -eq 0 ]; then
56 echo "Convertion Warning: mermaid is included, may you need to replace it with a picture???"
57fi