Skip to content

Commit

Permalink
Merge pull request #10 from saschagrunert/code-support
Browse files Browse the repository at this point in the history
Add inline code support
  • Loading branch information
k8s-ci-robot authored Sep 17, 2021
2 parents 2fc8c8a + a44ffea commit 2e856a6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
5 changes: 5 additions & 0 deletions mdtoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ var testcases = []testcase{{
includePrefix: false,
completeTOC: true,
validTOCTags: true,
}, {
file: testdata("code.md"),
includePrefix: true,
completeTOC: true,
validTOCTags: true,
}}

func testdata(subpath string) string {
Expand Down
11 changes: 5 additions & 6 deletions pkg/mdtoc/mdtoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,17 @@ func findTOCTags(raw []byte) (start, end int) {
return
}

func asText(node ast.Node) string {
var text string
func asText(node ast.Node) (text string) {
ast.WalkFunc(node, func(node ast.Node, entering bool) ast.WalkStatus {
if !entering {
return ast.GoToNext // Don't care about closing the heading section.
}
t, ok := node.(*ast.Text)
if !ok {
return ast.GoToNext // Ignore non-text nodes.

switch node.(type) {
case *ast.Text, *ast.Code:
text += string(node.AsLeaf().Literal)
}

text += string(t.AsLeaf().Literal)
return ast.GoToNext
})
return text
Expand Down
22 changes: 22 additions & 0 deletions testdata/code.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
title: this is a title block
description: we should ignore it
---

# Expected TOC

Manually verified by uploading to github.

<!-- toc -->
- [Expected TOC](#expected-toc)
- [H1 <code>H1</code> H1](#h1-h1-h1)
- [H2 <code>H2</code>](#h2-h2)
<!-- /toc -->

# H1 `H1` H1

filler

# H2 `H2`

filler

0 comments on commit 2e856a6

Please sign in to comment.