最近我重新开发了一个网站。重新设计的站点将在 Markdown 中完成,并通过 Hugo 静态站点 生成器运行。
有没有办法在通过Hugo处理的Markdown网页中包含其他文件?如果是这样,怎么办?除非我错过了什么,否则 Hugo 文档 中没有解决这个问题。
使用 HTML 和一些服务器(至少是 Apache),您可以执行以下操作:
<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>
我尝试创建一个模板页面,将“一些内容”和“更多内容”之类的内容放入模板中,然后将包含在我的 .md 文件中的内容通过模板中的 {{ .Content }}“包含” 。但是,1)这似乎是使用模板的错误方法。 2)如果我需要的话,我还没有找到引入更多文件的方法。
对于内容文件,有两个选项:
mmark
作为 Markdown 渲染引擎及其 include
功能。将内容文件重命名为“*.mmark”。请参阅https://github.com/miekg/mmark我是Hugo的维护者。
我有一个自定义简码,用于通过 Markdown 将静态演示文件呈现为代码,这很简单:
布局/短代码/code.html
{{ $file := .Get "file" | readFile }}
{{ $lang := .Get "language" }}
{{ (print "```" $lang "\n" $file "\n```") | markdownify }}
内容/post/some-post.md
{{% code file="/static/some-script.js" language="js" %}}
一位同事建议创建一个短代码来帮助解决这个问题。虽然这并不完全是我想要的——它比我想要的更复杂——但还不错,而且我还没有找到更好的方法。因此,我使用 shortcode 和 CSV 文件实现了一个解决方案。简单的示例文件如下:
内容文件仍然(大部分)是 Markdown,看起来像这样:
+++
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 >}}
(请注意,
type = "pages"
只是引入一个模板,该模板修改/覆盖hugo-uno默认的pages/single.html模板,以使输出更清晰,以便在下面显示。)
布局/短代码/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>
静态/test_data.csv:
John, 1940-10-09
Paul, 1942-06-18
George, 1943-02-25
Ringo, 1940-07-07
我已经设法包含带有短代码的 Markdown 文件,例如:
创建文件
layouts/shortcodes/include.html
为:
{{ $file := .Get 0 }}
{{ (printf "%s%s" .Page.File.Dir $file) | readFile | replaceRE "^---[\\s\\S]+?---" "" | safeHTML }}
然后将降价包含为:
{{% include somefile.md %}}
这是它的参考链接:https://roneo.org/en/hugo-include-another-file-with-a-shortcode/