Highlight shortcode and html partials in content

Hugo noob, so sorry if this has been covered elsewhere. I’ve been searching the forums hoping to find a similar post, but my search fu is obviously failing.

So I’m understanding that content in markdown is separate from layout and can’t include partials for example, but has an escape hatch for doing dynamic stuff like the highlight shortcode which invokes pygment. Great so far.

My specific application is I’m trying to write documentation that specifically has HTML examples which must be rendered as HTML and also render the raw HTML using highlight. I can copy the markup twice and it does work, but this sort of defeats the dryness. Is there a way to create a short code that does both but loads it’s content from a single file?

e.g.

{{<example-html-and-highlight html "content="examples/example1.html"></example-html-and-highlight>}}

Thanks for any pointers in advance.

Yan

OK, after hacking around a bit I was able to fake something that works

{{ $partial := (partial (.Get 0) .) }}
{{ $partial }}
{{ highlight $partial (.Get 1) (.Get 2) }}

Needs some parameter len checking but it appears to work. Thanks

One issue with this is that my content now lives under layout. Is there a good approach to including content as files from content from a partial?

Currently I don’t think so. I think what you want is a Unix-cat like template function. This has been discussed in endless ways in the discussion about allowing execution of shell programs from Hugo templates. I think we should just add a cross-platform cat implementation before we land that discussion. I need it. Many people need it.

1 Like

On reflection, I think this works better since it’s more like using highlight

{{ .Inner }}
{{ highlight .Inner (.Get 0) (.Get 1) }}

For short snippets the markup can be inline, so it’s probably a win.