Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add {{ .TableOfContents }} support in asciidoc #5678

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions helpers/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,13 +640,49 @@ func getAsciidocContent(ctx *RenderingContext) []byte {
}

jww.INFO.Println("Rendering", ctx.DocumentName, "with", path, "...")
args := []string{"--no-header-footer", "--safe"}
args := []string{"--no-header-footer", "--safe", "--attribute=toc"}
if isAsciidoctor {
// asciidoctor-specific arg to show stack traces on errors
args = append(args, "--trace")
}
args = append(args, "-")
return externallyRenderContent(ctx, path, args)
return replaceAsciidocTOC(externallyRenderContent(ctx, path, args))
}

// replaceAsciidocTOC replaces html tags for TOC for compatibility with blackfriday rendered TOC.
func replaceAsciidocTOC(content []byte) []byte {
first := []byte(`<div id="toc" class="toc">
<div id="toctitle">Table of Contents</div>
<ul class="sectlevel1">`)
last := []byte(`</ul>
</div>`)
newFirst := []byte(`<nav>
<ul>`)
newLast := []byte(`</ul>
</nav>`)

startOfTOC := bytes.Index(content, first)
if startOfTOC < 0 {
return content
}

peekEnd := len(content)
if peekEnd > 70+startOfTOC+len(first) {
peekEnd = 70 + startOfTOC + len(first)
}

correctNav := bytes.Index(content[startOfTOC:peekEnd], []byte(`<li><a href="#`))
if correctNav < 0 { // no match found
return content
}
lengthOfTOC := bytes.Index(content[startOfTOC:], last)
endOfTOC := startOfTOC + lengthOfTOC + len(last)

newcontent := make([]byte, 0, len(content))
newcontent = append(content[:startOfTOC], newFirst...)
newcontent = append(newcontent, content[startOfTOC+len(first):lengthOfTOC]...)
newcontent = append(newcontent, newLast...)
return append(newcontent, content[endOfTOC:]...)
}

// HasRst returns whether rst2html is installed on this computer.
Expand Down
23 changes: 23 additions & 0 deletions helpers/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,29 @@ func TestExtractNoTOC(t *testing.T) {
}
}

func TestReplaceAsciidocTOC(t *testing.T) {
// rendered by asciidoctor.
content := []byte(`<div id="toc" class="toc">
<div id="toctitle">Table of Contents</div>
<ul class="sectlevel1">
<li><a href="#sect1">sect1</a></li>
</ul>
</div>`)

// compatible TOC with blackfriday.
expected := []byte(`<nav>
<ul>
<li><a href="#sect1">sect1</a></li>
</ul>
</nav>`)

// replaces TOC for extraction by ExtractTOC()
actual := replaceAsciidocTOC(content)
if !bytes.Equal(actual, expected) {
t.Errorf("Actual (%s) did not equal expected (%s) content\n", actual, expected)
}
}

var totalWordsBenchmarkString = strings.Repeat("Hugo Rocks ", 200)

func TestTotalWords(t *testing.T) {
Expand Down