markup/goldmark: Support extras extension

Enables inclusion of these HTML elements in Markdown:

- Inserted Text (++inserted++)
- Mark Text (==marked==)
- Subscript (H~2~O)
- Superscript (1^st^)
This commit is contained in:
Joe Mooring
2024-05-03 18:06:10 -07:00
committed by Bjørn Erik Pedersen
parent b1bf0bff2c
commit ca9a77ef92
6 changed files with 113 additions and 1 deletions

View File

@@ -17,6 +17,7 @@ package goldmark
import (
"bytes"
"github.com/gohugoio/hugo-goldmark-extensions/extras"
"github.com/gohugoio/hugo-goldmark-extensions/passthrough"
"github.com/gohugoio/hugo/markup/goldmark/hugocontext"
"github.com/yuin/goldmark/util"
@@ -113,6 +114,15 @@ func newMarkdown(pcfg converter.ProviderConfig) goldmark.Markdown {
extensions = append(extensions, images.New(cfg.Parser.WrapStandAloneImageWithinParagraph))
extensions = append(extensions, extras.New(
extras.Config{
Insert: extras.InsertConfig{Enable: cfg.Extensions.Extras.Insert.Enable},
Mark: extras.MarkConfig{Enable: cfg.Extensions.Extras.Mark.Enable},
Subscript: extras.SubscriptConfig{Enable: cfg.Extensions.Extras.Subscript.Enable},
Superscript: extras.SuperscriptConfig{Enable: cfg.Extensions.Extras.Superscript.Enable},
},
))
if mcfg.Highlight.CodeFences {
extensions = append(extensions, codeblocks.New())
}

View File

@@ -49,6 +49,20 @@ var Default = Config{
EastAsianLineBreaksStyle: "simple",
EscapedSpace: false,
},
Extras: Extras{
Superscript: Superscript{
Enable: false,
},
Subscript: Subscript{
Enable: false,
},
Insert: Insert{
Enable: false,
},
Mark: Mark{
Enable: false,
},
},
Passthrough: Passthrough{
Enable: false,
Delimiters: DelimitersConfig{
@@ -112,6 +126,7 @@ type Extensions struct {
Typographer Typographer
Footnote bool
DefinitionList bool
Extras Extras
Passthrough Passthrough
// GitHub flavored markdown
@@ -150,7 +165,32 @@ type Typographer struct {
Apostrophe string
}
// Passthrough hold passthrough configuration.
// Extras holds extras configuration.
// github.com/hugoio/hugo-goldmark-extensions/extras
type Extras struct {
Insert Insert
Mark Mark
Subscript Subscript
Superscript Superscript
}
type Insert struct {
Enable bool
}
type Mark struct {
Enable bool
}
type Subscript struct {
Enable bool
}
type Superscript struct {
Enable bool
}
// Passthrough holds passthrough configuration.
// github.com/hugoio/hugo-goldmark-extensions/passthrough
type Passthrough struct {
// Whether to enable the extension

View File

@@ -744,3 +744,53 @@ a^*=x-b^*
%!%
`)
}
func TestExtrasExtension(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
[markup.goldmark.extensions.extras.insert]
enable = false
[markup.goldmark.extensions.extras.mark]
enable = false
[markup.goldmark.extensions.extras.subscript]
enable = false
[markup.goldmark.extensions.extras.superscript]
enable = false
-- layouts/index.html --
{{ .Content }}
-- content/_index.md --
---
title: home
---
++insert++
==mark==
H~2~0
1^st^
`
b := hugolib.Test(t, files)
b.AssertFileContent("public/index.html",
"<p>++insert++</p>",
"<p>==mark==</p>",
"<p>H~2~0</p>",
"<p>1^st^</p>",
)
files = strings.ReplaceAll(files, "enable = false", "enable = true")
b = hugolib.Test(t, files)
b.AssertFileContent("public/index.html",
"<p><ins>insert</ins></p>",
"<p><mark>mark</mark></p>",
"<p>H<sub>2</sub>0</p>",
"<p>1<sup>st</sup></p>",
)
}