| 1 | # Hugo Goldmark Extensions |
| 2 | |
| 3 | [](https://github.com/gohugoio/hugo-goldmark-extensions/actions?query=workflow:Test) |
| 4 | |
| 5 | This repository houses a collection of [Goldmark] extensions created by the [Hugo] community, focusing on expanding Hugo's markdown functionality. |
| 6 | |
| 7 | [CommonMark]: https://spec.commonmark.org/0.30/ |
| 8 | [Goldmark]: https://github.com/yuin/goldmark/ |
| 9 | [Hugo]: https://gohugo.io/ |
| 10 | [LaTeX]: https://www.latex-project.org/about/ |
| 11 | [KaTeX]: https://katex.org/ |
| 12 | [MathJax]: https://www.mathjax.org/ |
| 13 | |
| 14 | ## Passthrough extension |
| 15 | |
| 16 | [](https://godoc.org/github.com/gohugoio/hugo-goldmark-extensions/passthrough) |
| 17 | |
| 18 | Use this extension to preserve raw Markdown within delimited snippets of text. This was initially developed to support [LaTeX] mixed with Markdown, specifically mathematical expressions and equations. |
| 19 | |
| 20 | For example, to preserve raw Markdown for inline snippets delimited by the `$` character: |
| 21 | |
| 22 | Markdown|Default rendering|Passthrough rendering |
| 23 | :--|:--|:-- |
| 24 | `a $_text_$ snippet`|`a $<em>text</em>$ snippet`|`a $_text_$ snippet` |
| 25 | |
| 26 | In the Markdown example above, the underscores surrounding the word "text" signify emphasis. The Markdown renderer wraps the word within `em` tags as required by the [CommonMark] specification. In comparison, the passthrough extension preserves the text within and including the delimiters. |
| 27 | |
| 28 | Why is this important? Consider this example of a mathematical equation written in LaTeX: |
| 29 | |
| 30 | Markdown|Default rendering|Passthrough rendering |
| 31 | :--|:--|:-- |
| 32 | `$a^*=x-b^*$`|`$a^<em>=x-b^</em>$`|`$a^*=x-b^*$` |
| 33 | |
| 34 | Without this extension, LaTeX parsers such as [KaTeX] and [MathJax] will render this: |
| 35 | |
| 36 | \$a^<em>=x-b^</em>\$ |
| 37 | |
| 38 | Instead of this: |
| 39 | |
| 40 | $a^\*=x-b^\*$ |
| 41 | |
| 42 | ### Delimiters |
| 43 | |
| 44 | There are two types of delimiters: |
| 45 | |
| 46 | - Text within and including _inline_ delimiters is rendered inline with the surrounding text. |
| 47 | - Text within and including _block_ delimiters is rendered between adjacent block elements. |
| 48 | |
| 49 | As shown below, delimiters are defined in pairs of opening and closing characters. |
| 50 | |
| 51 | ### Usage |
| 52 | |
| 53 | ```go |
| 54 | package main |
| 55 | |
| 56 | import ( |
| 57 | "bytes" |
| 58 | "fmt" |
| 59 | |
| 60 | "github.com/gohugoio/hugo-goldmark-extensions/passthrough" |
| 61 | "github.com/yuin/goldmark" |
| 62 | ) |
| 63 | |
| 64 | func main() { |
| 65 | md := goldmark.New( |
| 66 | goldmark.WithExtensions( |
| 67 | passthrough.New( |
| 68 | passthrough.Config{ |
| 69 | InlineDelimiters: []passthrough.Delimiters{ |
| 70 | { |
| 71 | Open: "$", |
| 72 | Close: "$", |
| 73 | }, |
| 74 | { |
| 75 | Open: "\\(", |
| 76 | Close: "\\)", |
| 77 | }, |
| 78 | }, |
| 79 | BlockDelimiters: []passthrough.Delimiters{ |
| 80 | { |
| 81 | Open: "$$", |
| 82 | Close: "$$", |
| 83 | }, |
| 84 | { |
| 85 | Open: "\\[", |
| 86 | Close: "\\]", |
| 87 | }, |
| 88 | }, |
| 89 | }, |
| 90 | )), |
| 91 | ) |
| 92 | |
| 93 | input := ` |
| 94 | block $$a^*=x-b^*$$ snippet |
| 95 | |
| 96 | inline $a^*=x-b^*$ snippet |
| 97 | ` |
| 98 | |
| 99 | var buf bytes.Buffer |
| 100 | if err := md.Convert([]byte(input), &buf); err != nil { |
| 101 | panic(err) |
| 102 | } |
| 103 | |
| 104 | fmt.Println(buf.String()) |
| 105 | } |
| 106 | ``` |
| 107 | |
| 108 | ## Extras extension |
| 109 | |
| 110 | [](https://godoc.org/github.com/gohugoio/hugo-goldmark-extensions/extras) |
| 111 | |
| 112 | Use this extension to include [deleted text], [inserted text], [mark text], [subscript], and [superscript] elements in Markdown. |
| 113 | |
| 114 | Element|Markdown|Rendered |
| 115 | :--|:--|:-- |
| 116 | Deleted text|`~~foo~~`|`<del>foo</del>` |
| 117 | Inserted text|`++foo++`|`<ins>foo</ins>` |
| 118 | Mark text|`==bar==`|`<mark>bar</mark>` |
| 119 | Subscript|`H~2~O`|`H<sub>2</sub>O` |
| 120 | Superscript|`1^st^`|`1<sup>st</sup>` |
| 121 | |
| 122 | [deleted text]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/del |
| 123 | [inserted text]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ins |
| 124 | [mark text]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/mark |
| 125 | [subscript]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/sub |
| 126 | [superscript]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/sup |
| 127 | |
| 128 | ### Deleted text |
| 129 | |
| 130 | With Goldmark v1.7.1 and earlier, the Goldmark "strikethrough" extension was triggered by wrapping text within a pair of double-tilde characters. With Goldmark v1.7.2 and later, to provide full GFM compatibility, the Goldmark "strikethrough" extension is triggered by wrapping text within a pair of single- or double-tilde characters. |
| 131 | |
| 132 | This change conflicts with the Hugo Goldmark Extras "subscript" extension. |
| 133 | |
| 134 | When enabling the Hugo Goldmark Extras "subscript" extension, if you want to render subscript and strikethrough text concurrently, you must: |
| 135 | |
| 136 | 1. Disable the Goldmark "strikethrough" extension |
| 137 | 2. Enable the Hugo Goldmark Extras "delete" extension |
| 138 | |
| 139 | ### Usage |
| 140 | |
| 141 | ```go |
| 142 | package main |
| 143 | |
| 144 | import ( |
| 145 | "bytes" |
| 146 | "fmt" |
| 147 | |
| 148 | "github.com/gohugoio/hugo-goldmark-extensions/extras" |
| 149 | "github.com/yuin/goldmark" |
| 150 | ) |
| 151 | |
| 152 | func main() { |
| 153 | md := goldmark.New( |
| 154 | goldmark.WithExtensions(extras.New( |
| 155 | extras.Config{ |
| 156 | Delete: extras.DeleteConfig{Enable: true}, |
| 157 | Insert: extras.InsertConfig{Enable: true}, |
| 158 | Mark: extras.MarkConfig{Enable: true}, |
| 159 | Subscript: extras.SubscriptConfig{Enable: true}, |
| 160 | Superscript: extras.SuperscriptConfig{Enable: true}, |
| 161 | }, |
| 162 | )), |
| 163 | ) |
| 164 | |
| 165 | input := ` |
| 166 | Hydrogen (H) is the 1^st^ element in the periodic table. |
| 167 | |
| 168 | Water (H~2~O) is a liquid. |
| 169 | |
| 170 | Water (H~2~O) is a ~~liquid~~ ++compound++. |
| 171 | |
| 172 | Water (H~2~O) is an ==organic compound==. |
| 173 | ` |
| 174 | |
| 175 | var buf bytes.Buffer |
| 176 | if err := md.Convert([]byte(input), &buf); err != nil { |
| 177 | panic(err) |
| 178 | } |
| 179 | |
| 180 | fmt.Println(buf.String()) |
| 181 | } |
| 182 | ``` |