Include content of a URL

Hi,

I am quite new to hugo.

I would like to load content of a markdown file that is available on the internet.
I found out here I have to write my own shortcode. I tried it the way they mentioned. But when I use it with a URL it always says

execute of template failed: template: shortcodes/content.html:2:11: executing "shortcodes/content.html" at <readFile>: error calling readFile: file "<myUrl>" does not exist. 

But I am sure it is free to access on the internet.

What can I do?

Mmmmm … I suspect that somewhere you didn’t replace <myUrl> with your real url …

To receive assistance you’ll need to share your shortcode. I don’t know of a method used to get the contents of a remote markdown file and render it via shortcode at build time. There’s normally a step involving getting the remote file and moving it to the local filesystem, where functions like readFile work.

See https://gohugo.io/templates/files/ for more details. The “remote” calls work for JSON and CSV, where Hugo will retrieve and cache the files for processing, described at https://gohugo.io/templates/data-templates/#data-driven-content.

sorry, I guess I wasn’t clear enough, I did not add the URL there as I thought this was overfilling the error message, it does show my actual URL at that placeholder

Hi,

My shortcode is this:

{{$file := .Get 0}}
{{ $file | readFile | markdownify }}

I then use it like this:

{{% content "https://raw.githubusercontent.com/europace/kex-vorgang-export-api/master/README.md" %}}

but I get the error:

failed to render shortcode "content": failed to process shortcode: "...../layouts/shortcodes/content.html:2:11": execute of template failed: template: shortcodes/content.html:2:11: executing "shortcodes/content.html" at <readFile>: error calling readFile: file "https:/raw.githubusercontent.com/europace/kex-vorgang-export-api/master/README.md" does not exist

I did assume that readFile does not take a URL with it, but I could not find another way to make this work and I assumed that somehow this must be possible… :frowning:

@maiki do you have a suggestion for the shortcode that would allow this? If I do getJSON I am not sure what will happen, as this file is not json but markdown… :thinking:

so when I do the following in my shortcode:

{{ getJSON "https://raw.githubusercontent.com/europace/kex-vorgang-export-api/master/README.md" | markdownify}}

I get the error:

Building sites … ERROR 2020/08/05 08:31:18 Failed to get JSON resource "https://raw.githubusercontent.com/europace/kex-vorgang-export-api/master/README.md": invalid character '#' looking for beginning of value

So this seems just to work for actual json :frowning:

I have a quickfix for my problem now. It feels very hacky, but I did not find another way.

I now have a shortcode looking like this:

{{ $repo := .Get 0 }}
{{ $json := getJSON "https://api.github.com/repos/europace/" $repo "/readme" }}
{{ $json.content | base64Decode | markdownify }}

Then I call that in my md-file like this:

{{% europace-content "kex-vorgang-import-api" %}}

I am not happy with how it looks, but I am happy that I found something. I will keep this open for a few days as maybe somene has a better solution. Then I will close it :slight_smile:

As you’re discovered, we don’t have a generic “get file via HTTP” template function. readFile is for the local OS. Our only “remote get” functions as of today are getJSON and getCSV.

1 Like

Your feelings are valid, of course. I wanted to point out:

  • markdown is for humans to write readable text that can be rendered in different ways and be easy to remember
  • JSON is a data format made for the web

Getting the contents of a remote markdown file via JSON call, I wouldn’t call that hacky, I’d call it, “used as intended”. :slight_smile:

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