Skip to content

Commit 9b26c62

Browse files
authored
Merge pull request #138 from w568w/master
Fix some more issues
2 parents eaf0299 + 20a0966 commit 9b26c62

6 files changed

Lines changed: 68 additions & 13 deletions

File tree

main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ preview: {{.Preview}}
3737
{{- end}}
3838
type: {{.Type}}
3939
hide: {{.Hide}}
40+
toc: {{.Toc}}
4041
---
4142
`
4243
)
@@ -112,6 +113,10 @@ func main() {
112113
Name: "hide",
113114
Usage: "Hides the article",
114115
},
116+
&cli.BoolFlag{
117+
Name: "toc",
118+
Usage: "Adds a table of contents to the article",
119+
},
115120
&cli.BoolFlag{
116121
Name: "top",
117122
Usage: "Places the article at the top",
@@ -202,6 +207,7 @@ func New(c *cli.Context) {
202207
top := "false"
203208
postType := "post"
204209
hide := "false"
210+
toc := "false"
205211
date := time.Now()
206212

207213
// Empty string values
@@ -245,6 +251,9 @@ func New(c *cli.Context) {
245251
if c.Bool("hide") {
246252
hide = "true"
247253
}
254+
if c.Bool("toc") {
255+
toc = "true"
256+
}
248257
if c.Bool("draft") {
249258
draft = "true"
250259
}
@@ -299,6 +308,7 @@ func New(c *cli.Context) {
299308
"Top": top,
300309
"Type": postType,
301310
"Hide": hide,
311+
"Toc": toc,
302312
"Preview": preview,
303313
"Cover": cover,
304314
"Tags": tagString,

parse.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type ArticleConfig struct {
6767
Top bool
6868
Type string
6969
Hide bool
70+
Toc bool
7071
Image string
7172
Subtitle string
7273
Config map[string]interface{}
@@ -129,11 +130,14 @@ func renderHookLazyLoadImage(w io.Writer, node ast.Node, entering bool) (ast.Wal
129130
return ast.GoToNext, false
130131
}
131132

132-
func ParseMarkdown(markdown string) template.HTML {
133+
func ParseMarkdown(markdown string, toc bool) template.HTML {
133134
extensions := parser.CommonExtensions | parser.Footnotes
134135
parser := parser.NewWithExtensions(extensions)
135136

136137
htmlFlags := html.CommonFlags
138+
if toc {
139+
htmlFlags |= html.TOC
140+
}
137141
opts := html.RendererOptions{Flags: htmlFlags, RenderNodeHook: renderHookLazyLoadImage}
138142
renderer := html.NewRenderer(opts)
139143

@@ -226,10 +230,10 @@ func ParseArticleConfig(markdownPath string) (config *ArticleConfig, content str
226230
// Parse preview splited by MORE_SPLIT
227231
previewAry := strings.SplitN(content, MORE_SPLIT, 2)
228232
if len(config.Preview) <= 0 && len(previewAry) > 1 {
229-
config.Preview = ParseMarkdown(previewAry[0])
233+
config.Preview = ParseMarkdown(previewAry[0], false)
230234
content = strings.Replace(content, MORE_SPLIT, "", 1)
231235
} else {
232-
config.Preview = ParseMarkdown(string(config.Preview))
236+
config.Preview = ParseMarkdown(string(config.Preview), false)
233237
}
234238
return config, content
235239
}
@@ -250,7 +254,7 @@ func ParseArticle(markdownPath string) *Article {
250254
article.Preview = config.Preview
251255
article.Config = config.Config
252256
article.Markdown = content
253-
article.Content = ParseMarkdown(content)
257+
article.Content = ParseMarkdown(content, config.Toc)
254258
if config.Date != "" {
255259
article.Time = ParseDate(config.Date)
256260
article.Date = article.Time.Unix()

render.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,28 @@ func RenderArticles(tpl template.Template, articles Collections) {
6666
for i := range articles {
6767
currentArticle := articles[i].(Article)
6868
var renderArticle = RenderArticle{currentArticle, nil, nil}
69-
if i >= 1 {
70-
article := articles[i-1].(Article)
71-
renderArticle.Prev = &article
72-
}
73-
if i <= articleCount-2 {
74-
article := articles[i+1].(Article)
75-
renderArticle.Next = &article
69+
// Only show next and prev article if it is not hidden
70+
if !renderArticle.Hide {
71+
if i >= 1 {
72+
// Find prev unhidden article
73+
for j := i - 1; j >= 0; j-- {
74+
prevArticle := articles[j].(Article)
75+
if !prevArticle.Hide {
76+
renderArticle.Prev = &prevArticle
77+
break
78+
}
79+
}
80+
}
81+
if i <= articleCount-2 {
82+
// Find next unhidden article
83+
for j := i + 1; j < articleCount; j++ {
84+
nextArticle := articles[j].(Article)
85+
if !nextArticle.Hide {
86+
renderArticle.Next = &nextArticle
87+
break
88+
}
89+
}
90+
}
7691
}
7792
outPath := filepath.Join(publicPath, currentArticle.Link)
7893
wg.Add(1)
@@ -164,8 +179,8 @@ func RenderArticleList(rootPath string, articles Collections, tagName string) {
164179
"Develop": globalConfig.Develop,
165180
"Page": i + 1,
166181
"Total": page,
167-
"Prev": prev,
168-
"Next": next,
182+
"Prev": template.URL(filepath.ToSlash(prev)),
183+
"Next": template.URL(filepath.ToSlash(next)),
169184
"TagName": tagName,
170185
"TagCount": len(articles),
171186
}

template/source/ink-blog-tool-en.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ InkPaper is a static blog generator developed in Golang. No dependencies, cross
2323
2424
- Open `http://localhost:8000` in your browser to preview
2525

