From 09b40580b07244bdf316ad01a83fc6b5db19f0f7 Mon Sep 17 00:00:00 2001 From: wompmacho Date: Sat, 28 Mar 2026 04:22:29 +0000 Subject: [PATCH] fixed the include partial and its configuration. The primary issues were the lack of frontmatter stripping and restricted file system access for files outside the Hugo project root. Changes Made: 1. Improved include.html Partial: * Page Support: Now uses .Site.GetPage first, allowing files to be included via Hugo's virtual filesystem (mounts). * Frontmatter Stripping: Added a regex-based stripper to ensure that if a raw file is read via readFile, its YAML/TOML frontmatter isn't rendered as text/horizontal rules. * Shortcode Processing: Confirmed that RenderString correctly processes shortcodes within included content. 2. Updated Hugo Configuration: * Added [[module.mounts]] to /srv/dev/hugo/wiki/config/_default/hugo.toml to expose your private documentation directory (/srv/docs/private) to Hugo as content/private. --- config/_default/hugo.toml | 9 ++++++++- layouts/partials/include.html | 25 ++++++++++++++++++------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/config/_default/hugo.toml b/config/_default/hugo.toml index d0af0c7..8c9257d 100644 --- a/config/_default/hugo.toml +++ b/config/_default/hugo.toml @@ -41,4 +41,11 @@ enableGitInfo = true [markup.tableOfContents] endLevel = 3 ordered = false - startLevel = 2 \ No newline at end of file + startLevel = 2 +[module] + [[module.mounts]] + source = "content" + target = "content" + [[module.mounts]] + source = "/srv/docs/private" + target = "content/private" diff --git a/layouts/partials/include.html b/layouts/partials/include.html index 5633f33..a4202c2 100644 --- a/layouts/partials/include.html +++ b/layouts/partials/include.html @@ -2,16 +2,27 @@ Partial: include.html Handles including external Markdown files and rendering them. Parameters: - - context: The Page context (required for RenderString) - - path: Path to the file, relative to the project root + - context: The Page context (required for RenderString/GetPage) + - path: Path to the file, relative to the project root or content root */}} {{- $path := .path -}} {{- $context := .context -}} -{{- if fileExists $path -}} - {{- $content := readFile $path -}} - {{- $context.RenderString $content -}} +{{/* Try to get as a Page first (supports mounts and already handles frontmatter) */}} +{{- $p := $context.Site.GetPage $path -}} +{{- if $p -}} + {{- $p.Content -}} {{- else -}} - {{- warnf "Include file not found: %s" $path -}} -

[Include Error: File "{{ $path }}" not found]

+ {{/* Fallback to readFile for non-page files */}} + {{- if fileExists $path -}} + {{- $content := readFile $path -}} + {{/* Strip frontmatter if it exists */}} + {{- if (findRE "^---" $content) -}} + {{- $content = replaceRE "^(?s)---.*?---[\\r\\n]*" "" $content -}} + {{- end -}} + {{- $context.RenderString $content -}} + {{- else -}} + {{- warnf "Include file not found: %s" $path -}} +

[Include Error: File "{{ $path }}" not found]

+ {{- end -}} {{- end -}}