diff --git a/main.go b/main.go index d386b3c..8b8eab1 100755 --- a/main.go +++ b/main.go @@ -37,6 +37,7 @@ preview: {{.Preview}} {{- end}} type: {{.Type}} hide: {{.Hide}} +toc: {{.Toc}} --- ` ) @@ -112,6 +113,10 @@ func main() { Name: "hide", Usage: "Hides the article", }, + &cli.BoolFlag{ + Name: "toc", + Usage: "Adds a table of contents to the article", + }, &cli.BoolFlag{ Name: "top", Usage: "Places the article at the top", @@ -202,6 +207,7 @@ func New(c *cli.Context) { top := "false" postType := "post" hide := "false" + toc := "false" date := time.Now() // Empty string values @@ -245,6 +251,9 @@ func New(c *cli.Context) { if c.Bool("hide") { hide = "true" } + if c.Bool("toc") { + toc = "true" + } if c.Bool("draft") { draft = "true" } @@ -299,6 +308,7 @@ func New(c *cli.Context) { "Top": top, "Type": postType, "Hide": hide, + "Toc": toc, "Preview": preview, "Cover": cover, "Tags": tagString, diff --git a/parse.go b/parse.go index 30a1159..f72032b 100755 --- a/parse.go +++ b/parse.go @@ -67,6 +67,7 @@ type ArticleConfig struct { Top bool Type string Hide bool + Toc bool Image string Subtitle string Config map[string]interface{} @@ -129,11 +130,14 @@ func renderHookLazyLoadImage(w io.Writer, node ast.Node, entering bool) (ast.Wal return ast.GoToNext, false } -func ParseMarkdown(markdown string) template.HTML { +func ParseMarkdown(markdown string, toc bool) template.HTML { extensions := parser.CommonExtensions | parser.Footnotes parser := parser.NewWithExtensions(extensions) htmlFlags := html.CommonFlags + if toc { + htmlFlags |= html.TOC + } opts := html.RendererOptions{Flags: htmlFlags, RenderNodeHook: renderHookLazyLoadImage} renderer := html.NewRenderer(opts) @@ -226,10 +230,10 @@ func ParseArticleConfig(markdownPath string) (config *ArticleConfig, content str // Parse preview splited by MORE_SPLIT previewAry := strings.SplitN(content, MORE_SPLIT, 2) if len(config.Preview) <= 0 && len(previewAry) > 1 { - config.Preview = ParseMarkdown(previewAry[0]) + config.Preview = ParseMarkdown(previewAry[0], false) content = strings.Replace(content, MORE_SPLIT, "", 1) } else { - config.Preview = ParseMarkdown(string(config.Preview)) + config.Preview = ParseMarkdown(string(config.Preview), false) } return config, content } @@ -250,7 +254,7 @@ func ParseArticle(markdownPath string) *Article { article.Preview = config.Preview article.Config = config.Config article.Markdown = content - article.Content = ParseMarkdown(content) + article.Content = ParseMarkdown(content, config.Toc) if config.Date != "" { article.Time = ParseDate(config.Date) article.Date = article.Time.Unix() diff --git a/template/source/ink-blog-tool-en.md b/template/source/ink-blog-tool-en.md index d6d4170..f434498 100755 --- a/template/source/ink-blog-tool-en.md +++ b/template/source/ink-blog-tool-en.md @@ -70,6 +70,7 @@ tags: #Optional - Tag2 type: post #Specify type is post or page, Optional hide: false #Hide article,can be accessed via URL, Optional +toc: false #Show table of contents,Optional --- diff --git a/template/source/ink-blog-tool.md b/template/source/ink-blog-tool.md index c493d7b..2777391 100755 --- a/template/source/ink-blog-tool.md +++ b/template/source/ink-blog-tool.md @@ -82,6 +82,7 @@ tags: #可选 - 标签2 type: post #指定类型为文章(post)或页面(page),可选 hide: false #隐藏文章,只可通过链接访问,可选 +toc: false #是否显示文章目录,可选 ---