26+
### Features
27+
- YAML format configuration
28+
- Markdown format articles
29+
- No dependencies, cross platform
30+
- Super fast build times
31+
- Continuously improving theme and typography
32+
- Multiple article authors support
33+
- Archive and tag generation
34+
- Real-time preview when saving
35+
- Offline full-text keyword search
36+
- $\LaTeX$ style math formula support (MathJax):
37+
38+
$$
39+
\int_{-\infty}^\infty g(x) dx = \frac{1}{2\pi i} \oint_{\gamma} \frac{f(z)}{z-g(x)} dz
40+
$$
2641
### Website Configuration
2742
Edit `config.yml`, use this format:
2843

@@ -70,6 +85,7 @@ tags: #Optional
7085
- Tag2
7186
type: post #Specify type is post or page, Optional
7287
hide: false #Hide article,can be accessed via URL, Optional
88+
toc: false #Show table of contents,Optional
7389
7490
---
7591

template/source/ink-blog-tool.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ preview: 纸小墨(InkPaper)是一个GO语言编写的开源静态博客构
3434
- 归档与标签自动生成
3535
- 保存时实时预览页面
3636
- 离线的全文关键字搜索
37+
- $\LaTeX$ 风格的数学公式支持(MathJax):
38+
39+
$$
40+
\int_{-\infty}^\infty g(x) dx = \frac{1}{2\pi i} \oint_{\gamma} \frac{f(z)}{z-g(x)} dz
41+
$$
3742

3843
### 配置网站
3944
编辑`config.yml`,使用如下格式:
@@ -82,6 +87,7 @@ tags: #可选
8287
- 标签2
8388
type: post #指定类型为文章(post)或页面(page),可选
8489
hide: false #隐藏文章,只可通过链接访问,可选
90+
toc: false #是否显示文章目录,可选
8591
8692
---
8793

template/theme/_head.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
};
5252
var root = '{{.Site.Root}}';
5353
</script>
54+
55+
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
56+
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
57+
5458
{{if .Develop}}
5559
<script type="text/javascript">
5660
var conn, reloadTimer, connectTimer;

0 commit comments

Comments
 (0)