From 1ace505d272f8e4e907538bbdf8ebc86feae97a7 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Sat, 10 Apr 2021 13:55:03 +0200 Subject: [PATCH] feat: add option to include pages using the include shortcode (#92) BREAKING CHANGE: The include shortcode will render files as Markdown instead of HTML by default now. While the Markdown renderer should be able to render HTML as well this might still work as before. BREAKING CHANGE: The include shortcode option markdown was replaces by a generic type option that could be used to specify special include types. --- exampleSite/content/_includes/_index.md | 3 + exampleSite/content/_includes/include-page.md | 10 +++ exampleSite/content/shortcodes/includes.md | 89 ++++++++++++------- .../{includes => _includes}/example.html.part | 7 +- exampleSite/static/_includes/example.md.part | 7 ++ exampleSite/static/includes/table.md.part | 5 -- layouts/shortcodes/include.html | 17 ++-- 7 files changed, 94 insertions(+), 44 deletions(-) create mode 100644 exampleSite/content/_includes/_index.md create mode 100644 exampleSite/content/_includes/include-page.md rename exampleSite/static/{includes => _includes}/example.html.part (50%) create mode 100644 exampleSite/static/_includes/example.md.part delete mode 100644 exampleSite/static/includes/table.md.part diff --git a/exampleSite/content/_includes/_index.md b/exampleSite/content/_includes/_index.md new file mode 100644 index 0000000..9b76178 --- /dev/null +++ b/exampleSite/content/_includes/_index.md @@ -0,0 +1,3 @@ +--- +GeekdocHidden: true +--- diff --git a/exampleSite/content/_includes/include-page.md b/exampleSite/content/_includes/include-page.md new file mode 100644 index 0000000..bfc0534 --- /dev/null +++ b/exampleSite/content/_includes/include-page.md @@ -0,0 +1,10 @@ +_**Example page include**_ + +{{< hint info >}} +**Example Shortcode**\ +Shortcode used in an include page. +{{< /hint >}} + +| Head 1 | Head 2 | Head 3 | +| ------ | ------ | ------ | +| 1 | 2 | 3 | diff --git a/exampleSite/content/shortcodes/includes.md b/exampleSite/content/shortcodes/includes.md index 6628d3e..7e46db7 100644 --- a/exampleSite/content/shortcodes/includes.md +++ b/exampleSite/content/shortcodes/includes.md @@ -1,6 +1,11 @@ -Include shortcode can include files of different types. By specifying a language, the included file will have syntax highlighting. +--- +# Empty front matter as shortcodes could not be includes +# as first line of a file. +--- -## Shortcode +{{< toc >}} + +Include shortcode can include files of different types. By specifying a language, the included file will have syntax highlighting. ```tpl {{}} @@ -8,31 +13,18 @@ Include shortcode can include files of different types. By specifying a language Attributes: -| Name | Usage | default | -| ---------- | ----------------------------------------------------------------------------------------------------------------------------------- | ------------- | -| file | path to the included file relative to the hugo root | empty value | -| language\* | language for [syntax highlighting](https://gohugo.io/content-management/syntax-highlighting/#list-of-chroma-highlighting-languages) | empty value | -| markdown | included file is markdown | false | -| options | highlighting [options](https://gohugo.io/content-management/syntax-highlighting/#highlight-shortcode) | linenos=table | +| Name | Usage | default | +| -------- | ----------------------------------------------------------------------------------------------------------------------------------- | ------------- | +| file | path to the included file relative to the hugo root | undefined | +| language | language for [syntax highlighting](https://gohugo.io/content-management/syntax-highlighting/#list-of-chroma-highlighting-languages) | undefined | +| type | special include type (`html,page`) | undefined | +| options | highlighting [options](https://gohugo.io/content-management/syntax-highlighting/#highlight-shortcode) | linenos=table | -\* if not set, the content will be rendered as plain HTML +## Examples - -### Include \*.yml file with options +### Markdown file (default) -```tpl -{{}} -``` - - - -{{< include file="config.yaml" language="yaml" options="linenos=table,hl_lines=5-6,linenostart=100">}} - - - -### Include \*.md file - -Included markdown files will be rendered using the `RenderString` [function](https://gohugo.io/functions/renderstring/). +If no other options are specified, files will be rendered as Markdown using the `RenderString` [function](https://gohugo.io/functions/renderstring/). {{< hint warning >}} **Location of markdown files**\ @@ -40,21 +32,56 @@ If you include markdown files that should not get a menu entry, place them outsi {{< /hint >}} ```tpl -{{}} +{{}} ``` + - -{{< include file="static/includes/table.md.part" markdown="true" >}} - +{{< include file="/static/_includes/example.md.part" >}} + -### Include \*.html file +### Language files + +This method can be used to include source code files and keep them automatically up to date. + +```tpl +{{}} +``` + + + +{{< include file="config.yaml" language="yaml" options="linenos=table,hl_lines=5-6,linenostart=100">}} + + + +### Special include types + +#### HTML HTML content will be filtered by the `safeHTML` filter and added to the rendered page output. ```tpl -{{}} +{{}} ``` -{{< include file="static/includes/example.html.part" >}} +{{< include file="/static/_includes/example.html.part" type="html" >}} + +#### Pages + +In some situations, it can be helpful to include Markdown files that also contain shortcodes. While the [default method](#markdown-file-default) works fine to render plain Markdown, shortcodes are not parsed. The only way to get this to work is to use Hugo pages. There are several ways to structure these include pages, so whatever you do, keep in mind that Hugo needs to be able to render and serve these files as regular pages! How it works: + +1. First you need to create a directory **within** your content directory. For this example site `_includes` is used. +2. To prevent the theme from embedding the page in the navigation, create a file `_includes/_index.md` and add `GeekdocHidden: true` to the front matter. +3. Place your Markdown files within the `_includes` folder e.g. `/_includes/include-page.md`. Make sure to name it `*.md`. +4. Include the page using `{{}}`. + +Resulting structure should look like this: + +```Shell +_includes/ + ├── include-page.md + └── _index.md +``` + +{{< include file="/_includes/include-page.md" type="page" >}} diff --git a/exampleSite/static/includes/example.html.part b/exampleSite/static/_includes/example.html.part similarity index 50% rename from exampleSite/static/includes/example.html.part rename to exampleSite/static/_includes/example.html.part index 9d26592..f66d0dd 100644 --- a/exampleSite/static/includes/example.html.part +++ b/exampleSite/static/_includes/example.html.part @@ -1,6 +1,7 @@ -

This is heading 1

-

This is heading 2

-

This is heading 3

+

+Example HTML include +

+

This is heading 4

This is heading 5
This is heading 6
diff --git a/exampleSite/static/_includes/example.md.part b/exampleSite/static/_includes/example.md.part new file mode 100644 index 0000000..89d0525 --- /dev/null +++ b/exampleSite/static/_includes/example.md.part @@ -0,0 +1,7 @@ +_**Example Mardown include**_ + +File including a simple Markdown table. + +| Head 1 | Head 2 | Head 3 | +| ------ | ------ | ------ | +| 1 | 2 | 3 | diff --git a/exampleSite/static/includes/table.md.part b/exampleSite/static/includes/table.md.part deleted file mode 100644 index 4bf79a3..0000000 --- a/exampleSite/static/includes/table.md.part +++ /dev/null @@ -1,5 +0,0 @@ -#### Test Table - -| Head 1 | Head 2 | Head 3 | -|---|---|---| -| 1 | 2 | 3 | \ No newline at end of file diff --git a/layouts/shortcodes/include.html b/layouts/shortcodes/include.html index a3969ac..2eda972 100644 --- a/layouts/shortcodes/include.html +++ b/layouts/shortcodes/include.html @@ -1,10 +1,17 @@ {{ $file := .Get "file" }} +{{ $page := .Site.GetPage $file }} +{{ $type := .Get "type" }} +{{ $language := .Get "language" }} +{{ $options :=.Get "options" }} +
-{{- if eq (.Get "markdown") "true" -}} -{{- $file | readFile | $.Page.RenderString -}} -{{- else if (.Get "language") -}} -{{- highlight ($file | readFile) (.Get "language") (default "linenos=table" (.Get "options")) -}} +{{- if (.Get "language") -}} + {{- highlight ($file | readFile) $language (default "linenos=table" $options) -}} +{{- else if eq $type "html" -}} + {{- $file | readFile | safeHTML -}} +{{- else if eq $type "page" -}} + {{- with $page }}{{ .Content }}{{ end -}} {{- else -}} -{{ $file | readFile | safeHTML }} + {{- $file | readFile | $.Page.RenderString -}} {{- end -}}