From 5747ca67d2288cac581b6fdc97ef0a34d56b10b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Weber?= Date: Mon, 11 Mar 2024 18:56:18 +0100 Subject: [PATCH] highlight: support Markdown attributes #795 --- layouts/partials/shortcodes/highlight.html | 53 ++++++++++++++-------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/layouts/partials/shortcodes/highlight.html b/layouts/partials/shortcodes/highlight.html index bc474814878..1d7228ea06f 100644 --- a/layouts/partials/shortcodes/highlight.html +++ b/layouts/partials/shortcodes/highlight.html @@ -35,10 +35,19 @@ {{- $otherOptions = $otherOptions | merge (dict $k $v) }} {{- end }} {{- end }} + +{{- /* separate attributes from options */}} +{{- $hl_inline := false }} +{{- if and (isset $page.Params "markup") (isset $page.Params.markup "highlight") (isset $page.Params.markup.highlight "hl_inline") }} + {{- $hl_inline = $page.Params.markup.highlight.hl_inline }} +{{- end }} {{- $options = $options | merge $otherOptions }} {{- $otherOptions := dict }} {{- range $k, $v := $options }} - {{- if eq $k "title" }} + {{- if eq $k "hl_inline" }} + {{- $hl_inline = $v }} + {{- $otherOptions = (dict $k $v) | merge $otherOptions }} + {{- else if eq $k "title" }} {{- $otherAttributes = (dict $k $v) | merge $otherAttributes }} {{- else if eq $k "wrap" }} {{- $otherAttributes = (dict $k $v) | merge $otherAttributes }} @@ -47,21 +56,8 @@ {{- end }} {{- end }} {{- $options = $otherOptions }} -{{- $attributes = $attributes | merge $otherAttributes }} -{{- $hl_inline := false }} -{{- if and (isset $page.Params "markup") (isset $page.Params.markup "highlight") (isset $page.Params.markup.highlight "hl_inline") }} - {{- $hl_inline = $page.Params.markup.highlight.hl_inline }} -{{- end }} -{{- range $k, $v := $options }} - {{- if eq $k "hl_inline" }} - {{- $hl_inline = $v }} - {{- end }} -{{- end }} -{{- $params := slice }} -{{- range $k, $v := $options }} - {{- $params = $params | append (printf "%s=%s" $k (string $v)) }} -{{- end }} -{{- $params = delimit $params ", " }} + +{{- /* separate shortcode attributes from rest and put it back to options */}} {{- $title := "" }} {{- $wrap := true }} {{- if isset $page.Site.Params "highlightwrap" }} @@ -70,23 +66,40 @@ {{- if isset $page.Params "highlightwrap" }} {{- $wrap = $page.Params.highlightWrap }} {{- end }} +{{- $attributes = $attributes | merge $otherAttributes }} +{{- $otherAttributes := dict }} {{- range $k, $v := $attributes }} {{- if eq $k "title" }} {{- $title = $v }} {{- else if eq $k "wrap" }} {{- $wrap = $v }} + {{- else }} + {{- $otherAttributes = (dict $k $v) | merge $otherAttributes }} {{- end }} {{- end }} +{{- $attributes = $otherAttributes }} {{- if eq (printf "%T" $wrap) "string" }} {{- $wrap = (eq $wrap "true") }} {{- end }} -{{- $content = highlight $content $type $params }} -{{- $content = replaceRE "^([\\s\\n\\r]*)(${2}" $content }} + +{{- /* enrich options */}} +{{- $attributes = merge $attributes (dict "class" (delimit (append (index $attributes "class" | default slice) "highlight" slice) " ")) }} {{- if $wrap }} - {{- $content = replaceRE "^([\\s\\n\\r]*${2}" $content }} +{{- $content = replaceRE "(class=\")([^\"]*)\"" (printf "%s" (delimit $div " ")) $content 1 }} {{- if and $title (not $hl_inline) }} - {{ partial "shortcodes/tab.html" (dict + {{- partial "shortcodes/tab.html" (dict "page" $page "title" $title "content" $content