From 9bcdbe33254ef51c019ed274c45e992d59713308 Mon Sep 17 00:00:00 2001 From: Cell <1024@lruihao.cn> Date: Mon, 22 Dec 2025 13:59:47 +0800 Subject: [PATCH] feat: add `env` shortcode for conditional content rendering based on Hugo environment --- apps/test/content/posts/codeblock-test.md | 8 +++----- layouts/_partials/init/index.html | 2 +- layouts/_shortcodes/env.html | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 layouts/_shortcodes/env.html diff --git a/apps/test/content/posts/codeblock-test.md b/apps/test/content/posts/codeblock-test.md index 4091c469..ff8e0163 100644 --- a/apps/test/content/posts/codeblock-test.md +++ b/apps/test/content/posts/codeblock-test.md @@ -194,8 +194,6 @@ function isObjectLiteral(str) { ## Gist Embed -```md -{{}} -``` - - +{{< env "production" >}} +{{< gist Lruihao fb8b2d0353465c4d40bf74818db80710 >}} +{{< /env >}} diff --git a/layouts/_partials/init/index.html b/layouts/_partials/init/index.html index 01e9ca84..8b713b51 100644 --- a/layouts/_partials/init/index.html +++ b/layouts/_partials/init/index.html @@ -1,4 +1,4 @@ -{{- hugo.Store.Set "version" "v0.4.0-alpha.3-20251222034819-3fa866fa" -}} +{{- hugo.Store.Set "version" "v0.4.0-alpha.3-20251222065334-6cf32775" -}} {{- .Store.Set "this" dict -}} {{- partial "init/detection-env.html" . -}} diff --git a/layouts/_shortcodes/env.html b/layouts/_shortcodes/env.html new file mode 100644 index 00000000..a875ecc2 --- /dev/null +++ b/layouts/_shortcodes/env.html @@ -0,0 +1,21 @@ +{{- /* + A shortcode to conditionally render content based on the current Hugo environment. + Hugo environments (development, production, etc.) usage reference: + - https://gohugo.io/functions/hugo/environment/ + - https://gohugo.io/configuration/all/#environment + + For example, to render content only in the "production" environment: + {{< env "production" >}} + This content is only visible in production. + {{< /env >}} +*/ -}} + +{{- $env := .Get 0 -}} +{{- if eq $env hugo.Environment -}} + {{- .Inner | .Page.RenderString -}} +{{- else if hugo.IsServer -}} +
+ [Hugo Server]: Click to expand {{ $env }} environment content. +
{{- printf `%s` "\n" | trim .Inner -}}
+
+{{- end -}}