diff --git a/helpers/content.go b/helpers/content.go index f8479cd1b9a..75cb26d9c9c 100644 --- a/helpers/content.go +++ b/helpers/content.go @@ -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(`
+
Table of Contents
+ +
`) + newFirst := []byte(``) + + 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(`
  • +
    Table of Contents
    +
    +`) + + // compatible TOC with blackfriday. + expected := []byte(``) + + // 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) {