hugo/content/en/templates/render-hooks.md
Bjørn Erik Pedersen 41bc6f702a Squashed 'docs/' changes from 2201ac0e5..2c0125b52
2c0125b52 Remove .Site.Author
2cf8841b3 Update partialCached.md (#1924)
385487191 Update data-templates.md (#1926)
ce207e141 Remove redundant markdown and fix a few typos (#1936)
3687c2953 Make heading id linkable, take 2
45c79bea7 Make heading id linkable
b22079344 Delete duplicates the lines 557-569 and 570-582. (#1934)
0a90dc122 Rework the taxonomy variables page (#1935)
7f8979c50 Update theme
26e682a3a Update multilingual.md
d40e7693f Update postcss.md
375d75c01 Update postcss npm instructions (#1931)
63020094a Emphasize Window shell selection (#1930)
56824be2c Update configuration.md
b7b8f16b3 Docu 'Theme components': minor fix (#1929)
09dc81a05 Remove Docker from BSD page (#1927)
205fea204 netlify: Hugo 0.108.0
6abe49c28 Merge commit 'da670c38ee63a7fef25e2b9f42519232055b60dc'
12b59a4c5 docs: Add basic doc for wrapStandAloneImageWithinParagraph etc.
ba07bd970 dartsass: Add sourceMapIncludeSources option

git-subtree-dir: docs
git-subtree-split: 2c0125b5290494d49334606c451446ebd9df3c21
2022-12-20 11:04:41 +01:00

5.5 KiB

title linkTitle description date categories keywords toc menu
Markdown Render Hooks Render Hooks Render Hooks allow custom templates to override markdown rendering functionality. 2017-03-11
templates
markdown
true
docs
title parent weight
Markdown Render Hooks templates 20

Note that this is only supported with the Goldmark renderer.

You can override certain parts of the default Markdown rendering to HTML by creating templates with base names render-{kind} in layouts/_default/_markup.

You can also create type/section specific hooks in layouts/[type/section]/_markup, e.g.: layouts/blog/_markup.

The hook kinds currently supported are:

  • image
  • link
  • heading
  • codeblock{{< new-in "0.93.0" >}}

You can define Output-Format- and language-specific templates if needed. Your layouts folder may look like this:

layouts/
└── _default/
    └── _markup/
        ├── render-codeblock-bash.html
        ├── render-codeblock.html
        ├── render-heading.html
        ├── render-image.html
        ├── render-image.rss.xml
        └── render-link.html

Some use cases for the above:

  • Resolve link references using .GetPage. This would make links portable as you could translate ./my-post.md (and similar constructs that would work on GitHub) into /blog/2019/01/01/my-post/ etc.
  • Add target=_blank to external links.
  • Resolve and process images.
  • Add header links.

The render-link and render-image templates will receive this context:

Page
The Page being rendered.
Destination
The URL.
Title
The title attribute.
Text
The rendered (HTML) link text.
PlainText
The plain variant of the above.

The render-heading template will receive this context:

Page
The Page being rendered.
Level
The header level (1--6)
Anchor
An auto-generated html id unique to the header within the page
Text
The rendered (HTML) text.
PlainText
The plain variant of the above.
Attributes (map)
A map of attributes (e.g. id, class). Note that this will currently always be empty for links.

The render-image templates will also receive:

IsBlock {{< new-in "0.108.0" >}}
Returns true if this is a standalone image and the config option markup.goldmark.parser.wrapStandAloneImageWithinParagraph is disabled.
Ordinal {{< new-in "0.108.0" >}}
Zero-based ordinal for all the images in the current document.
[Text](https://www.gohugo.io "Title")

Here is a code example for how the render-link.html template could look:

{{< code file="layouts/_default/_markup/render-link.html" >}} <a href="{{ .Destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}{{ if strings.HasPrefix .Destination "http" }} target="_blank" rel="noopener"{{ end }}>{{ .Text | safeHTML }} {{< /code >}}

Image Markdown example

![Text](https://gohugo.io/images/hugo-logo-wide.svg "Title")

Here is a code example for how the render-image.html template could look:

{{< code file="layouts/_default/_markup/render-image.html" >}}

{{ .Text }}

{{< /code >}}

Given this template file

{{< code file="layouts/_default/_markup/render-heading.html" >}} <h{{ .Level }} id="{{ .Anchor | safeURL }}">{{ .Text | safeHTML }} ¶</h{{ .Level }}> {{< /code >}}

And this markdown

### Section A

The rendered html will be

<h3 id="section-a">Section A <a href="#section-a"></a></h3>

Render Hooks for Code Blocks

{{< new-in "0.93.0" >}}

You can add a hook template for either all code blocks or for a specific type/language (bash in the example below):

layouts
└── _default
    └── _markup
        └── render-codeblock.html
        └── render-codeblock-bash.html

The default behavior for these code blocks is to do Code Highlighting, but since you can pass attributes to these code blocks, they can be used for almost anything. One example would be the built-in GoAT Diagrams or this Mermaid Diagram Code Block Hook example.

The context (the ".") you receive in a code block template contains:

Type (string)
The type of code block. This will be the programming language, e.g. bash, when doing code highlighting.
Attributes (map)
Attributes passed in from Markdown (e.g. { attrName1=attrValue1 attrName2="attr Value 2" }).
Options (map)
Chroma highlighting processing options. This will only be filled if Type is a known Chroma Lexer.
Inner (string)
The text between the code fences.
Ordinal (integer)
Zero-based ordinal for all code blocks in the current document.
Page
The owning Page.
Position
Useful in error logging as it prints the filename and position (linenumber, column), e.g. {{ errorf "error in code block: %s" .Position }}.