Display external source code with highlight

Dear Hugo pros,

I would like to embed code with proper highlighting from an external source, means:

  • read external file determined by an URL, i.e. readFile $url
  • use the capabilities of highlight function
  • even be able to only render a part of the external file by specifying a lineStart and lineEnd
  • directly embed it via shortcode to my regular hugo markdown content

basically something like:

{{< highlight go "url=https://foo.bar/baz.go,lineStart=3,lineEnd=20,hl_lines=8 15-17" >}}
{{< /highlight >}}

I am not yet familiar with the power of Hugo, so I didn’t find out how I could realize this.
Is this possible these days and if yes, how? :slight_smile:

Thank you and kind regards


General approach:

  1. Use resources.GetRemote to capture the file
  2. Split the file into an array of lines
  3. Iterate through the lines, beginning at the “start” line and ending at the “stop” line
  4. Pass the result through the highlight function

Ah yes… I missed the resources.GetRemote… I will go this road.


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