Skip to content

Commit

Permalink
feat: support toc rendering
Browse files Browse the repository at this point in the history
Implemented by gomarkdown/markdown, and do not support customized styles.

This closes InkProject#94 , InkProject#65 , and basically InkProject#78 .
  • Loading branch information
w568w committed Jan 31, 2023
1 parent f8eb47d commit dfba340
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ preview: {{.Preview}}
{{- end}}
type: {{.Type}}
hide: {{.Hide}}
toc: {{.Toc}}
---
`
)
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -202,6 +207,7 @@ func New(c *cli.Context) {
top := "false"
postType := "post"
hide := "false"
toc := "false"
date := time.Now()

// Empty string values
Expand Down Expand Up @@ -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"
}
Expand Down Expand Up @@ -299,6 +308,7 @@ func New(c *cli.Context) {
"Top": top,
"Type": postType,
"Hide": hide,
"Toc": toc,
"Preview": preview,
"Cover": cover,
"Tags": tagString,
Expand Down
12 changes: 8 additions & 4 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type ArticleConfig struct {
Top bool
Type string
Hide bool
Toc bool
Image string
Subtitle string
Config map[string]interface{}
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
}
Expand All @@ -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()
Expand Down
1 change: 1 addition & 0 deletions template/source/ink-blog-tool-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
1 change: 1 addition & 0 deletions template/source/ink-blog-tool.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ tags: #可选
- 标签2
type: post #指定类型为文章(post)或页面(page),可选
hide: false #隐藏文章,只可通过链接访问,可选
toc: false #是否显示文章目录,可选
---
Expand Down

0 comments on commit dfba340

Please sign in to comment.