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

2020-08-01 00:16:18 +08:00
 whenov

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

支持 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 $<
1903 次点击
所在节点    分享创造
5 条回复
yujiang
2020-08-01 09:45:07 +08:00
hexo
whenov
2020-08-01 10:02:33 +08:00
@yujiang 个人使用的工具,还是希望能简单易懂。所以我的电脑上用的都是 suckless 那一套( suckless.org

开发做的越多,越喜欢 DTSTTCPW:Do the simplest thing that could possibly work
wolong
2020-08-01 18:42:33 +08:00
请问脚本在哪啊
whenov
2020-08-01 18:53:42 +08:00
@wolong 就在页面上啊?一个 html 模板加一个 Makefile
wolong
2020-08-01 22:35:05 +08:00
@whenov 原来是要装一个 Pandoc,我懂了,开始我以为是 node 什么的

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/694754

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX