From a3f09559e465ea1255baf83345fc7fd1b2da62b3 Mon Sep 17 00:00:00 2001
From: Caio Lente <7017340+clente@users.noreply.github.com>
Date: Fri, 27 Oct 2023 19:30:53 -0300
Subject: Conditional CSS
---
README.md | 4 ++++
layouts/_default/_markup/render-codeblock.html | 6 ++++++
layouts/_default/baseof.html | 6 ++++--
layouts/shortcodes/highlight.html | 5 +++++
4 files changed, 19 insertions(+), 2 deletions(-)
create mode 100644 layouts/_default/_markup/render-codeblock.html
create mode 100644 layouts/shortcodes/highlight.html
diff --git a/README.md b/README.md
index d6d242c..3b73cd4 100644
--- a/README.md
+++ b/README.md
@@ -119,6 +119,10 @@ already implemented:
writing the CSS you need in `assets/{custom_css}.css` and then including
`style: "{custom_css}.css"` in the [front
matter](https://gohugo.io/content-management/front-matter/) of said page.
+- Conditional CSS (EXPERIMENTAL): since **Bear Cub** does syntax highlighting
+ without inline styles (see `config.toml` for more information), it only load
+ its `syntax.css` if, and only if, a code block is actually present in the
+ current page.
- Dynamic social card generation (EXPERIMENTAL): if you don't add preview images
to a post, this template will generate one based on the title. You can see an
example below.
diff --git a/layouts/_default/_markup/render-codeblock.html b/layouts/_default/_markup/render-codeblock.html
new file mode 100644
index 0000000..6e1a075
--- /dev/null
+++ b/layouts/_default/_markup/render-codeblock.html
@@ -0,0 +1,6 @@
+
+{{ .Page.Store.Set "hasCodeBlock" true }}
+
+
+{{ $result := transform.HighlightCodeBlock . }}
+{{ $result.Wrapped }}
\ No newline at end of file
diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html
index fdfa47c..475d6f9 100644
--- a/layouts/_default/baseof.html
+++ b/layouts/_default/baseof.html
@@ -14,8 +14,10 @@
{{ $style := resources.Get "style.css" | minify }}
- {{ $syntax := resources.Get "syntax.css" | minify }}
-
+ {{ if (.Page.Store.Get "hasCodeBlock") }}
+ {{ $syntax := resources.Get "syntax.css" | minify }}
+
+ {{ end }}
{{ with .Params.style }}
{{ $extra := resources.Get . | minify }}
diff --git a/layouts/shortcodes/highlight.html b/layouts/shortcodes/highlight.html
new file mode 100644
index 0000000..6c86ca8
--- /dev/null
+++ b/layouts/shortcodes/highlight.html
@@ -0,0 +1,5 @@
+
+{{ .Page.Store.Set "hasCodeBlock" true }}
+
+
+{{ if len .Params | eq 2 }}{{ highlight (trim .InnerDeindent "\n\r") (.Get 0) (.Get 1) }}{{ else }}{{ highlight (trim .InnerDeindent "\n\r") (.Get 0) "" }}{{ end }}
\ No newline at end of file
--
cgit v1.2.3