My site has markdown files with shortcodes for embedding photos. I have two versions of the same photo
shortcode, one for HTML output, and one that inserts informative placeholders where the images should go for a custom plaintext output format. For my plaintext output, I want Hugo to expand those shortcodes, but leave the markdown as-is.
For example, if my markdown file contains:
## Forest Photos
Here is a *nice* photo:
{{< photo src="photo.jpg" alt="A photograph of a tree." caption="An interesting tree I found in the forest." >}}
My HTML output, via {{ .Content }}
in my HTML template, renders as intended:
<h2>
Forest Photos
</h2>
<p>
Here is a <em>nice</em> photo:
</p>
<figure class="photo">
<img src="photo.jpg" alt="A photograph of a tree." height="200" width="300" />
<figcaption>
An interesting tree I found in the forest.
</figcaption>
</figure>
My markdown source is already nicely formatted. For my plaintext output, my first instinct was to use {{ .RawContent }}
, but that leaves the shortcode as-is:
## Forest Photos
Here is a *nice* photo:
{{< photo src="photo.jpg" alt="A photograph of a tree." caption="An interesting tree I found in the forest." >}}
If I use {{ .Plain }}
, my shortcode works, but my markdown is flattened and stripped of semantic meaning:
Forest Photos
Here is a nice photo:
|--PHOTO---------------------------------------------->
A photograph of a tree.
Caption:
"An interesting tree I found in the forest."
URL:
<http://example.com/content/photo.jpg>
<--END-----------------------------------------------||
In an earlier thread, bep suggested using {{ .RawContent | .RenderString }}
to expand the shortcode, but that also converts the markdown to HTML, making it functionally equivalent to {{ .Content }}
.
The .RenderString
docs mention an optional markup
argument, used to specify a content format to convert to HTML. One of those content formats listed is HTML. I thought perhaps if I passed in my markdown and called it HTML, .RenderString
would leave it alone but expand the shortcodes as desired.
{{ $opt := dict "display" "block" "markup" "html" }}
{{ .RawContent | .RenderString $opt }}
This resulted in a build error:
Error: Error building site: failed to render pages: render of "page" failed: "/Users/tmcltr/Developer/myproject/layouts/_default/single.plaintext.txt:8:17": execute of template failed: template: _default/single.plaintext.txt:8:17: executing "_default/single.plaintext.txt" at <.RenderString>: error calling RenderString: "/Users/tmcltr/Developer/myproject/content/a-section/a-post/index.md:1:1": no content renderer found for markup "markdown"
The content file mentioned there doesn’t seem to be the problem. no content renderer found for markup "markdown"
appears to be what it says when it doesn’t recognize the specified content format.
I tried a few different variations. Instead of html
, I tried HTML
, htm
, HTM
, md
, and foo
, but the build error was unchanged. I tried markdown
and goldmark
, both of which allowed the build to complete without error. But of course my markdown was rendered into HTML.
I guess I have two questions:
- Is this a bug? Is HTML not a valid content format to send through
.RenderString
? - Is there no way to expand shortcodes without converting to HTML?