问题
Recently the re-development of a web site was given to me. The re-worked site is to be done in Markdown and run through the Hugo static site generator.
Is there a way to include other files in a Markdown web page processed through Hugo? If so, how? Unless I've missed something, this isn't addressed in the Hugo docs.
With HTML and some servers (Apache, at least) you can do something like:
<html>
<body>
Some content
<!--#include virtual="name_of_first_file_to_include" -->
More content
<!--#include virtual="name_of_second_file_to_include" -->
Still more content
</body>
<html>
I've tried creating a template page which puts stuff like "Some content" and "More content" into the template and then the included stuff in my .md file which gets "included" via {{ .Content }} in the template. However, 1) That seems like the wrong way to use a template. 2) I've not figured out a way to bring in more files if I need them.
回答1:
For content files there are two options:
- Shortcodes. Powerful and documented.
- Use
mmark
as the markdown rendering engine with itsinclude
feature. Rename the content files to "*.mmark". See https://github.com/miekg/mmark
I am the maintainer of Hugo.
回答2:
I have a custom shortcode for rendering my static demo files as code via markdown, it's simple:
layouts/shortcodes/code.html
{{ $file := .Get "file" | readFile }}
{{ $lang := .Get "language" }}
{{ (print "```" $lang "\n" $file "\n```") | markdownify }}
content/post/some-post.md
{{% code file="/static/some-script.js" language="js" %}}
回答3:
A colleague suggested creating a shortcode to help with this. While this isn't quite what I had in mind - it is a more complicated than I would have liked - it's not too bad and I've not found a better way. Thus I've implemented a solution using a shortcode and a CSV file. Simple example files are below:
The content file is still (mostly) Markdown and looks something like:
+++
date = "2016-09-29"
title = "short_code_test"
type = "pages"
+++
## Short Code test
Test table should appear below:
{{< display_table_csv "static/test_data.csv" >}}
<tr><th>Name</th><th>Birthday</th>
{{< /test_table_shortcode >}}
(Note that the type = "pages"
just pulls in a template which modifies/overrides the hugo-uno default pages/single.html template to make the output cleaner for purposes of display below.)
layouts/shortcodes/display_table_csv.html:
<table>
<thead>
{{ .Inner }}
</thead>
<tbody>
{{ $url := (index .Params 0) }}
{{ $sep := "," }}
{{ range $row_i, $row := getCSV $sep $url }}
<tr>
{{ range $col_i, $col := $row }}
<td>{{ $col }}</td>
{{ end }}
</tr>
{{ end }}
</tbody>
</table>
static/test_data.csv:
John, 1940-10-09
Paul, 1942-06-18
George, 1943-02-25
Ringo, 1940-07-07
This image shows how things rendered:
The Data-driven Content page in the Hugo docs was helpful also.
来源:https://stackoverflow.com/questions/39539812/how-can-another-file-be-included-in-a-hugo-markdown-page