Including markdown (or mmark) inside an html content file

Hello everyone,
I know this has come up before, because I just spent a few hours reading a bunch of past discussions and github issues, and trying every thing i could find, with no luck.
My problem is I have a content file in html and I need to include another “piece of content” - note I don’t mean to imply that the data to include lives under content/ I don’t actually care much about where the data goes, I also tried putting it in layouts/partials/ and using ‘partial’ template functions to load it - and this piece of content is markdown formatted.

so basically i just need to include markdown data in a piece of html content. I’ve tried to give the html content an .md extension (since afaik md can contain any valid html as well), but then i got errors
like template: __spec.html:6: unexpected "&" in operand even though there’s not even an ‘&’, that line just contains
{{ partial "meta.html" . }}

when i changed the extention of the file i want to include with partial to mmark (because ideally i would use mmark) I get Error(s) rendering pages: html/template: cannot redefine "__resources.html" after it has executed

and i saw references to shortcodes and how you can surround them with % % to process as markdown but then I still need to know what shortcode to use, and there doesn’t seem to be a shortcode to “load text from a file”

and i saw you can do " | markdownify" in templates but that only works on strings, not on the hugo.Page datastructure

I know I’m not being very specific with what I tried, how the files look like , and what the errors are, but it would take me an hour to go through all the combinations of everything, I’m hoping somebody can just tell me “Dieter you’re an idiot you just have to do foobar” and get the right answer in a minute :slightly_smiling:

so, thanks in advance for the help. I’ll gladly provide more details if needed.
But basically I’m maintaing a technical specification file at that I want to “embed” into a webpage (see for live example). it currently works but only because the spec is in html which is quite messy.

I haven’t read you entire post, but what you probably want is shortcodes + markdownify. I see now that it only supports strings directly (that is a mistake), but there are workarounds to get strings from anything (see the print func), so inside a shortcode:

{{ .Page.Content | print | markdownify }}

Should work.

Looking at your two links, I’m still a little confused as to what you’re trying to do, but here is what I was able to glean from it:

  • You have a markdown file with some content in it
  • You want to have that chunk of markdown converted into HTML and added to several pages, but you want to be able to change it once (ie, within the markdown) and have it change everywhere

If this is correct - and this is piggybacking on what Bjorn already mentioned - do the following, and I’m using the sample links you included in your original post:

  1. Take your markdown file. I assume it’s called Copy that to layouts/partials/spec.html (note: you can keep it in markdown, but you’ll need to change the extension to .html)
  2. In your template for a single page (for the sake of this thread, let’s say it’s the default single page layout living at layouts/_default/single.html), add {{partial "spec.html" | print | markdownify}} wherever you want the spec to fall in the source and check your browser.
  3. Looking at the spec.html you linked to, I don’t know why you’re interested in including this in multiple pages (not that I’m saying you shouldn’t), so you may want to get creative with the front matter you include in individual markdown files (ie, pages) and then include something to the effect of include_spec: true in the front matter of your file and add a conditional to the templating: (eg, {{if .Params.include_spec }}{{partial "spec.html" | print | markdownify }}{{end}} I mention in #2.

Does this answer your question?

Thanks to both of you. that trick works great.
@rdwatters i’m not trying to include it in multiple pages. only in 1 place. to give some additional clarification about why not just include the markown as content directly, it’s because it’s a technical specification that is maintained in a separate repository, the website is a different project that also happens to have a page where it renders the spec.

an additional complication is also since i can’t just symlink (since go and by extension hugo have poor symlink support) it means i have an extra level in the directory hierarchy (caused by having to include the spec repository instead of the file or a symlink to the file), but this is not an issue with partials and seems to work fine.

Cool. I’m glad it’s working for you. I can think of plenty of workarounds in terms of adding an updated version to your repository every time you build the site (eg, using wget) via some basic bash scripting. I get the feeling, however, that you wouldn’t need my help on that. Are you using Wercker or S3 or GH pages to build/host the site?

You can also do this using mmark

Save the main content file with .mmark extension and then inside the .mmark file you can include text from anywhere - the contents of the other file will also be treated as markdown - mmark will include text inside {{}} e.g.


For this to work you need to rebuild public with Hugo when the text changes - watch will not dynamically update if the included text changes - it is only rendered when you build with Hugo

@rdwatters: no, linode vps + lighttpd. currently when the spec updates, i just manually update the submodule, run hugo (on my laptop) and rsync the data to the vps. i could automate it sure but doesn’t seem worth the hassle just yet.