Golang 语雀内容系统(5) 详情页Toc导航栏

实现功能

  • 增加文章详情页Toc文章目录

Golang 语雀内容系统(5) 详情页Toc导航栏_第1张图片

实现思路

对文章内容提取 h1, h2, h3, h4, h5 标签与锚,这里我们将采用到第三方包

  • github.com/PuerkitoBio/goquery
// handler/post.go

html = `

语雀文章内容

语雀文章内容

语雀文章内容

语雀文章内容

语雀文章内容
` 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 }

你可能感兴趣的:(goblog博客搭建)