Using distinct shortcodes for different output formats

Hi! I’m new to Hugo and one of the big plus for me has been the ability to specify custom output formats. Specifically I want to generate Web pages but also Gemtext content.

I defined my custom format as follows:

[mediaTypes."text/gemini"]
  suffixes = ["gmi"]

[outputFormats.GEMTEXT]
  name = "GEMTEXT"
  isPlainText = true
  isHTML = false
  mediaType = "text/gemini"
  protocol = "gemini://"
  permalinkable = true
  path = "gemtext/"

[outputs]
  home = [ "HTML", "GEMTEXT" ]
  page = [ "HTML", "GEMTEXT" ]

I also have defined a very simple template for both the home page (layouts/index.gmi) and single pages (layouts/_default/single.gmi). Running Hugo generates my Gemtext files fine, in the gemtext subfolder, with the .gmi extension, but shortcodes such as rel or relref are not processed, whereas in the generated HTML files they are correctly converted into links.

I’ve tried to write my own shortcode in layouts/shortcodes/, using a bunch of different extensions (relref.gemtext, relref.gemtext.text, relref.gemtext.html, relref.gmi, relref.gmi.text, with gemtext uppercased, etc) to no avail.

I’ve also tried to write a new shortcode with a unique name: it is easily rendered in the HTML pages but is left untouched in the Gemtext pages:

  • in content/test.md: {{< heyya >}}
  • in layouts/shortcodes/heyya.<everything-i-could-think-of>: {{- print "HEYYA" -}}
  • in public/gemtext/test.gmi: {{< heyya >}}
  • in public/test.html: <!-- html trimmed--> HEYYA

What am I missing? Let me know if you need additional snippets.

Thank you for your time!

Does this template render .RawContent?

I would go the way with Markdown Render Hooks

try to create render-link.gmi as template, don’t forget to bring the gemtext links to a separate line

my HTML samples are here

PS. use the latest HUGO version

Indeed! Here it is:

{{ if .Title }}# {{ println .Title }}{{ end }}{{ .RawContent }}

It simply writes # title if there is title defined in the frontmatter, followed by the raw content.

My issue is with shortcodes at the moment, but this will be interesting to use later, thank you for the link.

So, .RawContent is… raw. Meaning unprocessed, as-written, etc.

You could try using this instead:

{{ .Content | plainify }}

The shortcodes will be processed, but it may affect formatting a bit.

I’ve tried to play with .Content but discarded it because even though it correctly processes the shortcodes, it produces HTML content which is not the desired output. I just tried plainify but it removes all links and other Markdown semantics along the way :sweat_smile:

I expect the Markdown to not be rendered into HTML and kept mostly as-is, but with the shortcodes processed through definitions specific to that custom output format. Maybe it is a misleaded goal?

Yeah, that one.

For rendering links, @ju52 has it right.

If you want to remove the {{< foo >}} 's from your gmi output, you could process .RawContent through replaceRE.

Alright then I will try to use replaceRE or an external tool. Thank you for clearing things up :slight_smile:

I have not read this entire thread, but you can create output format specific versions of your shortcode, e.g. myshortcode.json.

This is what I intended to do, but after following this thread my understanding is that the processing of shortcodes happens after the Markdown content has been initially processed (into HTML, JSON, etc) and I could not find a way to make Hugo use output-format-specific shortcodes without processing the Markdown.

Briefly:

  • .Content includes processed shortcodes but HTML is generated, which is not desired.
  • .RawContent is the Markdown as-is but skips shortcode processing.

What I wanted was only applying custom shortcodes (e.g. changing image or figure shortcodes into Markdown links to the images) but without rendering the Markdown into HTML or anything else, leaving the Markdown to Gemtext to an external, more appropriate tool. Even better would have been to render the Markdown into Gemtext as part of the Hugo execution, but I feel like this would require modifying Hugo itself. I may be wrong, again I’m discovering Hugo :slight_smile:

When you call .Content the markdown is rendered to HTML. But if you have multiple versions of a given shortcode (language, output format), Hugo renders multiple versions of that same content.

I just tried it and indeed, with .Content I get the shortcodes “heyya”, defined differently both in heyya.html and heyya.gmi, to be used for the correct output formats in the resulting HTML and Gemtext pages.

This does not solve my issue (I do not want the Markdown to be rendered as HTML in my Gemtext pages) but at least I know how to make custom output format shortcodes work now.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.