如何将另一个文件包含在 Hugo/Markdown 页面中?

How can another file be Included in a Hugo/Markdown page?

最近给我重新开发了一个网站。重新制作的网站将在 Markdown and run through the Hugo static site 生成器中完成。

有没有办法在通过 Hugo 处理的 Markdown 网页中包含其他文件?如果是这样,如何?除非我遗漏了什么,否则 Hugo docs.

中没有解决这个问题

使用 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>

我尝试创建一个模板页面,将 "Some content" 和 "More content" 之类的内容放入模板,然后将包含的内容放入我的 .md 文件中,该文件通过 { { .Content }} 在模板中。但是,1) 这似乎是使用模板的错误方法。 2) 如果需要,我还没有找到引入更多文件的方法。

一位同事建议 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 和一个 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" 只是引入了一个模板,其中 modifies/overrides hugo-uno 默认的 pages/single。html 模板使输出更清晰,目的是显示在下方。)

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

这张图片显示了事物的渲染方式:

Data-driven Content page in the Hugo docs 也很有帮助。

对于内容文件,有两个选项:

  1. 简码。功能强大且有据可查。
  2. 使用 mmark 作为具有 include 功能的降价渲染引擎。将内容文件重命名为“*.mmark”。参见 https://github.com/miekg/mmark

我是Hugo的维护者

我有一个自定义简码,用于通过 markdown 将我的静态演示文件呈现为代码,很简单:

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" %}}