Trying to fetch markdown from a remote repository using resources.GetRemote

I am trying to fetch markdown from a remote repository during the Hugo build and have that markdown render in a page. I have managed to do this with a shortcode that uses the resources.GetRemote function, however my right hand side bar TOC does not include the headings from the markdown that is pulled in at build time, which is a problem. I don’t know if I am doing something wrong in the shortcode, or whether it is an issue in the theme I’m using (Docsy).

Hugo 0.119.0
Docsy 0.8.0

I was inspired by this post: Is there a way to embed raw github url in hugo? - #7 by jmooring

My shortcode is as follows and can be found here:

{{ $url := $.Page.Params.rnurl }
{{ with resources.GetRemote $url }}
  {{ with .Err }}
    {{ errorf "%s" . }}
  {{ else }}
    {{ .Content | $.Page.RenderString }}
  {{ end }}
{{ else }}
  {{ errorf "Unable to get remote resource." }}
{{ end }}

The markdown page where I want to pull in the remote markdown contains this and can be found here:

---
title: "fetch-remote"
linkTitle: "fetch-remote"
no_list: true
rnurl: "https://raw.githubusercontent.com/mstura/doc_test/master/markdown/com.castsoftware.dotnet/1.5.md"
---

{{< fetch-remote >}}

The result can be seen here where the right side bar does not contain the headings from the imported markdown.

Maybe what I am trying to achieve is just not possible? Does anyone have any clues?
thank you for any pearls of wisdom… or just to tell me I can’t do what I’m trying to do…!

Note 1:
I have achieved what I want using RenderShortcodes (RenderShortcodes | Hugo) but this requires that I import the markdown files from the remote repo into the repo I’m building from using module.imports - BUT i’m trying to avoid doing this because it will inflate the number of files that are output at the end of the build (I’m using Cloudflare and there is a hard limit on the number of files that you can host per “site” (20000)). You can see this result here.

Note 2:
I’ve experimented also with getJSON by pulling the markdown in JSON format with the github API, but I have similar issues and also getJSON is now deprecated.

{{ $url := $.Page.Params.rnurl }}
{{ with resources.GetRemote $url }}
  {{ with .Err }}
    {{ errorf "%s" . }}
  {{ else }}
{{ .Content }}
  {{ end }}
{{ else }}
  {{ errorf "Unable to get remote resource." }}
{{ end }}

And call it with using the {{% %}} notation.

Note that the {{ .Content }} line must not be indented.

1 Like

Genius! Thank you so much, that is EXACTLY what I was trying to achieve… If I could send you a beer or something to say thank you, I would!

Do you have a moment to explain what I was doing wrong - I presume that the $.Page.RenderString was doing something that it shouldn’t?

And the indentation? I hadn’t realised that was important, so thanks for that.

Regarding the call notation…

https://gohugo.io/troubleshooting/faq/#why-are-there-two-ways-to-call-a-shortcode

And now that you inserting raw markdown, anything indented by four or more spaces (or a tab) is rendered as an indented code block.

1 Like

Thank you.

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