Include external code to posts

I want to include a source code saved externally from article file.

  • content/post/article.md
# this is title

this is source code

{{< highlight go "/code/sample.go" >}}
  • static/code/sample.go
func main() {
    fmt.Printf("hello, world!\n")
}

this hope to be equivalent of :

  • content/post/article.md
# this is title

this is source code

{{< highlight go >}}
func main() {
    fmt.Printf("hello, world!\n")
}
{{< /highlight >}}

I read this page, but all ways seem to require “copy and paste” on markdown file.

This feature is available on Octopress : http://octopress.org/docs/plugins/include-code/ (but generate commapnd takes so long time.)

Thank you.

Currently not possible.

But also discussed here:

Thank you!

I started a conversation about hybrid .hugo content files over here, if implemented, this could solve this problem.

Hello… this was not available in 2015, but how about 2018? :slight_smile: We are using Hugo for a site and are looking to use it in another one. Currently we have an application compile the content for us, but I am thinking it would be incredibly cool and valuable to simply use built-in functionality if it exists. Thank you for any assistance!

Have a look at readFile.


See also:

2 Likes

Awesome, thank you @kaushalmodi!

Also I just came across this post: https://it.knightnet.org.uk/kb/hugo/hugo-snippets/

In the middle there is a shortcode to do exactly what you want.

2 Likes

Even better! Thank you @gaetawoo!

Haha, I’d forgotten I’d put that in there! :blush:

2 Likes