用docsify动态生成文档网站

docsify 是一个动态生成文档网站的工具。不同于 GitBook、Hexo 的地方是它不会将 .md 转成 .html 文件,所有转换工作都是在运行时进行。

需要到docsify-cli来启动web服务器

我在ubuntu18.04的环境下用node v16.19.1 (npm v9.5.1) 可以安装成功,其他环境需要自己试一下

使用nvm安装node

1
nvm install v16

升级npm

1
npm install -g npm@9.5.1

安装docsify-cli

1
npm i docsify-cli -g

初始化文档目录

1
docsify init .

启动web服务器

1
docsify serve .

默认启动在3000端口,可以通过nginx做反向代理

参考

docsify

主页模板

默认生成的index.html不太好用,这里给一个好用的模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>docsify</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0"
/>
<link
rel="stylesheet"
href="//cdn.jsdelivr.net/npm/docsify@4/lib/themes/vue.css"
title="vue"
/>
<link
rel="stylesheet"
href="//cdn.jsdelivr.net/npm/docsify@4/lib/themes/dark.css"
title="dark"
disabled
/>
<link
rel="stylesheet"
href="//cdn.jsdelivr.net/npm/docsify@4/lib/themes/buble.css"
title="buble"
disabled
/>
<link
rel="stylesheet"
href="//cdn.jsdelivr.net/npm/docsify@4/lib/themes/pure.css"
title="pure"
disabled
/>
<link
rel="stylesheet"
href="//cdn.jsdelivr.net/npm/docsify@4/lib/themes/dolphin.css"
title="dolphin"
disabled
/>
<style>
nav.app-nav li ul {
min-width: 100px;
}

#carbonads {
box-shadow: none !important;
width: auto !important;
}
</style>
</head>

<body>
<div id="app">Loading ...</div>
<script src="//cdn.jsdelivr.net/npm/docsify-plugin-carbon@1"></script>
<script>

// Docsify configuration
window.$docsify = {
auto2top: true,
coverpage: false,
executeScript: true,
loadSidebar: true,
loadNavbar: false,
mergeNavbar: false,
maxLevel: 4,
subMaxLevel: 4,
name: 'docsify',
search: 'auto',
};
</script>
<script src="//cdn.jsdelivr.net/npm/docsify@4/lib/docsify.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/docsify@4/lib/plugins/search.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/prismjs@1/components/prism-bash.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/prismjs@1/components/prism-markdown.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/prismjs@1/components/prism-nginx.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/prismjs@1/components/prism-php.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/vue@2/dist/vue.min.js"></script>
<!-- <script src="//cdn.jsdelivr.net/npm/vue@3/dist/vue.global.prod.js"></script> -->
</body>
</html>

自动生成_sidebar.md

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# -*- coding: utf-8 -*-
import sys
import os
from functools import cmp_to_key


def mycmp_filename(name1, name2):
if name1.upper() == "README.MD":
return -1
elif name2.upper() == "README.MD":
return 1
elif name1 < name2:
return -1
elif name2 < name1:
return 1
return 0


def gen(e, path, level):
content = ""
name, ext = os.path.splitext(e)
if os.path.isfile(path):
if not name.startswith('_') and ext.lower() == ".md":
if os.path.getsize(path):
content += "{}- [{}]({})\n".format(" "*level, name, path.replace('\\', '/') )
elif os.path.isdir(path):
if name.startswith('_'):
return ""
content2 = ""
for e2 in sorted(os.listdir(path), key=cmp_to_key(mycmp_filename)):
content2 += gen(e2, os.path.join(path, e2), level+1)
if content2:
if name:
content += "{}- {}\n".format(" "*level, name)
content += content2
else:
content = ""
return content


def main():
with open("_sidebar.md", "w", encoding="utf-8") as fp:
fp.write("<!-- docs/_sidebar.md -->\n")
fp.write("{}\n".format(gen("", ".", -1)))

if __name__ == '__main__':
main()