实现性能
- 减少文章详情页 Toc 文章目录
实现思路
对文章内容提取 h1, h2, h3, h4, h5 标签与锚,这里咱们将采纳到第三方包
- github.com/PuerkitoBio/goquery
// handler/post.go
html = `
<h1 id="H55oy1"> 语雀文章内容 </h1>
<h2 id="H55oy2"> 语雀文章内容 </h2>
<h3 id="H55oy3"> 语雀文章内容 </h3>
<h4 id="H55oy4"> 语雀文章内容 </h4>
<h5 id="H55oy5"> 语雀文章内容 </h5>
`
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(html))
var navs []*Nav
doc.Find("h1, h2, h3, h4, h5").Each(func(i int, s *goquery.Selection) {
// ...
navs = append(navs, &Nav{
ID: "",
Title: "",
Level: "",
})
})
type Nav struct {
ID string
Title string
Level int
}