From 91fe87af8bf5fe9bc70f3fc66dcc5da4ed0e57fe Mon Sep 17 00:00:00 2001 From: Robert Fekete Date: Tue, 14 Nov 2023 09:57:25 +0100 Subject: [PATCH] Adds shortcode to include remote markdown file Fixes https://github.com/google/docsy/issues/1716 --- layouts/shortcodes/include-remote-md.html | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 layouts/shortcodes/include-remote-md.html diff --git a/layouts/shortcodes/include-remote-md.html b/layouts/shortcodes/include-remote-md.html new file mode 100644 index 0000000000..023a1fa698 --- /dev/null +++ b/layouts/shortcodes/include-remote-md.html @@ -0,0 +1,26 @@ +{{/* Fetch a remote markdown file and include it in the page. If the file has a frontmatter, define the marker of the frontmatter in the second parameter (defaults to "---"). If the content after the frontmatter includes the marker (for example, includes "---" as part of a markdown-formatted table) the content included will be incomplete. */}} +{{ $url := .Get 0 }} +{{ $marker := .Get 1 | default "---" }} + +{{/* Do not change the indentation of the following block */}} +{{ with resources.GetRemote $url }} + {{ with .Err }} + {{ errorf "%s" . }} +{{ else }} +{{/* Test for frontmatter */}} +{{ if hasPrefix .Content $marker }} +{{ $split := split .Content $marker }} + +{{ with strings.Contains .Content $marker }} {{- warnf "Remote snippet includes frontmatter marker, content is truncated: %s" $url -}} +{{ end }} + +{{/* Output stuff after the frontmatter if a frontmatter was detected. */}} +{{- index $split 2 | markdownify | safeHTML -}} +{{ else }} +{{/* Output the content of the file if no frontmatter was detected. */}} +{{- .Content | safeHTML -}} +{{ end }} +{{ end }} +{{ else }} + {{ errorf "Unable to get remote resource %q" $url }} +{{ end }} \ No newline at end of file