分享自用的静态博客生成脚本

日期 : 2020-08-01 19:42:27作者 : 中原锦绣

现在的静态博客生成器都太复杂了,大多数功能我都不需要,所以就自己写了几行脚本。有相同需求的朋友可以参考。

支持 LaTeX 公式,主题是 GitHub 风格。

使用

执行 make,可将当前目录下的 Markdown 文件转换为 HTML 文件,并生成目录 index.html 。

编译时依赖

Pandoc

运行时依赖

MathJax, github-markdown-css

代码

page.tpl:

<!DOCTYPE html>
<title>$title$</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/4.0.0/github-markdown.min.css">
<style>
.markdown-body {
  max-width: 960px;
  margin: 20px auto;
}
</style>

<script type="text/x-mathjax-config">
MathJax.Hub.Config({
  tex2jax: {inlineMath: [['$$','$$'], ['\\(','\\)']]}
});
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML' async></script>

<article class="markdown-body">
$body$
</article>

Makefile:

index.html : $(patsubst %.md,%.html,$(wildcard *.md))
	ls -1t *.html | \
	xargs grep -E --exclude=index.html --max-count=1 "<h1>.+</h1>" | \
	sed -E "s/(.+)\.html:<h1>(.+)<\/h1>/- [\2](\/\1)/" | \
	pandoc --standalone --template=page.tpl --metadata=title:"Untitled Site" --output=$@ --from=commonmark

%.html : %.md
	pandoc --standalone --template=page.tpl --metadata=title:$(basename $@) --output=$@ --from=commonmark $<

标签 :