Skip to content

Commit

Permalink
Conditional CSS
Browse files Browse the repository at this point in the history
  • Loading branch information
clente committed Oct 27, 2023
1 parent 95746b1 commit a3f0955
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions layouts/_default/_markup/render-codeblock.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!-- https://github.com/jmooring/hugo-testing/blob/hugo-forum-topic-40998/layouts/_default/_markup/render-codeblock-katex.html -->
{{ .Page.Store.Set "hasCodeBlock" true }}

<!-- https://www.veriphor.com/articles/code-block-render-hooks/ -->
{{ $result := transform.HighlightCodeBlock . }}
{{ $result.Wrapped }}
6 changes: 4 additions & 2 deletions layouts/_default/baseof.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
{{ $style := resources.Get "style.css" | minify }}
<link href="{{ $style.RelPermalink }}" rel="stylesheet">

{{ $syntax := resources.Get "syntax.css" | minify }}
<link href="{{ $syntax.RelPermalink }}" rel="stylesheet">
{{ if (.Page.Store.Get "hasCodeBlock") }}
{{ $syntax := resources.Get "syntax.css" | minify }}
<link href="{{ $syntax.RelPermalink }}" rel="stylesheet">
{{ end }}

{{ with .Params.style }}
{{ $extra := resources.Get . | minify }}
Expand Down
5 changes: 5 additions & 0 deletions layouts/shortcodes/highlight.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- https://github.com/jmooring/hugo-testing/blob/hugo-forum-topic-40998/layouts/_default/_markup/render-codeblock-katex.html -->
{{ .Page.Store.Set "hasCodeBlock" true }}

<!-- https://github.com/gohugoio/hugo/blob/master/tpl/tplimpl/embedded/templates/shortcodes/highlight.html -->
{{ if len .Params | eq 2 }}{{ highlight (trim .InnerDeindent "\n\r") (.Get 0) (.Get 1) }}{{ else }}{{ highlight (trim .InnerDeindent "\n\r") (.Get 0) "" }}{{ end }}

0 comments on commit a3f0955

Please sign in to comment.