Add Goldmark as the new default markdown handler

This commit adds the fast and CommonMark compliant Goldmark as the new default markdown handler in Hugo.

If you want to continue using BlackFriday as the default for md/markdown extensions, you can use this configuration:

```toml
[markup]
defaultMarkdownHandler="blackfriday"
```

Fixes #5963
Fixes #1778
Fixes #6355
This commit is contained in:
Bjørn Erik Pedersen
2019-11-06 20:10:47 +01:00
parent a3fe5e5e35
commit bfb9613a14
69 changed files with 3424 additions and 1668 deletions

View File

@@ -24,19 +24,18 @@ import (
)
// Provider is the package entry point.
var Provider converter.NewProvider = provider{}
var Provider converter.ProviderProvider = provider{}
type provider struct {
}
func (p provider) New(cfg converter.ProviderConfig) (converter.Provider, error) {
var n converter.NewConverter = func(ctx converter.DocumentContext) (converter.Converter, error) {
return converter.NewProvider("asciidoc", func(ctx converter.DocumentContext) (converter.Converter, error) {
return &asciidocConverter{
ctx: ctx,
cfg: cfg,
}, nil
}
return n, nil
}), nil
}
type asciidocConverter struct {

View File

@@ -0,0 +1,70 @@
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package helpers implements general utility functions that work with
// and on content. The helper functions defined here lay down the
// foundation of how Hugo works with files and filepaths, and perform
// string operations on content.
package blackfriday_config
import (
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
)
// Default holds the default BlackFriday config.
// Do not change!
var Default = Config{
Smartypants: true,
AngledQuotes: false,
SmartypantsQuotesNBSP: false,
Fractions: true,
HrefTargetBlank: false,
NofollowLinks: false,
NoreferrerLinks: false,
SmartDashes: true,
LatexDashes: true,
PlainIDAnchors: true,
TaskLists: true,
SkipHTML: false,
}
// Config holds configuration values for BlackFriday rendering.
// It is kept here because it's used in several packages.
type Config struct {
Smartypants bool
SmartypantsQuotesNBSP bool
AngledQuotes bool
Fractions bool
HrefTargetBlank bool
NofollowLinks bool
NoreferrerLinks bool
SmartDashes bool
LatexDashes bool
TaskLists bool
PlainIDAnchors bool
Extensions []string
ExtensionsMask []string
SkipHTML bool
FootnoteAnchorPrefix string
FootnoteReturnLinkContents string
}
func UpdateConfig(b Config, m map[string]interface{}) (Config, error) {
if err := mapstructure.Decode(m, &b); err != nil {
return b, errors.WithMessage(err, "failed to decode rendering config")
}
return b, nil
}

View File

@@ -15,36 +15,27 @@
package blackfriday
import (
"github.com/gohugoio/hugo/markup/blackfriday/blackfriday_config"
"github.com/gohugoio/hugo/markup/converter"
"github.com/gohugoio/hugo/markup/internal"
"github.com/russross/blackfriday"
)
// Provider is the package entry point.
var Provider converter.NewProvider = provider{}
var Provider converter.ProviderProvider = provider{}
type provider struct {
}
func (p provider) New(cfg converter.ProviderConfig) (converter.Provider, error) {
defaultBlackFriday, err := internal.NewBlackfriday(cfg)
if err != nil {
return nil, err
}
defaultExtensions := getMarkdownExtensions(cfg.MarkupConfig.BlackFriday)
defaultExtensions := getMarkdownExtensions(defaultBlackFriday)
pygmentsCodeFences := cfg.Cfg.GetBool("pygmentsCodeFences")
pygmentsCodeFencesGuessSyntax := cfg.Cfg.GetBool("pygmentsCodeFencesGuessSyntax")
pygmentsOptions := cfg.Cfg.GetString("pygmentsOptions")
var n converter.NewConverter = func(ctx converter.DocumentContext) (converter.Converter, error) {
b := defaultBlackFriday
return converter.NewProvider("blackfriday", func(ctx converter.DocumentContext) (converter.Converter, error) {
b := cfg.MarkupConfig.BlackFriday
extensions := defaultExtensions
if ctx.ConfigOverrides != nil {
var err error
b, err = internal.UpdateBlackFriday(b, ctx.ConfigOverrides)
b, err = blackfriday_config.UpdateConfig(b, ctx.ConfigOverrides)
if err != nil {
return nil, err
}
@@ -56,27 +47,16 @@ func (p provider) New(cfg converter.ProviderConfig) (converter.Provider, error)
bf: b,
extensions: extensions,
cfg: cfg,
pygmentsCodeFences: pygmentsCodeFences,
pygmentsCodeFencesGuessSyntax: pygmentsCodeFencesGuessSyntax,
pygmentsOptions: pygmentsOptions,
}, nil
}
return n, nil
}), nil
}
type blackfridayConverter struct {
ctx converter.DocumentContext
bf *internal.BlackFriday
bf blackfriday_config.Config
extensions int
pygmentsCodeFences bool
pygmentsCodeFencesGuessSyntax bool
pygmentsOptions string
cfg converter.ProviderConfig
cfg converter.ProviderConfig
}
func (c *blackfridayConverter) AnchorSuffix() string {
@@ -90,7 +70,6 @@ func (c *blackfridayConverter) Convert(ctx converter.RenderContext) (converter.R
r := c.getHTMLRenderer(ctx.RenderTOC)
return converter.Bytes(blackfriday.Markdown(ctx.Src, r, c.extensions)), nil
}
func (c *blackfridayConverter) getHTMLRenderer(renderTOC bool) blackfriday.Renderer {
@@ -114,7 +93,7 @@ func (c *blackfridayConverter) getHTMLRenderer(renderTOC bool) blackfriday.Rende
}
}
func getFlags(renderTOC bool, cfg *internal.BlackFriday) int {
func getFlags(renderTOC bool, cfg blackfriday_config.Config) int {
var flags int
@@ -168,7 +147,7 @@ func getFlags(renderTOC bool, cfg *internal.BlackFriday) int {
return flags
}
func getMarkdownExtensions(cfg *internal.BlackFriday) int {
func getMarkdownExtensions(cfg blackfriday_config.Config) int {
// Default Blackfriday common extensions
commonExtensions := 0 |
blackfriday.EXTENSION_NO_INTRA_EMPHASIS |

View File

@@ -18,19 +18,15 @@ import (
"github.com/spf13/viper"
"github.com/gohugoio/hugo/markup/internal"
"github.com/gohugoio/hugo/markup/converter"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/markup/blackfriday/blackfriday_config"
"github.com/russross/blackfriday"
)
func TestGetMarkdownExtensionsMasksAreRemovedFromExtensions(t *testing.T) {
c := qt.New(t)
b, err := internal.NewBlackfriday(converter.ProviderConfig{Cfg: viper.New()})
c.Assert(err, qt.IsNil)
b := blackfriday_config.Default
b.Extensions = []string{"headerId"}
b.ExtensionsMask = []string{"noIntraEmphasis"}
@@ -45,9 +41,7 @@ func TestGetMarkdownExtensionsByDefaultAllExtensionsAreEnabled(t *testing.T) {
testFlag int
}
c := qt.New(t)
b, err := internal.NewBlackfriday(converter.ProviderConfig{Cfg: viper.New()})
c.Assert(err, qt.IsNil)
b := blackfriday_config.Default
b.Extensions = []string{""}
b.ExtensionsMask = []string{""}
@@ -79,9 +73,7 @@ func TestGetMarkdownExtensionsByDefaultAllExtensionsAreEnabled(t *testing.T) {
}
func TestGetMarkdownExtensionsAddingFlagsThroughRenderingContext(t *testing.T) {
c := qt.New(t)
b, err := internal.NewBlackfriday(converter.ProviderConfig{Cfg: viper.New()})
c.Assert(err, qt.IsNil)
b := blackfriday_config.Default
b.Extensions = []string{"definitionLists"}
b.ExtensionsMask = []string{""}
@@ -93,10 +85,7 @@ func TestGetMarkdownExtensionsAddingFlagsThroughRenderingContext(t *testing.T) {
}
func TestGetFlags(t *testing.T) {
c := qt.New(t)
cfg := converter.ProviderConfig{Cfg: viper.New()}
b, err := internal.NewBlackfriday(cfg)
c.Assert(err, qt.IsNil)
b := blackfriday_config.Default
flags := getFlags(false, b)
if flags&blackfriday.HTML_USE_XHTML != blackfriday.HTML_USE_XHTML {
t.Errorf("Test flag: %d was not found amongs set flags:%d; Result: %d", blackfriday.HTML_USE_XHTML, flags, flags&blackfriday.HTML_USE_XHTML)
@@ -105,9 +94,8 @@ func TestGetFlags(t *testing.T) {
func TestGetAllFlags(t *testing.T) {
c := qt.New(t)
cfg := converter.ProviderConfig{Cfg: viper.New()}
b, err := internal.NewBlackfriday(cfg)
c.Assert(err, qt.IsNil)
b := blackfriday_config.Default
type data struct {
testFlag int
@@ -145,9 +133,8 @@ func TestGetAllFlags(t *testing.T) {
for _, d := range allFlags {
expectedFlags |= d.testFlag
}
if expectedFlags != actualFlags {
t.Errorf("Expected flags (%d) did not equal actual (%d) flags.", expectedFlags, actualFlags)
}
c.Assert(actualFlags, qt.Equals, expectedFlags)
}
func TestConvert(t *testing.T) {

View File

@@ -30,10 +30,9 @@ type hugoHTMLRenderer struct {
// BlockCode renders a given text as a block of code.
// Pygments is used if it is setup to handle code fences.
func (r *hugoHTMLRenderer) BlockCode(out *bytes.Buffer, text []byte, lang string) {
if r.c.pygmentsCodeFences && (lang != "" || r.c.pygmentsCodeFencesGuessSyntax) {
opts := r.c.pygmentsOptions
if r.c.cfg.MarkupConfig.Highlight.CodeFences {
str := strings.Trim(string(text), "\n\r")
highlighted, _ := r.c.cfg.Highlight(str, lang, opts)
highlighted, _ := r.c.cfg.Highlight(str, lang, "")
out.WriteString(highlighted)
} else {
r.Renderer.BlockCode(out, text, lang)

View File

@@ -16,33 +16,51 @@ package converter
import (
"github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/markup/markup_config"
"github.com/gohugoio/hugo/markup/tableofcontents"
"github.com/spf13/afero"
)
// ProviderConfig configures a new Provider.
type ProviderConfig struct {
MarkupConfig markup_config.Config
Cfg config.Provider // Site config
ContentFs afero.Fs
Logger *loggers.Logger
Highlight func(code, lang, optsStr string) (string, error)
}
// NewProvider creates converter providers.
type NewProvider interface {
// ProviderProvider creates converter providers.
type ProviderProvider interface {
New(cfg ProviderConfig) (Provider, error)
}
// Provider creates converters.
type Provider interface {
New(ctx DocumentContext) (Converter, error)
Name() string
}
// NewConverter is an adapter that can be used as a ConverterProvider.
type NewConverter func(ctx DocumentContext) (Converter, error)
// NewProvider creates a new Provider with the given name.
func NewProvider(name string, create func(ctx DocumentContext) (Converter, error)) Provider {
return newConverter{
name: name,
create: create,
}
}
// New creates a new Converter for the given ctx.
func (n NewConverter) New(ctx DocumentContext) (Converter, error) {
return n(ctx)
type newConverter struct {
name string
create func(ctx DocumentContext) (Converter, error)
}
func (n newConverter) New(ctx DocumentContext) (Converter, error) {
return n.create(ctx)
}
func (n newConverter) Name() string {
return n.name
}
// Converter wraps the Convert method that converts some markup into
@@ -61,6 +79,11 @@ type DocumentInfo interface {
AnchorSuffix() string
}
// TableOfContentsProvider provides the content as a ToC structure.
type TableOfContentsProvider interface {
TableOfContents() tableofcontents.Root
}
// Bytes holds a byte slice and implements the Result interface.
type Bytes []byte

233
markup/goldmark/convert.go Normal file
View File

@@ -0,0 +1,233 @@
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package goldmark converts Markdown to HTML using Goldmark.
package goldmark
import (
"bytes"
"fmt"
"path/filepath"
"github.com/pkg/errors"
"github.com/spf13/afero"
"github.com/gohugoio/hugo/hugofs"
"github.com/alecthomas/chroma/styles"
"github.com/gohugoio/hugo/markup/converter"
"github.com/gohugoio/hugo/markup/highlight"
hl "github.com/gohugoio/hugo/markup/highlight/temphighlighting"
"github.com/gohugoio/hugo/markup/markup_config"
"github.com/gohugoio/hugo/markup/tableofcontents"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
// Provider is the package entry point.
var Provider converter.ProviderProvider = provide{}
type provide struct {
}
func (p provide) New(cfg converter.ProviderConfig) (converter.Provider, error) {
md := newMarkdown(cfg.MarkupConfig)
return converter.NewProvider("goldmark", func(ctx converter.DocumentContext) (converter.Converter, error) {
return &goldmarkConverter{
ctx: ctx,
cfg: cfg,
md: md,
}, nil
}), nil
}
type goldmarkConverter struct {
md goldmark.Markdown
ctx converter.DocumentContext
cfg converter.ProviderConfig
}
func newMarkdown(mcfg markup_config.Config) goldmark.Markdown {
cfg := mcfg.Goldmark
var (
extensions = []goldmark.Extender{
newTocExtension(),
}
rendererOptions []renderer.Option
parserOptions []parser.Option
)
if cfg.Renderer.HardWraps {
rendererOptions = append(rendererOptions, html.WithHardWraps())
}
if cfg.Renderer.XHTML {
rendererOptions = append(rendererOptions, html.WithXHTML())
}
if cfg.Renderer.Unsafe {
rendererOptions = append(rendererOptions, html.WithUnsafe())
}
if mcfg.Highlight.CodeFences {
extensions = append(extensions, newHighlighting(mcfg.Highlight))
}
if cfg.Extensions.Table {
extensions = append(extensions, extension.Table)
}
if cfg.Extensions.Strikethrough {
extensions = append(extensions, extension.Strikethrough)
}
if cfg.Extensions.Linkify {
extensions = append(extensions, extension.Linkify)
}
if cfg.Extensions.TaskList {
extensions = append(extensions, extension.TaskList)
}
if cfg.Extensions.Typographer {
extensions = append(extensions, extension.Typographer)
}
if cfg.Extensions.DefinitionList {
extensions = append(extensions, extension.DefinitionList)
}
if cfg.Extensions.Footnote {
extensions = append(extensions, extension.Footnote)
}
if cfg.Parser.AutoHeadingID {
parserOptions = append(parserOptions, parser.WithAutoHeadingID())
}
if cfg.Parser.Attribute {
parserOptions = append(parserOptions, parser.WithAttribute())
}
md := goldmark.New(
goldmark.WithExtensions(
extensions...,
),
goldmark.WithParserOptions(
parserOptions...,
),
goldmark.WithRendererOptions(
rendererOptions...,
),
)
return md
}
type converterResult struct {
converter.Result
toc tableofcontents.Root
}
func (c converterResult) TableOfContents() tableofcontents.Root {
return c.toc
}
func (c *goldmarkConverter) Convert(ctx converter.RenderContext) (result converter.Result, err error) {
defer func() {
if r := recover(); r != nil {
dir := afero.GetTempDir(hugofs.Os, "hugo_bugs")
name := fmt.Sprintf("goldmark_%s.txt", c.ctx.DocumentID)
filename := filepath.Join(dir, name)
afero.WriteFile(hugofs.Os, filename, ctx.Src, 07555)
err = errors.Errorf("[BUG] goldmark: create an issue on GitHub attaching the file in: %s", filename)
}
}()
buf := &bytes.Buffer{}
result = buf
pctx := parser.NewContext()
pctx.Set(tocEnableKey, ctx.RenderTOC)
reader := text.NewReader(ctx.Src)
doc := c.md.Parser().Parse(
reader,
parser.WithContext(pctx),
)
if err := c.md.Renderer().Render(buf, ctx.Src, doc); err != nil {
return nil, err
}
if toc, ok := pctx.Get(tocResultKey).(tableofcontents.Root); ok {
return converterResult{
Result: buf,
toc: toc,
}, nil
}
return buf, nil
}
func newHighlighting(cfg highlight.Config) goldmark.Extender {
style := styles.Get(cfg.Style)
if style == nil {
style = styles.Fallback
}
e := hl.NewHighlighting(
hl.WithStyle(cfg.Style),
hl.WithCodeBlockOptions(highlight.GetCodeBlockOptions()),
hl.WithFormatOptions(
cfg.ToHTMLOptions()...,
),
hl.WithWrapperRenderer(func(w util.BufWriter, ctx hl.CodeBlockContext, entering bool) {
l, hasLang := ctx.Language()
var language string
if hasLang {
language = string(l)
}
if entering {
if !ctx.Highlighted() {
w.WriteString(`<pre>`)
highlight.WriteCodeTag(w, language)
return
}
w.WriteString(`<div class="highlight">`)
return
}
if !ctx.Highlighted() {
w.WriteString(`</code></pre>`)
return
}
w.WriteString("</div>")
}),
)
return e
}

View File

@@ -0,0 +1,219 @@
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package goldmark
import (
"strings"
"testing"
"github.com/gohugoio/hugo/markup/highlight"
"github.com/gohugoio/hugo/markup/markup_config"
"github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/markup/converter"
qt "github.com/frankban/quicktest"
)
func TestConvert(t *testing.T) {
c := qt.New(t)
// Smoke test of the default configuration.
content := `
## Code Fences
§§§bash
LINE1
§§§
## Code Fences No Lexer
§§§moo
LINE1
§§§
## Custom ID {#custom}
## Auto ID
* Autolink: https://gohugo.io/
* Strikethrough:~~Hi~~ Hello, world!
## Table
| foo | bar |
| --- | --- |
| baz | bim |
## Task Lists (default on)
- [x] Finish my changes[^1]
- [ ] Push my commits to GitHub
- [ ] Open a pull request
## Smartypants (default on)
* Straight double "quotes" and single 'quotes' into “curly” quote HTML entities
* Dashes (“--” and “---”) into en- and em-dash entities
* Three consecutive dots (“...”) into an ellipsis entity
## Footnotes
That's some text with a footnote.[^1]
## Definition Lists
date
: the datetime assigned to this page.
description
: the description for the content.
[^1]: And that's the footnote.
`
// Code fences
content = strings.Replace(content, "§§§", "```", -1)
mconf := markup_config.Default
mconf.Highlight.NoClasses = false
p, err := Provider.New(
converter.ProviderConfig{
MarkupConfig: mconf,
Logger: loggers.NewErrorLogger(),
},
)
c.Assert(err, qt.IsNil)
conv, err := p.New(converter.DocumentContext{})
c.Assert(err, qt.IsNil)
b, err := conv.Convert(converter.RenderContext{Src: []byte(content)})
c.Assert(err, qt.IsNil)
got := string(b.Bytes())
// Header IDs
c.Assert(got, qt.Contains, `<h2 id="custom">Custom ID</h2>`, qt.Commentf(got))
c.Assert(got, qt.Contains, `<h2 id="auto-id">Auto ID</h2>`, qt.Commentf(got))
// Code fences
c.Assert(got, qt.Contains, "<div class=\"highlight\"><pre class=\"chroma\"><code class=\"language-bash\" data-lang=\"bash\">LINE1\n</code></pre></div>")
c.Assert(got, qt.Contains, "Code Fences No Lexer</h2>\n<pre><code class=\"language-moo\" data-lang=\"moo\">LINE1\n</code></pre>")
// Extensions
c.Assert(got, qt.Contains, `Autolink: <a href="https://gohugo.io/">https://gohugo.io/</a>`)
c.Assert(got, qt.Contains, `Strikethrough:<del>Hi</del> Hello, world`)
c.Assert(got, qt.Contains, `<th>foo</th>`)
c.Assert(got, qt.Contains, `<li><input disabled="" type="checkbox">Push my commits to GitHub</li>`)
c.Assert(got, qt.Contains, `Straight double &ldquo;quotes&rdquo; and single &lsquo;quotes&rsquo;`)
c.Assert(got, qt.Contains, `Dashes (“&ndash;” and “&mdash;”) `)
c.Assert(got, qt.Contains, `Three consecutive dots (“&hellip;”)`)
c.Assert(got, qt.Contains, `footnote.<sup id="fnref:1"><a href="#fn:1" class="footnote-ref" role="doc-noteref">1</a></sup>`)
c.Assert(got, qt.Contains, `<section class="footnotes" role="doc-endnotes">`)
c.Assert(got, qt.Contains, `<dt>date</dt>`)
}
func TestCodeFence(t *testing.T) {
c := qt.New(t)
lines := `LINE1
LINE2
LINE3
LINE4
LINE5
`
convertForConfig := func(c *qt.C, conf highlight.Config, code, language string) string {
mconf := markup_config.Default
mconf.Highlight = conf
p, err := Provider.New(
converter.ProviderConfig{
MarkupConfig: mconf,
Logger: loggers.NewErrorLogger(),
},
)
content := "```" + language + "\n" + code + "\n```"
c.Assert(err, qt.IsNil)
conv, err := p.New(converter.DocumentContext{})
c.Assert(err, qt.IsNil)
b, err := conv.Convert(converter.RenderContext{Src: []byte(content)})
c.Assert(err, qt.IsNil)
return string(b.Bytes())
}
c.Run("Basic", func(c *qt.C) {
cfg := highlight.DefaultConfig
cfg.NoClasses = false
result := convertForConfig(c, cfg, `echo "Hugo Rocks!"`, "bash")
// TODO(bep) there is a whitespace mismatch (\n) between this and the highlight template func.
c.Assert(result, qt.Equals, `<div class="highlight"><pre class="chroma"><code class="language-bash" data-lang="bash"><span class="nb">echo</span> <span class="s2">&#34;Hugo Rocks!&#34;</span>
</code></pre></div>`)
result = convertForConfig(c, cfg, `echo "Hugo Rocks!"`, "unknown")
c.Assert(result, qt.Equals, "<pre><code class=\"language-unknown\" data-lang=\"unknown\">echo &quot;Hugo Rocks!&quot;\n</code></pre>")
})
c.Run("Highlight lines, default config", func(c *qt.C) {
cfg := highlight.DefaultConfig
cfg.NoClasses = false
result := convertForConfig(c, cfg, lines, `bash {linenos=table,hl_lines=[2 "4-5"],linenostart=3}`)
c.Assert(result, qt.Contains, "<div class=\"highlight\"><div class=\"chroma\">\n<table class=\"lntable\"><tr><td class=\"lntd\">\n<pre class=\"chroma\"><code><span class")
c.Assert(result, qt.Contains, "<span class=\"hl\"><span class=\"lnt\">4")
result = convertForConfig(c, cfg, lines, "bash {linenos=inline,hl_lines=[2]}")
c.Assert(result, qt.Contains, "<span class=\"ln\">2</span>LINE2\n</span>")
c.Assert(result, qt.Not(qt.Contains), "<table")
result = convertForConfig(c, cfg, lines, "bash {linenos=true,hl_lines=[2]}")
c.Assert(result, qt.Contains, "<table")
c.Assert(result, qt.Contains, "<span class=\"hl\"><span class=\"lnt\">2\n</span>")
})
c.Run("Highlight lines, linenumbers default on", func(c *qt.C) {
cfg := highlight.DefaultConfig
cfg.NoClasses = false
cfg.LineNos = true
result := convertForConfig(c, cfg, lines, "bash")
c.Assert(result, qt.Contains, "<span class=\"lnt\">2\n</span>")
result = convertForConfig(c, cfg, lines, "bash {linenos=false,hl_lines=[2]}")
c.Assert(result, qt.Not(qt.Contains), "class=\"lnt\"")
})
c.Run("Highlight lines, linenumbers default on, linenumbers in table default off", func(c *qt.C) {
cfg := highlight.DefaultConfig
cfg.NoClasses = false
cfg.LineNos = true
cfg.LineNumbersInTable = false
result := convertForConfig(c, cfg, lines, "bash")
c.Assert(result, qt.Contains, "<span class=\"ln\">2</span>LINE2\n<")
result = convertForConfig(c, cfg, lines, "bash {linenos=table}")
c.Assert(result, qt.Contains, "<span class=\"lnt\">1\n</span>")
})
}

View File

@@ -0,0 +1,74 @@
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package goldmark_config holds Goldmark related configuration.
package goldmark_config
// DefaultConfig holds the default Goldmark configuration.
var Default = Config{
Extensions: Extensions{
Typographer: true,
Footnote: true,
DefinitionList: true,
Table: true,
Strikethrough: true,
Linkify: true,
TaskList: true,
},
Renderer: Renderer{
Unsafe: false,
},
Parser: Parser{
AutoHeadingID: true,
Attribute: true,
},
}
// Config configures Goldmark.
type Config struct {
Renderer Renderer
Parser Parser
Extensions Extensions
}
type Extensions struct {
Typographer bool
Footnote bool
DefinitionList bool
// GitHub flavored markdown
Table bool
Strikethrough bool
Linkify bool
TaskList bool
}
type Renderer struct {
// Whether softline breaks should be rendered as '<br>'
HardWraps bool
// XHTML instead of HTML5.
XHTML bool
// Allow raw HTML etc.
Unsafe bool
}
type Parser struct {
// Enables custom heading ids and
// auto generated heading ids.
AutoHeadingID bool
// Enables custom attributes.
Attribute bool
}

102
markup/goldmark/toc.go Normal file
View File

@@ -0,0 +1,102 @@
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package goldmark
import (
"bytes"
"github.com/gohugoio/hugo/markup/tableofcontents"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
var (
tocResultKey = parser.NewContextKey()
tocEnableKey = parser.NewContextKey()
)
type tocTransformer struct {
}
func (t *tocTransformer) Transform(n *ast.Document, reader text.Reader, pc parser.Context) {
if b, ok := pc.Get(tocEnableKey).(bool); !ok || !b {
return
}
var (
toc tableofcontents.Root
header tableofcontents.Header
level int
row = -1
inHeading bool
headingText bytes.Buffer
)
ast.Walk(n, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
s := ast.WalkStatus(ast.WalkContinue)
if n.Kind() == ast.KindHeading {
if inHeading && !entering {
header.Text = headingText.String()
headingText.Reset()
toc.AddAt(header, row, level-1)
header = tableofcontents.Header{}
inHeading = false
return s, nil
}
inHeading = true
}
if !(inHeading && entering) {
return s, nil
}
switch n.Kind() {
case ast.KindHeading:
heading := n.(*ast.Heading)
level = heading.Level
if level == 1 || row == -1 {
row++
}
id, found := heading.AttributeString("id")
if found {
header.ID = string(id.([]byte))
}
case ast.KindText:
textNode := n.(*ast.Text)
headingText.Write(textNode.Text(reader.Source()))
}
return s, nil
})
pc.Set(tocResultKey, toc)
}
type tocExtension struct {
}
func newTocExtension() goldmark.Extender {
return &tocExtension{}
}
func (e *tocExtension) Extend(m goldmark.Markdown) {
m.Parser().AddOptions(parser.WithASTTransformers(util.Prioritized(&tocTransformer{}, 10)))
}

View File

@@ -0,0 +1,76 @@
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package goldmark converts Markdown to HTML using Goldmark.
package goldmark
import (
"testing"
"github.com/gohugoio/hugo/markup/markup_config"
"github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/markup/converter"
qt "github.com/frankban/quicktest"
)
func TestToc(t *testing.T) {
c := qt.New(t)
content := `
# Header 1
## First h2
Some text.
### H3
Some more text.
## Second h2
And then some.
### Second H3
#### First H4
`
p, err := Provider.New(
converter.ProviderConfig{
MarkupConfig: markup_config.Default,
Logger: loggers.NewErrorLogger()})
c.Assert(err, qt.IsNil)
conv, err := p.New(converter.DocumentContext{})
c.Assert(err, qt.IsNil)
b, err := conv.Convert(converter.RenderContext{Src: []byte(content), RenderTOC: true})
c.Assert(err, qt.IsNil)
got := b.(converter.TableOfContentsProvider).TableOfContents().ToHTML(2, 3)
c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ul>
<li><a href="#first-h2">First h2</a>
<ul>
<li><a href="#h3">H3</a></li>
</ul>
</li>
<li><a href="#second-h2">Second h2</a>
<ul>
<li><a href="#second-h3">Second H3</a></li>
</ul>
</li>
</ul>
</nav>`, qt.Commentf(got))
}

188
markup/highlight/config.go Normal file
View File

@@ -0,0 +1,188 @@
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package highlight provides code highlighting.
package highlight
import (
"fmt"
"strconv"
"strings"
"github.com/alecthomas/chroma/formatters/html"
"github.com/gohugoio/hugo/config"
"github.com/mitchellh/mapstructure"
)
var DefaultConfig = Config{
// The highlighter style to use.
// See https://xyproto.github.io/splash/docs/all.html
Style: "monokai",
LineNoStart: 1,
CodeFences: true,
NoClasses: true,
LineNumbersInTable: true,
TabWidth: 4,
}
//
type Config struct {
Style string
CodeFences bool
// Use inline CSS styles.
NoClasses bool
// When set, line numbers will be printed.
LineNos bool
LineNumbersInTable bool
// Start the line numbers from this value (default is 1).
LineNoStart int
// A space separated list of line numbers, e.g. “3-8 10-20”.
Hl_Lines string
// TabWidth sets the number of characters for a tab. Defaults to 4.
TabWidth int
}
func (cfg Config) ToHTMLOptions() []html.Option {
var options = []html.Option{
html.TabWidth(cfg.TabWidth),
html.WithLineNumbers(cfg.LineNos),
html.BaseLineNumber(cfg.LineNoStart),
html.LineNumbersInTable(cfg.LineNumbersInTable),
html.WithClasses(!cfg.NoClasses),
}
if cfg.Hl_Lines != "" {
ranges, err := hlLinesToRanges(cfg.LineNoStart, cfg.Hl_Lines)
if err == nil {
options = append(options, html.HighlightLines(ranges))
}
}
return options
}
func applyOptionsFromString(opts string, cfg *Config) error {
optsm, err := parseOptions(opts)
if err != nil {
return err
}
return mapstructure.WeakDecode(optsm, cfg)
}
// ApplyLegacyConfig applies legacy config from back when we had
// Pygments.
func ApplyLegacyConfig(cfg config.Provider, conf *Config) error {
if conf.Style == DefaultConfig.Style {
if s := cfg.GetString("pygmentsStyle"); s != "" {
conf.Style = s
}
}
if conf.NoClasses == DefaultConfig.NoClasses && cfg.IsSet("pygmentsUseClasses") {
conf.NoClasses = !cfg.GetBool("pygmentsUseClasses")
}
if conf.CodeFences == DefaultConfig.CodeFences && cfg.IsSet("pygmentsCodeFences") {
conf.CodeFences = cfg.GetBool("pygmentsCodeFences")
}
if cfg.IsSet("pygmentsOptions") {
if err := applyOptionsFromString(cfg.GetString("pygmentsOptions"), conf); err != nil {
return err
}
}
return nil
}
func parseOptions(in string) (map[string]interface{}, error) {
in = strings.Trim(in, " ")
opts := make(map[string]interface{})
if in == "" {
return opts, nil
}
for _, v := range strings.Split(in, ",") {
keyVal := strings.Split(v, "=")
key := strings.ToLower(strings.Trim(keyVal[0], " "))
if len(keyVal) != 2 {
return opts, fmt.Errorf("invalid Highlight option: %s", key)
}
if key == "linenos" {
opts[key] = keyVal[1] != "false"
if keyVal[1] == "table" || keyVal[1] == "inline" {
opts["lineNumbersInTable"] = keyVal[1] == "table"
}
} else {
opts[key] = keyVal[1]
}
}
return opts, nil
}
// startLine compansates for https://github.com/alecthomas/chroma/issues/30
func hlLinesToRanges(startLine int, s string) ([][2]int, error) {
var ranges [][2]int
s = strings.TrimSpace(s)
if s == "" {
return ranges, nil
}
// Variants:
// 1 2 3 4
// 1-2 3-4
// 1-2 3
// 1 3-4
// 1 3-4
fields := strings.Split(s, " ")
for _, field := range fields {
field = strings.TrimSpace(field)
if field == "" {
continue
}
numbers := strings.Split(field, "-")
var r [2]int
first, err := strconv.Atoi(numbers[0])
if err != nil {
return ranges, err
}
first = first + startLine - 1
r[0] = first
if len(numbers) > 1 {
second, err := strconv.Atoi(numbers[1])
if err != nil {
return ranges, err
}
second = second + startLine - 1
r[1] = second
} else {
r[1] = first
}
ranges = append(ranges, r)
}
return ranges, nil
}

View File

@@ -0,0 +1,59 @@
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package highlight provides code highlighting.
package highlight
import (
"testing"
"github.com/spf13/viper"
qt "github.com/frankban/quicktest"
)
func TestConfig(t *testing.T) {
c := qt.New(t)
c.Run("applyLegacyConfig", func(c *qt.C) {
v := viper.New()
v.Set("pygmentsStyle", "hugo")
v.Set("pygmentsUseClasses", false)
v.Set("pygmentsCodeFences", false)
v.Set("pygmentsOptions", "linenos=inline")
cfg := DefaultConfig
err := ApplyLegacyConfig(v, &cfg)
c.Assert(err, qt.IsNil)
c.Assert(cfg.Style, qt.Equals, "hugo")
c.Assert(cfg.NoClasses, qt.Equals, true)
c.Assert(cfg.CodeFences, qt.Equals, false)
c.Assert(cfg.LineNos, qt.Equals, true)
c.Assert(cfg.LineNumbersInTable, qt.Equals, false)
})
c.Run("parseOptions", func(c *qt.C) {
cfg := DefaultConfig
opts := "noclasses=true,linenos=inline,linenostart=32,hl_lines=3-8 10-20"
err := applyOptionsFromString(opts, &cfg)
c.Assert(err, qt.IsNil)
c.Assert(cfg.NoClasses, qt.Equals, true)
c.Assert(cfg.LineNos, qt.Equals, true)
c.Assert(cfg.LineNumbersInTable, qt.Equals, false)
c.Assert(cfg.LineNoStart, qt.Equals, 32)
c.Assert(cfg.Hl_Lines, qt.Equals, "3-8 10-20")
})
}

View File

@@ -0,0 +1,132 @@
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package highlight
import (
"fmt"
"io"
"strings"
"github.com/alecthomas/chroma"
"github.com/alecthomas/chroma/formatters/html"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles"
hl "github.com/gohugoio/hugo/markup/highlight/temphighlighting"
)
func New(cfg Config) Highlighter {
return Highlighter{
cfg: cfg,
}
}
type Highlighter struct {
cfg Config
}
func (h Highlighter) Highlight(code, lang, optsStr string) (string, error) {
cfg := h.cfg
if optsStr != "" {
if err := applyOptionsFromString(optsStr, &cfg); err != nil {
return "", err
}
}
return highlight(code, lang, cfg)
}
func highlight(code, lang string, cfg Config) (string, error) {
w := &strings.Builder{}
var lexer chroma.Lexer
if lang != "" {
lexer = lexers.Get(lang)
}
if lexer == nil {
wrapper := getPreWrapper(lang)
fmt.Fprint(w, wrapper.Start(true, ""))
fmt.Fprint(w, code)
fmt.Fprint(w, wrapper.End(true))
return w.String(), nil
}
style := styles.Get(cfg.Style)
if style == nil {
style = styles.Fallback
}
iterator, err := lexer.Tokenise(nil, code)
if err != nil {
return "", err
}
options := cfg.ToHTMLOptions()
options = append(options, getHtmlPreWrapper(lang))
formatter := html.New(options...)
fmt.Fprintf(w, `<div class="highlight">`)
if err := formatter.Format(w, style, iterator); err != nil {
return "", err
}
fmt.Fprintf(w, `</div>`)
return w.String(), nil
}
func GetCodeBlockOptions() func(ctx hl.CodeBlockContext) []html.Option {
return func(ctx hl.CodeBlockContext) []html.Option {
var language string
if l, ok := ctx.Language(); ok {
language = string(l)
}
return []html.Option{
getHtmlPreWrapper(language),
}
}
}
func getPreWrapper(language string) preWrapper {
return preWrapper{language: language}
}
func getHtmlPreWrapper(language string) html.Option {
return html.WithPreWrapper(getPreWrapper(language))
}
type preWrapper struct {
language string
}
func (p preWrapper) Start(code bool, styleAttr string) string {
w := &strings.Builder{}
fmt.Fprintf(w, "<pre%s>", styleAttr)
var language string
if code {
language = p.language
}
WriteCodeTag(w, language)
return w.String()
}
func WriteCodeTag(w io.Writer, language string) {
fmt.Fprint(w, "<code")
if language != "" {
fmt.Fprintf(w, " class=\"language-"+language+"\"")
fmt.Fprintf(w, " data-lang=\""+language+"\"")
}
fmt.Fprint(w, ">")
}
func (p preWrapper) End(code bool) string {
return "</code></pre>"
}

View File

@@ -0,0 +1,87 @@
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package highlight provides code highlighting.
package highlight
import (
"testing"
qt "github.com/frankban/quicktest"
)
func TestHighlight(t *testing.T) {
c := qt.New(t)
lines := `LINE1
LINE2
LINE3
LINE4
LINE5
`
c.Run("Basic", func(c *qt.C) {
cfg := DefaultConfig
cfg.NoClasses = false
h := New(cfg)
result, _ := h.Highlight(`echo "Hugo Rocks!"`, "bash", "")
c.Assert(result, qt.Equals, `<div class="highlight"><pre class="chroma"><code class="language-bash" data-lang="bash"><span class="nb">echo</span> <span class="s2">&#34;Hugo Rocks!&#34;</span></code></pre></div>`)
result, _ = h.Highlight(`echo "Hugo Rocks!"`, "unknown", "")
c.Assert(result, qt.Equals, `<pre><code class="language-unknown" data-lang="unknown">echo "Hugo Rocks!"</code></pre>`)
})
c.Run("Highlight lines, default config", func(c *qt.C) {
cfg := DefaultConfig
cfg.NoClasses = false
h := New(cfg)
result, _ := h.Highlight(lines, "bash", "linenos=table,hl_lines=2 4-5,linenostart=3")
c.Assert(result, qt.Contains, "<div class=\"highlight\"><div class=\"chroma\">\n<table class=\"lntable\"><tr><td class=\"lntd\">\n<pre class=\"chroma\"><code><span class")
c.Assert(result, qt.Contains, "<span class=\"hl\"><span class=\"lnt\">4")
result, _ = h.Highlight(lines, "bash", "linenos=inline,hl_lines=2")
c.Assert(result, qt.Contains, "<span class=\"ln\">2</span>LINE2\n</span>")
c.Assert(result, qt.Not(qt.Contains), "<table")
result, _ = h.Highlight(lines, "bash", "linenos=true,hl_lines=2")
c.Assert(result, qt.Contains, "<table")
c.Assert(result, qt.Contains, "<span class=\"hl\"><span class=\"lnt\">2\n</span>")
})
c.Run("Highlight lines, linenumbers default on", func(c *qt.C) {
cfg := DefaultConfig
cfg.NoClasses = false
cfg.LineNos = true
h := New(cfg)
result, _ := h.Highlight(lines, "bash", "")
c.Assert(result, qt.Contains, "<span class=\"lnt\">2\n</span>")
result, _ = h.Highlight(lines, "bash", "linenos=false,hl_lines=2")
c.Assert(result, qt.Not(qt.Contains), "class=\"lnt\"")
})
c.Run("Highlight lines, linenumbers default on, linenumbers in table default off", func(c *qt.C) {
cfg := DefaultConfig
cfg.NoClasses = false
cfg.LineNos = true
cfg.LineNumbersInTable = false
h := New(cfg)
result, _ := h.Highlight(lines, "bash", "")
c.Assert(result, qt.Contains, "<span class=\"ln\">2</span>LINE2\n<")
result, _ = h.Highlight(lines, "bash", "linenos=table")
c.Assert(result, qt.Contains, "<span class=\"lnt\">1\n</span>")
})
}

View File

@@ -0,0 +1,512 @@
// package highlighting is a extension for the goldmark(http://github.com/yuin/goldmark).
//
// This extension adds syntax-highlighting to the fenced code blocks using
// chroma(https://github.com/alecthomas/chroma).
//
// TODO(bep) this is a very temporary fork based on https://github.com/yuin/goldmark-highlighting/pull/10
// MIT Licensed, Copyright Yusuke Inuzuka
package temphighlighting
import (
"bytes"
"io"
"strconv"
"strings"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
"github.com/alecthomas/chroma"
chromahtml "github.com/alecthomas/chroma/formatters/html"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles"
)
// ImmutableAttributes is a read-only interface for ast.Attributes.
type ImmutableAttributes interface {
// Get returns (value, true) if an attribute associated with given
// name exists, otherwise (nil, false)
Get(name []byte) (interface{}, bool)
// GetString returns (value, true) if an attribute associated with given
// name exists, otherwise (nil, false)
GetString(name string) (interface{}, bool)
// All returns all attributes.
All() []ast.Attribute
}
type immutableAttributes struct {
n ast.Node
}
func (a *immutableAttributes) Get(name []byte) (interface{}, bool) {
return a.n.Attribute(name)
}
func (a *immutableAttributes) GetString(name string) (interface{}, bool) {
return a.n.AttributeString(name)
}
func (a *immutableAttributes) All() []ast.Attribute {
if a.n.Attributes() == nil {
return []ast.Attribute{}
}
return a.n.Attributes()
}
// CodeBlockContext holds contextual information of code highlighting.
type CodeBlockContext interface {
// Language returns (language, true) if specified, otherwise (nil, false).
Language() ([]byte, bool)
// Highlighted returns true if this code block can be highlighted, otherwise false.
Highlighted() bool
// Attributes return attributes of the code block.
Attributes() ImmutableAttributes
}
type codeBlockContext struct {
language []byte
highlighted bool
attributes ImmutableAttributes
}
func newCodeBlockContext(language []byte, highlighted bool, attrs ImmutableAttributes) CodeBlockContext {
return &codeBlockContext{
language: language,
highlighted: highlighted,
attributes: attrs,
}
}
func (c *codeBlockContext) Language() ([]byte, bool) {
if c.language != nil {
return c.language, true
}
return nil, false
}
func (c *codeBlockContext) Highlighted() bool {
return c.highlighted
}
func (c *codeBlockContext) Attributes() ImmutableAttributes {
return c.attributes
}
// WrapperRenderer renders wrapper elements like div, pre, etc.
type WrapperRenderer func(w util.BufWriter, context CodeBlockContext, entering bool)
// CodeBlockOptions creates Chroma options per code block.
type CodeBlockOptions func(ctx CodeBlockContext) []chromahtml.Option
// Config struct holds options for the extension.
type Config struct {
html.Config
// Style is a highlighting style.
// Supported styles are defined under https://github.com/alecthomas/chroma/tree/master/formatters.
Style string
// FormatOptions is a option related to output formats.
// See https://github.com/alecthomas/chroma#the-html-formatter for details.
FormatOptions []chromahtml.Option
// CSSWriter is an io.Writer that will be used as CSS data output buffer.
// If WithClasses() is enabled, you can get CSS data corresponds to the style.
CSSWriter io.Writer
// CodeBlockOptions allows set Chroma options per code block.
CodeBlockOptions CodeBlockOptions
// WrapperRendererCodeBlockOptions allows you to change wrapper elements.
WrapperRenderer WrapperRenderer
}
// NewConfig returns a new Config with defaults.
func NewConfig() Config {
return Config{
Config: html.NewConfig(),
Style: "github",
FormatOptions: []chromahtml.Option{},
CSSWriter: nil,
WrapperRenderer: nil,
CodeBlockOptions: nil,
}
}
// SetOption implements renderer.SetOptioner.
func (c *Config) SetOption(name renderer.OptionName, value interface{}) {
switch name {
case optStyle:
c.Style = value.(string)
case optFormatOptions:
if value != nil {
c.FormatOptions = value.([]chromahtml.Option)
}
case optCSSWriter:
c.CSSWriter = value.(io.Writer)
case optWrapperRenderer:
c.WrapperRenderer = value.(WrapperRenderer)
case optCodeBlockOptions:
c.CodeBlockOptions = value.(CodeBlockOptions)
default:
c.Config.SetOption(name, value)
}
}
// Option interface is a functional option interface for the extension.
type Option interface {
renderer.Option
// SetHighlightingOption sets given option to the extension.
SetHighlightingOption(*Config)
}
type withHTMLOptions struct {
value []html.Option
}
func (o *withHTMLOptions) SetConfig(c *renderer.Config) {
if o.value != nil {
for _, v := range o.value {
v.(renderer.Option).SetConfig(c)
}
}
}
func (o *withHTMLOptions) SetHighlightingOption(c *Config) {
if o.value != nil {
for _, v := range o.value {
v.SetHTMLOption(&c.Config)
}
}
}
// WithHTMLOptions is functional option that wraps goldmark HTMLRenderer options.
func WithHTMLOptions(opts ...html.Option) Option {
return &withHTMLOptions{opts}
}
const optStyle renderer.OptionName = "HighlightingStyle"
var highlightLinesAttrName = []byte("hl_lines")
var styleAttrName = []byte("hl_style")
var nohlAttrName = []byte("nohl")
var linenosAttrName = []byte("linenos")
var linenosTableAttrValue = []byte("table")
var linenosInlineAttrValue = []byte("inline")
var linenostartAttrName = []byte("linenostart")
type withStyle struct {
value string
}
func (o *withStyle) SetConfig(c *renderer.Config) {
c.Options[optStyle] = o.value
}
func (o *withStyle) SetHighlightingOption(c *Config) {
c.Style = o.value
}
// WithStyle is a functional option that changes highlighting style.
func WithStyle(style string) Option {
return &withStyle{style}
}
const optCSSWriter renderer.OptionName = "HighlightingCSSWriter"
type withCSSWriter struct {
value io.Writer
}
func (o *withCSSWriter) SetConfig(c *renderer.Config) {
c.Options[optCSSWriter] = o.value
}
func (o *withCSSWriter) SetHighlightingOption(c *Config) {
c.CSSWriter = o.value
}
// WithCSSWriter is a functional option that sets io.Writer for CSS data.
func WithCSSWriter(w io.Writer) Option {
return &withCSSWriter{w}
}
const optWrapperRenderer renderer.OptionName = "HighlightingWrapperRenderer"
type withWrapperRenderer struct {
value WrapperRenderer
}
func (o *withWrapperRenderer) SetConfig(c *renderer.Config) {
c.Options[optWrapperRenderer] = o.value
}
func (o *withWrapperRenderer) SetHighlightingOption(c *Config) {
c.WrapperRenderer = o.value
}
// WithWrapperRenderer is a functional option that sets WrapperRenderer that
// renders wrapper elements like div, pre, etc.
func WithWrapperRenderer(w WrapperRenderer) Option {
return &withWrapperRenderer{w}
}
const optCodeBlockOptions renderer.OptionName = "HighlightingCodeBlockOptions"
type withCodeBlockOptions struct {
value CodeBlockOptions
}
func (o *withCodeBlockOptions) SetConfig(c *renderer.Config) {
c.Options[optWrapperRenderer] = o.value
}
func (o *withCodeBlockOptions) SetHighlightingOption(c *Config) {
c.CodeBlockOptions = o.value
}
// WithCodeBlockOptions is a functional option that sets CodeBlockOptions that
// allows setting Chroma options per code block.
func WithCodeBlockOptions(c CodeBlockOptions) Option {
return &withCodeBlockOptions{value: c}
}
const optFormatOptions renderer.OptionName = "HighlightingFormatOptions"
type withFormatOptions struct {
value []chromahtml.Option
}
func (o *withFormatOptions) SetConfig(c *renderer.Config) {
if _, ok := c.Options[optFormatOptions]; !ok {
c.Options[optFormatOptions] = []chromahtml.Option{}
}
c.Options[optStyle] = append(c.Options[optFormatOptions].([]chromahtml.Option), o.value...)
}
func (o *withFormatOptions) SetHighlightingOption(c *Config) {
c.FormatOptions = append(c.FormatOptions, o.value...)
}
// WithFormatOptions is a functional option that wraps chroma HTML formatter options.
func WithFormatOptions(opts ...chromahtml.Option) Option {
return &withFormatOptions{opts}
}
// HTMLRenderer struct is a renderer.NodeRenderer implementation for the extension.
type HTMLRenderer struct {
Config
}
// NewHTMLRenderer builds a new HTMLRenderer with given options and returns it.
func NewHTMLRenderer(opts ...Option) renderer.NodeRenderer {
r := &HTMLRenderer{
Config: NewConfig(),
}
for _, opt := range opts {
opt.SetHighlightingOption(&r.Config)
}
return r
}
// RegisterFuncs implements NodeRenderer.RegisterFuncs.
func (r *HTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
reg.Register(ast.KindFencedCodeBlock, r.renderFencedCodeBlock)
}
func getAttributes(node *ast.FencedCodeBlock, infostr []byte) ImmutableAttributes {
if node.Attributes() != nil {
return &immutableAttributes{node}
}
if infostr != nil {
attrStartIdx := -1
for idx, char := range infostr {
if char == '{' {
attrStartIdx = idx
break
}
}
if attrStartIdx > 0 {
n := ast.NewTextBlock() // dummy node for storing attributes
attrStr := infostr[attrStartIdx:]
if attrs, hasAttr := parser.ParseAttributes(text.NewReader(attrStr)); hasAttr {
for _, attr := range attrs {
n.SetAttribute(attr.Name, attr.Value)
}
return &immutableAttributes{n}
}
}
}
return nil
}
func (r *HTMLRenderer) renderFencedCodeBlock(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
n := node.(*ast.FencedCodeBlock)
if !entering {
return ast.WalkContinue, nil
}
language := n.Language(source)
chromaFormatterOptions := make([]chromahtml.Option, len(r.FormatOptions))
copy(chromaFormatterOptions, r.FormatOptions)
style := styles.Get(r.Style)
nohl := false
var info []byte
if n.Info != nil {
info = n.Info.Segment.Value(source)
}
attrs := getAttributes(n, info)
if attrs != nil {
baseLineNumber := 1
if linenostartAttr, ok := attrs.Get(linenostartAttrName); ok {
baseLineNumber = int(linenostartAttr.(float64))
chromaFormatterOptions = append(chromaFormatterOptions, chromahtml.BaseLineNumber(baseLineNumber))
}
if linesAttr, hasLinesAttr := attrs.Get(highlightLinesAttrName); hasLinesAttr {
if lines, ok := linesAttr.([]interface{}); ok {
var hlRanges [][2]int
for _, l := range lines {
if ln, ok := l.(float64); ok {
hlRanges = append(hlRanges, [2]int{int(ln) + baseLineNumber - 1, int(ln) + baseLineNumber - 1})
}
if rng, ok := l.([]uint8); ok {
slices := strings.Split(string([]byte(rng)), "-")
lhs, err := strconv.Atoi(slices[0])
if err != nil {
continue
}
rhs := lhs
if len(slices) > 1 {
rhs, err = strconv.Atoi(slices[1])
if err != nil {
continue
}
}
hlRanges = append(hlRanges, [2]int{lhs + baseLineNumber - 1, rhs + baseLineNumber - 1})
}
}
chromaFormatterOptions = append(chromaFormatterOptions, chromahtml.HighlightLines(hlRanges))
}
}
if styleAttr, hasStyleAttr := attrs.Get(styleAttrName); hasStyleAttr {
styleStr := string([]byte(styleAttr.([]uint8)))
style = styles.Get(styleStr)
}
if _, hasNohlAttr := attrs.Get(nohlAttrName); hasNohlAttr {
nohl = true
}
if linenosAttr, ok := attrs.Get(linenosAttrName); ok {
switch v := linenosAttr.(type) {
case bool:
chromaFormatterOptions = append(chromaFormatterOptions, chromahtml.WithLineNumbers(v))
case []uint8:
if v != nil {
chromaFormatterOptions = append(chromaFormatterOptions, chromahtml.WithLineNumbers(true))
}
if bytes.Equal(v, linenosTableAttrValue) {
chromaFormatterOptions = append(chromaFormatterOptions, chromahtml.LineNumbersInTable(true))
} else if bytes.Equal(v, linenosInlineAttrValue) {
chromaFormatterOptions = append(chromaFormatterOptions, chromahtml.LineNumbersInTable(false))
}
}
}
}
var lexer chroma.Lexer
if language != nil {
lexer = lexers.Get(string(language))
}
if !nohl && lexer != nil {
if style == nil {
style = styles.Fallback
}
var buffer bytes.Buffer
l := n.Lines().Len()
for i := 0; i < l; i++ {
line := n.Lines().At(i)
buffer.Write(line.Value(source))
}
iterator, err := lexer.Tokenise(nil, buffer.String())
if err == nil {
c := newCodeBlockContext(language, true, attrs)
if r.CodeBlockOptions != nil {
chromaFormatterOptions = append(chromaFormatterOptions, r.CodeBlockOptions(c)...)
}
formatter := chromahtml.New(chromaFormatterOptions...)
if r.WrapperRenderer != nil {
r.WrapperRenderer(w, c, true)
}
_ = formatter.Format(w, style, iterator) == nil
if r.WrapperRenderer != nil {
r.WrapperRenderer(w, c, false)
}
if r.CSSWriter != nil {
_ = formatter.WriteCSS(r.CSSWriter, style)
}
return ast.WalkContinue, nil
}
}
var c CodeBlockContext
if r.WrapperRenderer != nil {
c = newCodeBlockContext(language, false, attrs)
r.WrapperRenderer(w, c, true)
} else {
_, _ = w.WriteString("<pre><code")
language := n.Language(source)
if language != nil {
_, _ = w.WriteString(" class=\"language-")
r.Writer.Write(w, language)
_, _ = w.WriteString("\"")
}
_ = w.WriteByte('>')
}
l := n.Lines().Len()
for i := 0; i < l; i++ {
line := n.Lines().At(i)
r.Writer.RawWrite(w, line.Value(source))
}
if r.WrapperRenderer != nil {
r.WrapperRenderer(w, c, false)
} else {
_, _ = w.WriteString("</code></pre>\n")
}
return ast.WalkContinue, nil
}
type highlighting struct {
options []Option
}
// Highlighting is a goldmark.Extender implementation.
var Highlighting = &highlighting{
options: []Option{},
}
// NewHighlighting returns a new extension with given options.
func NewHighlighting(opts ...Option) goldmark.Extender {
return &highlighting{
options: opts,
}
}
// Extend implements goldmark.Extender.
func (e *highlighting) Extend(m goldmark.Markdown) {
m.Renderer().AddOptions(renderer.WithNodeRenderers(
util.Prioritized(NewHTMLRenderer(e.options...), 200),
))
}

View File

@@ -0,0 +1,335 @@
// TODO(bep) this is a very temporary fork based on https://github.com/yuin/goldmark-highlighting/pull/10
// MIT Licensed, Copyright Yusuke Inuzuka
package temphighlighting
import (
"bytes"
"fmt"
"strings"
"testing"
"github.com/yuin/goldmark/util"
chromahtml "github.com/alecthomas/chroma/formatters/html"
"github.com/yuin/goldmark"
)
type preWrapper struct {
language string
}
func (p preWrapper) Start(code bool, styleAttr string) string {
w := &strings.Builder{}
fmt.Fprintf(w, "<pre%s><code", styleAttr)
if p.language != "" {
fmt.Fprintf(w, " class=\"language-"+p.language)
}
fmt.Fprint(w, ">")
return w.String()
}
func (p preWrapper) End(code bool) string {
return "</code></pre>"
}
func TestHighlighting(t *testing.T) {
var css bytes.Buffer
markdown := goldmark.New(
goldmark.WithExtensions(
NewHighlighting(
WithStyle("monokai"),
WithCSSWriter(&css),
WithFormatOptions(
chromahtml.WithClasses(true),
chromahtml.WithLineNumbers(false),
),
WithWrapperRenderer(func(w util.BufWriter, c CodeBlockContext, entering bool) {
_, ok := c.Language()
if entering {
if !ok {
w.WriteString("<pre><code>")
return
}
w.WriteString(`<div class="highlight">`)
} else {
if !ok {
w.WriteString("</pre></code>")
return
}
w.WriteString(`</div>`)
}
}),
WithCodeBlockOptions(func(c CodeBlockContext) []chromahtml.Option {
if language, ok := c.Language(); ok {
// Turn on line numbers for Go only.
if string(language) == "go" {
return []chromahtml.Option{
chromahtml.WithLineNumbers(true),
}
}
}
return nil
}),
),
),
)
var buffer bytes.Buffer
if err := markdown.Convert([]byte(`
Title
=======
`+"``` go\n"+`func main() {
fmt.Println("ok")
}
`+"```"+`
`), &buffer); err != nil {
t.Fatal(err)
}
if strings.TrimSpace(buffer.String()) != strings.TrimSpace(`
<h1>Title</h1>
<div class="highlight"><pre class="chroma"><span class="ln">1</span><span class="kd">func</span> <span class="nf">main</span><span class="p">(</span><span class="p">)</span> <span class="p">{</span>
<span class="ln">2</span> <span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;ok&#34;</span><span class="p">)</span>
<span class="ln">3</span><span class="p">}</span>
</pre></div>
`) {
t.Error("failed to render HTML")
}
if strings.TrimSpace(css.String()) != strings.TrimSpace(`/* Background */ .chroma { color: #f8f8f2; background-color: #272822 }
/* Error */ .chroma .err { color: #960050; background-color: #1e0010 }
/* LineTableTD */ .chroma .lntd { vertical-align: top; padding: 0; margin: 0; border: 0; }
/* LineTable */ .chroma .lntable { border-spacing: 0; padding: 0; margin: 0; border: 0; width: auto; overflow: auto; display: block; }
/* LineHighlight */ .chroma .hl { display: block; width: 100%;background-color: #3c3d38 }
/* LineNumbersTable */ .chroma .lnt { margin-right: 0.4em; padding: 0 0.4em 0 0.4em;color: #7f7f7f }
/* LineNumbers */ .chroma .ln { margin-right: 0.4em; padding: 0 0.4em 0 0.4em;color: #7f7f7f }
/* Keyword */ .chroma .k { color: #66d9ef }
/* KeywordConstant */ .chroma .kc { color: #66d9ef }
/* KeywordDeclaration */ .chroma .kd { color: #66d9ef }
/* KeywordNamespace */ .chroma .kn { color: #f92672 }
/* KeywordPseudo */ .chroma .kp { color: #66d9ef }
/* KeywordReserved */ .chroma .kr { color: #66d9ef }
/* KeywordType */ .chroma .kt { color: #66d9ef }
/* NameAttribute */ .chroma .na { color: #a6e22e }
/* NameClass */ .chroma .nc { color: #a6e22e }
/* NameConstant */ .chroma .no { color: #66d9ef }
/* NameDecorator */ .chroma .nd { color: #a6e22e }
/* NameException */ .chroma .ne { color: #a6e22e }
/* NameFunction */ .chroma .nf { color: #a6e22e }
/* NameOther */ .chroma .nx { color: #a6e22e }
/* NameTag */ .chroma .nt { color: #f92672 }
/* Literal */ .chroma .l { color: #ae81ff }
/* LiteralDate */ .chroma .ld { color: #e6db74 }
/* LiteralString */ .chroma .s { color: #e6db74 }
/* LiteralStringAffix */ .chroma .sa { color: #e6db74 }
/* LiteralStringBacktick */ .chroma .sb { color: #e6db74 }
/* LiteralStringChar */ .chroma .sc { color: #e6db74 }
/* LiteralStringDelimiter */ .chroma .dl { color: #e6db74 }
/* LiteralStringDoc */ .chroma .sd { color: #e6db74 }
/* LiteralStringDouble */ .chroma .s2 { color: #e6db74 }
/* LiteralStringEscape */ .chroma .se { color: #ae81ff }
/* LiteralStringHeredoc */ .chroma .sh { color: #e6db74 }
/* LiteralStringInterpol */ .chroma .si { color: #e6db74 }
/* LiteralStringOther */ .chroma .sx { color: #e6db74 }
/* LiteralStringRegex */ .chroma .sr { color: #e6db74 }
/* LiteralStringSingle */ .chroma .s1 { color: #e6db74 }
/* LiteralStringSymbol */ .chroma .ss { color: #e6db74 }
/* LiteralNumber */ .chroma .m { color: #ae81ff }
/* LiteralNumberBin */ .chroma .mb { color: #ae81ff }
/* LiteralNumberFloat */ .chroma .mf { color: #ae81ff }
/* LiteralNumberHex */ .chroma .mh { color: #ae81ff }
/* LiteralNumberInteger */ .chroma .mi { color: #ae81ff }
/* LiteralNumberIntegerLong */ .chroma .il { color: #ae81ff }
/* LiteralNumberOct */ .chroma .mo { color: #ae81ff }
/* Operator */ .chroma .o { color: #f92672 }
/* OperatorWord */ .chroma .ow { color: #f92672 }
/* Comment */ .chroma .c { color: #75715e }
/* CommentHashbang */ .chroma .ch { color: #75715e }
/* CommentMultiline */ .chroma .cm { color: #75715e }
/* CommentSingle */ .chroma .c1 { color: #75715e }
/* CommentSpecial */ .chroma .cs { color: #75715e }
/* CommentPreproc */ .chroma .cp { color: #75715e }
/* CommentPreprocFile */ .chroma .cpf { color: #75715e }
/* GenericDeleted */ .chroma .gd { color: #f92672 }
/* GenericEmph */ .chroma .ge { font-style: italic }
/* GenericInserted */ .chroma .gi { color: #a6e22e }
/* GenericStrong */ .chroma .gs { font-weight: bold }
/* GenericSubheading */ .chroma .gu { color: #75715e }`) {
t.Error("failed to render CSS")
}
}
func TestHighlighting2(t *testing.T) {
markdown := goldmark.New(
goldmark.WithExtensions(
Highlighting,
),
)
var buffer bytes.Buffer
if err := markdown.Convert([]byte(`
Title
=======
`+"```"+`
func main() {
fmt.Println("ok")
}
`+"```"+`
`), &buffer); err != nil {
t.Fatal(err)
}
if strings.TrimSpace(buffer.String()) != strings.TrimSpace(`
<h1>Title</h1>
<pre><code>func main() {
fmt.Println(&quot;ok&quot;)
}
</code></pre>
`) {
t.Error("failed to render HTML")
}
}
func TestHighlighting3(t *testing.T) {
markdown := goldmark.New(
goldmark.WithExtensions(
Highlighting,
),
)
var buffer bytes.Buffer
if err := markdown.Convert([]byte(`
Title
=======
`+"```"+`cpp {hl_lines=[1,2]}
#include <iostream>
int main() {
std::cout<< "hello" << std::endl;
}
`+"```"+`
`), &buffer); err != nil {
t.Fatal(err)
}
if strings.TrimSpace(buffer.String()) != strings.TrimSpace(`
<h1>Title</h1>
<pre style="background-color:#fff"><span style="display:block;width:100%;background-color:#e5e5e5"><span style="color:#999;font-weight:bold;font-style:italic">#</span><span style="color:#999;font-weight:bold;font-style:italic">include</span> <span style="color:#999;font-weight:bold;font-style:italic">&lt;iostream&gt;</span><span style="color:#999;font-weight:bold;font-style:italic">
</span></span><span style="display:block;width:100%;background-color:#e5e5e5"><span style="color:#999;font-weight:bold;font-style:italic"></span><span style="color:#458;font-weight:bold">int</span> <span style="color:#900;font-weight:bold">main</span>() {
</span> std<span style="color:#000;font-weight:bold">:</span><span style="color:#000;font-weight:bold">:</span>cout<span style="color:#000;font-weight:bold">&lt;</span><span style="color:#000;font-weight:bold">&lt;</span> <span style="color:#d14"></span><span style="color:#d14">&#34;</span><span style="color:#d14">hello</span><span style="color:#d14">&#34;</span> <span style="color:#000;font-weight:bold">&lt;</span><span style="color:#000;font-weight:bold">&lt;</span> std<span style="color:#000;font-weight:bold">:</span><span style="color:#000;font-weight:bold">:</span>endl;
}
</pre>
`) {
t.Error("failed to render HTML")
}
}
func TestHighlightingHlLines(t *testing.T) {
markdown := goldmark.New(
goldmark.WithExtensions(
NewHighlighting(
WithFormatOptions(
chromahtml.WithClasses(true),
),
),
),
)
for i, test := range []struct {
attributes string
expect []int
}{
{`hl_lines=["2"]`, []int{2}},
{`hl_lines=["2-3",5],linenostart=5`, []int{2, 3, 5}},
{`hl_lines=["2-3"]`, []int{2, 3}},
} {
t.Run(fmt.Sprint(i), func(t *testing.T) {
var buffer bytes.Buffer
codeBlock := fmt.Sprintf(`bash {%s}
LINE1
LINE2
LINE3
LINE4
LINE5
LINE6
LINE7
LINE8
`, test.attributes)
if err := markdown.Convert([]byte(`
`+"```"+codeBlock+"```"+`
`), &buffer); err != nil {
t.Fatal(err)
}
for _, line := range test.expect {
expectStr := fmt.Sprintf("<span class=\"hl\">LINE%d\n</span>", line)
if !strings.Contains(buffer.String(), expectStr) {
t.Fatal("got\n", buffer.String(), "\nexpected\n", expectStr)
}
}
})
}
}
func TestHighlightingLinenos(t *testing.T) {
outputLineNumbersInTable := `<div class="chroma">
<table class="lntable"><tr><td class="lntd">
<span class="lnt">1
</span></td>
<td class="lntd">
LINE1
</td></tr></table>
</div>`
for i, test := range []struct {
attributes string
lineNumbers bool
lineNumbersInTable bool
expect string
}{
{`linenos=true`, false, false, `<span class="ln">1</span>LINE1`},
{`linenos=false`, false, false, `LINE1`},
{``, true, false, `<span class="ln">1</span>LINE1`},
{``, true, true, outputLineNumbersInTable},
{`linenos=inline`, true, true, `<span class="ln">1</span>LINE1`},
{`linenos=foo`, false, false, `<span class="ln">1</span>LINE1`},
{`linenos=table`, false, false, outputLineNumbersInTable},
} {
t.Run(fmt.Sprint(i), func(t *testing.T) {
markdown := goldmark.New(
goldmark.WithExtensions(
NewHighlighting(
WithFormatOptions(
chromahtml.WithLineNumbers(test.lineNumbers),
chromahtml.LineNumbersInTable(test.lineNumbersInTable),
chromahtml.PreventSurroundingPre(true),
chromahtml.WithClasses(true),
),
),
),
)
var buffer bytes.Buffer
codeBlock := fmt.Sprintf(`bash {%s}
LINE1
`, test.attributes)
content := "```" + codeBlock + "```"
if err := markdown.Convert([]byte(content), &buffer); err != nil {
t.Fatal(err)
}
s := strings.TrimSpace(buffer.String())
if s != test.expect {
t.Fatal("got\n", s, "\nexpected\n", test.expect)
}
})
}
}

View File

@@ -1,108 +0,0 @@
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package helpers implements general utility functions that work with
// and on content. The helper functions defined here lay down the
// foundation of how Hugo works with files and filepaths, and perform
// string operations on content.
package internal
import (
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/markup/converter"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
)
// BlackFriday holds configuration values for BlackFriday rendering.
// It is kept here because it's used in several packages.
type BlackFriday struct {
Smartypants bool
SmartypantsQuotesNBSP bool
AngledQuotes bool
Fractions bool
HrefTargetBlank bool
NofollowLinks bool
NoreferrerLinks bool
SmartDashes bool
LatexDashes bool
TaskLists bool
PlainIDAnchors bool
Extensions []string
ExtensionsMask []string
SkipHTML bool
FootnoteAnchorPrefix string
FootnoteReturnLinkContents string
}
func UpdateBlackFriday(old *BlackFriday, m map[string]interface{}) (*BlackFriday, error) {
// Create a copy so we can modify it.
bf := *old
if err := mapstructure.Decode(m, &bf); err != nil {
return nil, errors.WithMessage(err, "failed to decode rendering config")
}
return &bf, nil
}
// NewBlackfriday creates a new Blackfriday filled with site config or some sane defaults.
func NewBlackfriday(cfg converter.ProviderConfig) (*BlackFriday, error) {
var siteConfig map[string]interface{}
if cfg.Cfg != nil {
siteConfig = cfg.Cfg.GetStringMap("blackfriday")
}
defaultParam := map[string]interface{}{
"smartypants": true,
"angledQuotes": false,
"smartypantsQuotesNBSP": false,
"fractions": true,
"hrefTargetBlank": false,
"nofollowLinks": false,
"noreferrerLinks": false,
"smartDashes": true,
"latexDashes": true,
"plainIDAnchors": true,
"taskLists": true,
"skipHTML": false,
}
maps.ToLower(defaultParam)
config := make(map[string]interface{})
for k, v := range defaultParam {
config[k] = v
}
for k, v := range siteConfig {
config[k] = v
}
combinedConfig := &BlackFriday{}
if err := mapstructure.Decode(config, combinedConfig); err != nil {
return nil, errors.Errorf("failed to decode Blackfriday config: %s", err)
}
// TODO(bep) update/consolidate docs
if combinedConfig.FootnoteAnchorPrefix == "" {
combinedConfig.FootnoteAnchorPrefix = cfg.Cfg.GetString("footnoteAnchorPrefix")
}
if combinedConfig.FootnoteReturnLinkContents == "" {
combinedConfig.FootnoteReturnLinkContents = cfg.Cfg.GetString("footnoteReturnLinkContents")
}
return combinedConfig, nil
}

View File

@@ -16,6 +16,12 @@ package markup
import (
"strings"
"github.com/gohugoio/hugo/markup/highlight"
"github.com/gohugoio/hugo/markup/markup_config"
"github.com/gohugoio/hugo/markup/goldmark"
"github.com/gohugoio/hugo/markup/org"
"github.com/gohugoio/hugo/markup/asciidoc"
@@ -29,39 +35,71 @@ import (
func NewConverterProvider(cfg converter.ProviderConfig) (ConverterProvider, error) {
converters := make(map[string]converter.Provider)
add := func(p converter.NewProvider, aliases ...string) error {
markupConfig, err := markup_config.Decode(cfg.Cfg)
if err != nil {
return nil, err
}
if cfg.Highlight == nil {
h := highlight.New(markupConfig.Highlight)
cfg.Highlight = func(code, lang, optsStr string) (string, error) {
return h.Highlight(code, lang, optsStr)
}
}
cfg.MarkupConfig = markupConfig
add := func(p converter.ProviderProvider, aliases ...string) error {
c, err := p.New(cfg)
if err != nil {
return err
}
name := c.Name()
aliases = append(aliases, name)
if strings.EqualFold(name, cfg.MarkupConfig.DefaultMarkdownHandler) {
aliases = append(aliases, "markdown")
}
addConverter(converters, c, aliases...)
return nil
}
if err := add(blackfriday.Provider, "md", "markdown", "blackfriday"); err != nil {
if err := add(goldmark.Provider); err != nil {
return nil, err
}
if err := add(mmark.Provider, "mmark"); err != nil {
if err := add(blackfriday.Provider); err != nil {
return nil, err
}
if err := add(asciidoc.Provider, "asciidoc"); err != nil {
if err := add(mmark.Provider); err != nil {
return nil, err
}
if err := add(rst.Provider, "rst"); err != nil {
if err := add(asciidoc.Provider, "ad", "adoc"); err != nil {
return nil, err
}
if err := add(pandoc.Provider, "pandoc"); err != nil {
if err := add(rst.Provider); err != nil {
return nil, err
}
if err := add(org.Provider, "org"); err != nil {
if err := add(pandoc.Provider, "pdc"); err != nil {
return nil, err
}
if err := add(org.Provider); err != nil {
return nil, err
}
return &converterRegistry{converters: converters}, nil
return &converterRegistry{
config: cfg,
converters: converters,
}, nil
}
type ConverterProvider interface {
Get(name string) converter.Provider
//Default() converter.Provider
GetMarkupConfig() markup_config.Config
Highlight(code, lang, optsStr string) (string, error)
}
type converterRegistry struct {
@@ -70,12 +108,22 @@ type converterRegistry struct {
// may be registered multiple times.
// All names are lower case.
converters map[string]converter.Provider
config converter.ProviderConfig
}
func (r *converterRegistry) Get(name string) converter.Provider {
return r.converters[strings.ToLower(name)]
}
func (r *converterRegistry) Highlight(code, lang, optsStr string) (string, error) {
return r.config.Highlight(code, lang, optsStr)
}
func (r *converterRegistry) GetMarkupConfig() markup_config.Config {
return r.config.MarkupConfig
}
func addConverter(m map[string]converter.Provider, c converter.Provider, aliases ...string) {
for _, alias := range aliases {
m[alias] = c

View File

@@ -0,0 +1,105 @@
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package markup_config
import (
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/docshelper"
"github.com/gohugoio/hugo/markup/blackfriday/blackfriday_config"
"github.com/gohugoio/hugo/markup/goldmark/goldmark_config"
"github.com/gohugoio/hugo/markup/highlight"
"github.com/gohugoio/hugo/markup/tableofcontents"
"github.com/gohugoio/hugo/parser"
"github.com/mitchellh/mapstructure"
)
type Config struct {
// Default markdown handler for md/markdown extensions.
// Default is "goldmark".
// Before Hugo 0.60 this was "blackfriday".
DefaultMarkdownHandler string
Highlight highlight.Config
TableOfContents tableofcontents.Config
// Content renderers
Goldmark goldmark_config.Config
BlackFriday blackfriday_config.Config
}
func Decode(cfg config.Provider) (conf Config, err error) {
conf = Default
m := cfg.GetStringMap("markup")
if m == nil {
return
}
err = mapstructure.WeakDecode(m, &conf)
if err != nil {
return
}
if err = applyLegacyConfig(cfg, &conf); err != nil {
return
}
if err = highlight.ApplyLegacyConfig(cfg, &conf.Highlight); err != nil {
return
}
return
}
func applyLegacyConfig(cfg config.Provider, conf *Config) error {
if bm := cfg.GetStringMap("blackfriday"); bm != nil {
// Legacy top level blackfriday config.
err := mapstructure.WeakDecode(bm, &conf.BlackFriday)
if err != nil {
return err
}
}
if conf.BlackFriday.FootnoteAnchorPrefix == "" {
conf.BlackFriday.FootnoteAnchorPrefix = cfg.GetString("footnoteAnchorPrefix")
}
if conf.BlackFriday.FootnoteReturnLinkContents == "" {
conf.BlackFriday.FootnoteReturnLinkContents = cfg.GetString("footnoteReturnLinkContents")
}
return nil
}
var Default = Config{
DefaultMarkdownHandler: "goldmark",
TableOfContents: tableofcontents.DefaultConfig,
Highlight: highlight.DefaultConfig,
Goldmark: goldmark_config.Default,
BlackFriday: blackfriday_config.Default,
}
func init() {
docsProvider := func() map[string]interface{} {
docs := make(map[string]interface{})
docs["markup"] = parser.LowerCaseCamelJSONMarshaller{Value: Default}
return docs
}
// TODO(bep) merge maps
docshelper.AddDocProvider("config", docsProvider)
}

View File

@@ -0,0 +1,69 @@
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package markup_config
import (
"testing"
"github.com/spf13/viper"
qt "github.com/frankban/quicktest"
)
func TestConfig(t *testing.T) {
c := qt.New(t)
c.Run("Decode", func(c *qt.C) {
c.Parallel()
v := viper.New()
v.Set("markup", map[string]interface{}{
"goldmark": map[string]interface{}{
"renderer": map[string]interface{}{
"unsafe": true,
},
},
})
conf, err := Decode(v)
c.Assert(err, qt.IsNil)
c.Assert(conf.Goldmark.Renderer.Unsafe, qt.Equals, true)
c.Assert(conf.BlackFriday.Fractions, qt.Equals, true)
})
c.Run("legacy", func(c *qt.C) {
c.Parallel()
v := viper.New()
v.Set("blackfriday", map[string]interface{}{
"angledQuotes": true,
})
v.Set("footnoteAnchorPrefix", "myprefix")
v.Set("footnoteReturnLinkContents", "myreturn")
v.Set("pygmentsStyle", "hugo")
conf, err := Decode(v)
c.Assert(err, qt.IsNil)
c.Assert(conf.BlackFriday.AngledQuotes, qt.Equals, true)
c.Assert(conf.BlackFriday.FootnoteAnchorPrefix, qt.Equals, "myprefix")
c.Assert(conf.BlackFriday.FootnoteReturnLinkContents, qt.Equals, "myreturn")
c.Assert(conf.Highlight.Style, qt.Equals, "hugo")
c.Assert(conf.Highlight.CodeFences, qt.Equals, true)
})
}

View File

@@ -29,13 +29,23 @@ func TestConverterRegistry(t *testing.T) {
r, err := NewConverterProvider(converter.ProviderConfig{Cfg: viper.New()})
c.Assert(err, qt.IsNil)
c.Assert("goldmark", qt.Equals, r.GetMarkupConfig().DefaultMarkdownHandler)
checkName := func(name string) {
p := r.Get(name)
c.Assert(p, qt.Not(qt.IsNil))
c.Assert(p.Name(), qt.Equals, name)
}
c.Assert(r.Get("foo"), qt.IsNil)
c.Assert(r.Get("markdown"), qt.Not(qt.IsNil))
c.Assert(r.Get("mmark"), qt.Not(qt.IsNil))
c.Assert(r.Get("asciidoc"), qt.Not(qt.IsNil))
c.Assert(r.Get("rst"), qt.Not(qt.IsNil))
c.Assert(r.Get("pandoc"), qt.Not(qt.IsNil))
c.Assert(r.Get("org"), qt.Not(qt.IsNil))
c.Assert(r.Get("markdown").Name(), qt.Equals, "goldmark")
checkName("goldmark")
checkName("mmark")
checkName("asciidoc")
checkName("rst")
checkName("pandoc")
checkName("org")
checkName("blackfriday")
}

View File

@@ -15,33 +15,28 @@
package mmark
import (
"github.com/gohugoio/hugo/markup/internal"
"github.com/gohugoio/hugo/markup/blackfriday/blackfriday_config"
"github.com/gohugoio/hugo/markup/converter"
"github.com/miekg/mmark"
)
// Provider is the package entry point.
var Provider converter.NewProvider = provider{}
var Provider converter.ProviderProvider = provider{}
type provider struct {
}
func (p provider) New(cfg converter.ProviderConfig) (converter.Provider, error) {
defaultBlackFriday, err := internal.NewBlackfriday(cfg)
if err != nil {
return nil, err
}
defaultBlackFriday := cfg.MarkupConfig.BlackFriday
defaultExtensions := getMmarkExtensions(defaultBlackFriday)
var n converter.NewConverter = func(ctx converter.DocumentContext) (converter.Converter, error) {
return converter.NewProvider("mmark", func(ctx converter.DocumentContext) (converter.Converter, error) {
b := defaultBlackFriday
extensions := defaultExtensions
if ctx.ConfigOverrides != nil {
var err error
b, err = internal.UpdateBlackFriday(b, ctx.ConfigOverrides)
b, err = blackfriday_config.UpdateConfig(b, ctx.ConfigOverrides)
if err != nil {
return nil, err
}
@@ -54,16 +49,14 @@ func (p provider) New(cfg converter.ProviderConfig) (converter.Provider, error)
extensions: extensions,
cfg: cfg,
}, nil
}
return n, nil
}), nil
}
type mmarkConverter struct {
ctx converter.DocumentContext
extensions int
b *internal.BlackFriday
b blackfriday_config.Config
cfg converter.ProviderConfig
}
@@ -74,7 +67,7 @@ func (c *mmarkConverter) Convert(ctx converter.RenderContext) (converter.Result,
func getHTMLRenderer(
ctx converter.DocumentContext,
cfg *internal.BlackFriday,
cfg blackfriday_config.Config,
pcfg converter.ProviderConfig) mmark.Renderer {
var (
@@ -97,15 +90,14 @@ func getHTMLRenderer(
htmlFlags |= mmark.HTML_FOOTNOTE_RETURN_LINKS
return &mmarkRenderer{
Config: cfg,
Cfg: pcfg.Cfg,
highlight: pcfg.Highlight,
Renderer: mmark.HtmlRendererWithParameters(htmlFlags, "", "", renderParameters),
BlackfridayConfig: cfg,
Config: pcfg,
Renderer: mmark.HtmlRendererWithParameters(htmlFlags, "", "", renderParameters),
}
}
func getMmarkExtensions(cfg *internal.BlackFriday) int {
func getMmarkExtensions(cfg blackfriday_config.Config) int {
flags := 0
flags |= mmark.EXTENSION_TABLES
flags |= mmark.EXTENSION_FENCED_CODE

View File

@@ -20,19 +20,14 @@ import (
"github.com/gohugoio/hugo/common/loggers"
"github.com/miekg/mmark"
"github.com/gohugoio/hugo/markup/internal"
"github.com/gohugoio/hugo/markup/converter"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/markup/blackfriday/blackfriday_config"
"github.com/gohugoio/hugo/markup/converter"
"github.com/miekg/mmark"
)
func TestGetMmarkExtensions(t *testing.T) {
c := qt.New(t)
b, err := internal.NewBlackfriday(converter.ProviderConfig{Cfg: viper.New()})
c.Assert(err, qt.IsNil)
b := blackfriday_config.Default
//TODO: This is doing the same just with different marks...
type data struct {

View File

@@ -17,26 +17,24 @@ import (
"bytes"
"strings"
"github.com/gohugoio/hugo/markup/blackfriday/blackfriday_config"
"github.com/gohugoio/hugo/markup/converter"
"github.com/miekg/mmark"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/markup/internal"
)
// hugoHTMLRenderer wraps a blackfriday.Renderer, typically a blackfriday.Html
// adding some custom behaviour.
type mmarkRenderer struct {
Cfg config.Provider
Config *internal.BlackFriday
highlight func(code, lang, optsStr string) (string, error)
Config converter.ProviderConfig
BlackfridayConfig blackfriday_config.Config
mmark.Renderer
}
// BlockCode renders a given text as a block of code.
func (r *mmarkRenderer) BlockCode(out *bytes.Buffer, text []byte, lang string, caption []byte, subfigure bool, callouts bool) {
if r.Cfg.GetBool("pygmentsCodeFences") && (lang != "" || r.Cfg.GetBool("pygmentsCodeFencesGuessSyntax")) {
if r.Config.MarkupConfig.Highlight.CodeFences {
str := strings.Trim(string(text), "\n\r")
highlighted, _ := r.highlight(str, lang, "")
highlighted, _ := r.Config.Highlight(str, lang, "")
out.WriteString(highlighted)
} else {
r.Renderer.BlockCode(out, text, lang, caption, subfigure, callouts)

View File

@@ -23,19 +23,18 @@ import (
)
// Provider is the package entry point.
var Provider converter.NewProvider = provide{}
var Provider converter.ProviderProvider = provide{}
type provide struct {
}
func (p provide) New(cfg converter.ProviderConfig) (converter.Provider, error) {
var n converter.NewConverter = func(ctx converter.DocumentContext) (converter.Converter, error) {
return converter.NewProvider("org", func(ctx converter.DocumentContext) (converter.Converter, error) {
return &orgConverter{
ctx: ctx,
cfg: cfg,
}, nil
}
return n, nil
}), nil
}
type orgConverter struct {

View File

@@ -23,19 +23,18 @@ import (
)
// Provider is the package entry point.
var Provider converter.NewProvider = provider{}
var Provider converter.ProviderProvider = provider{}
type provider struct {
}
func (p provider) New(cfg converter.ProviderConfig) (converter.Provider, error) {
var n converter.NewConverter = func(ctx converter.DocumentContext) (converter.Converter, error) {
return converter.NewProvider("pandoc", func(ctx converter.DocumentContext) (converter.Converter, error) {
return &pandocConverter{
ctx: ctx,
cfg: cfg,
}, nil
}
return n, nil
}), nil
}

View File

@@ -25,20 +25,18 @@ import (
)
// Provider is the package entry point.
var Provider converter.NewProvider = provider{}
var Provider converter.ProviderProvider = provider{}
type provider struct {
}
func (p provider) New(cfg converter.ProviderConfig) (converter.Provider, error) {
var n converter.NewConverter = func(ctx converter.DocumentContext) (converter.Converter, error) {
return converter.NewProvider("rst", func(ctx converter.DocumentContext) (converter.Converter, error) {
return &rstConverter{
ctx: ctx,
cfg: cfg,
}, nil
}
return n, nil
}), nil
}
type rstConverter struct {

View File

@@ -0,0 +1,148 @@
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tableofcontents
import (
"strings"
)
type Headers []Header
type Header struct {
ID string
Text string
Headers Headers
}
func (h Header) IsZero() bool {
return h.ID == "" && h.Text == ""
}
type Root struct {
Headers Headers
}
func (toc *Root) AddAt(h Header, y, x int) {
for i := len(toc.Headers); i <= y; i++ {
toc.Headers = append(toc.Headers, Header{})
}
if x == 0 {
toc.Headers[y] = h
return
}
header := &toc.Headers[y]
for i := 1; i < x; i++ {
if len(header.Headers) == 0 {
header.Headers = append(header.Headers, Header{})
}
header = &header.Headers[len(header.Headers)-1]
}
header.Headers = append(header.Headers, h)
}
func (toc Root) ToHTML(startLevel, stopLevel int) string {
b := &tocBuilder{
s: strings.Builder{},
h: toc.Headers,
startLevel: startLevel,
stopLevel: stopLevel,
}
b.Build()
return b.s.String()
}
type tocBuilder struct {
s strings.Builder
h Headers
startLevel int
stopLevel int
}
func (b *tocBuilder) Build() {
b.buildHeaders2(b.h)
}
func (b *tocBuilder) buildHeaders2(h Headers) {
b.s.WriteString("<nav id=\"TableOfContents\">")
b.buildHeaders(1, 0, b.h)
b.s.WriteString("</nav>")
}
func (b *tocBuilder) buildHeaders(level, indent int, h Headers) {
if level < b.startLevel {
for _, h := range h {
b.buildHeaders(level+1, indent, h.Headers)
}
return
}
if b.stopLevel != -1 && level > b.stopLevel {
return
}
hasChildren := len(h) > 0
if hasChildren {
b.s.WriteString("\n")
b.indent(indent + 1)
b.s.WriteString("<ul>\n")
}
for _, h := range h {
b.buildHeader(level+1, indent+2, h)
}
if hasChildren {
b.indent(indent + 1)
b.s.WriteString("</ul>")
b.s.WriteString("\n")
b.indent(indent)
}
}
func (b *tocBuilder) buildHeader(level, indent int, h Header) {
b.indent(indent)
b.s.WriteString("<li>")
if !h.IsZero() {
b.s.WriteString("<a href=\"#" + h.ID + "\">" + h.Text + "</a>")
}
b.buildHeaders(level, indent, h.Headers)
b.s.WriteString("</li>\n")
}
func (b *tocBuilder) indent(n int) {
for i := 0; i < n; i++ {
b.s.WriteString(" ")
}
}
var DefaultConfig = Config{
StartLevel: 2,
EndLevel: 3,
}
type Config struct {
// Heading start level to include in the table of contents, starting
// at h1 (inclusive).
StartLevel int
// Heading end level, inclusive, to include in the table of contents.
// Default is 3, a value of -1 will include everything.
EndLevel int
}

View File

@@ -0,0 +1,119 @@
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tableofcontents
import (
"testing"
qt "github.com/frankban/quicktest"
)
func TestToc(t *testing.T) {
c := qt.New(t)
toc := &Root{}
toc.AddAt(Header{Text: "Header 1", ID: "h1-1"}, 0, 0)
toc.AddAt(Header{Text: "1-H2-1", ID: "1-h2-1"}, 0, 1)
toc.AddAt(Header{Text: "1-H2-2", ID: "1-h2-2"}, 0, 1)
toc.AddAt(Header{Text: "1-H3-1", ID: "1-h2-2"}, 0, 2)
toc.AddAt(Header{Text: "Header 2", ID: "h1-2"}, 1, 0)
got := toc.ToHTML(1, -1)
c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ul>
<li><a href="#h1-1">Header 1</a>
<ul>
<li><a href="#1-h2-1">1-H2-1</a></li>
<li><a href="#1-h2-2">1-H2-2</a>
<ul>
<li><a href="#1-h2-2">1-H3-1</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#h1-2">Header 2</a></li>
</ul>
</nav>`, qt.Commentf(got))
got = toc.ToHTML(1, 1)
c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ul>
<li><a href="#h1-1">Header 1</a></li>
<li><a href="#h1-2">Header 2</a></li>
</ul>
</nav>`, qt.Commentf(got))
got = toc.ToHTML(1, 2)
c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ul>
<li><a href="#h1-1">Header 1</a>
<ul>
<li><a href="#1-h2-1">1-H2-1</a></li>
<li><a href="#1-h2-2">1-H2-2</a></li>
</ul>
</li>
<li><a href="#h1-2">Header 2</a></li>
</ul>
</nav>`, qt.Commentf(got))
got = toc.ToHTML(2, 2)
c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ul>
<li><a href="#1-h2-1">1-H2-1</a></li>
<li><a href="#1-h2-2">1-H2-2</a></li>
</ul>
</nav>`, qt.Commentf(got))
}
func TestTocMissingParent(t *testing.T) {
c := qt.New(t)
toc := &Root{}
toc.AddAt(Header{Text: "H2", ID: "h2"}, 0, 1)
toc.AddAt(Header{Text: "H3", ID: "h3"}, 1, 2)
toc.AddAt(Header{Text: "H3", ID: "h3"}, 1, 2)
got := toc.ToHTML(1, -1)
c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ul>
<li>
<ul>
<li><a href="#h2">H2</a></li>
</ul>
</li>
<li>
<ul>
<li>
<ul>
<li><a href="#h3">H3</a></li>
<li><a href="#h3">H3</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</nav>`, qt.Commentf(got))
got = toc.ToHTML(3, 3)
c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ul>
<li><a href="#h3">H3</a></li>
<li><a href="#h3">H3</a></li>
</ul>
</nav>`, qt.Commentf(got))
}