From 2a53dbac37f3478d0e50eefae1a5779b50ff9a03 Mon Sep 17 00:00:00 2001 From: Cell <1024@lruihao.cn> Date: Sun, 21 Jul 2024 19:02:11 +0800 Subject: [PATCH] :sparkles: Feat: add echarts support for code blocks --- layouts/_default/_markup/render-codeblock-echarts.html | 1 + layouts/partials/assets.html | 2 +- layouts/partials/plugin/echarts.html | 7 +++++++ layouts/shortcodes/echarts.html | 10 +++------- 4 files changed, 12 insertions(+), 8 deletions(-) create mode 100644 layouts/_default/_markup/render-codeblock-echarts.html create mode 100644 layouts/partials/plugin/echarts.html diff --git a/layouts/_default/_markup/render-codeblock-echarts.html b/layouts/_default/_markup/render-codeblock-echarts.html new file mode 100644 index 00000000..00c669c2 --- /dev/null +++ b/layouts/_default/_markup/render-codeblock-echarts.html @@ -0,0 +1 @@ +{{- dict "Options" .Attributes "Inner" .Inner | partial "plugin/echarts.html" -}} diff --git a/layouts/partials/assets.html b/layouts/partials/assets.html index 0d77ad3c..0d370f53 100644 --- a/layouts/partials/assets.html +++ b/layouts/partials/assets.html @@ -124,7 +124,7 @@ {{- end -}} {{- /* ECharts */ -}} -{{- if .HasShortcode "echarts" -}} +{{- if .Store.Get "hasEcharts" -}} {{- $source := $cdn.echartsJS | default "lib/echarts/echarts.min.js" -}} {{- dict "Source" $source "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}} {{- $lightTheme := resources.Get "lib/echarts/theme/light.yml" | transform.Unmarshal -}} diff --git a/layouts/partials/plugin/echarts.html b/layouts/partials/plugin/echarts.html new file mode 100644 index 00000000..df6a2718 --- /dev/null +++ b/layouts/partials/plugin/echarts.html @@ -0,0 +1,7 @@ +{{- /* Echarts support for code fences extended and shortcodes. */ -}} +{{- $width := .Options.width | default "100%" -}} +{{- $height := .Options.height | default "30rem" -}} +{{- $attrs := printf `style="width: %v; height: %v;"` $width $height -}} +
+ +{{- page.Store.Set "hasEcharts" true -}} diff --git a/layouts/shortcodes/echarts.html b/layouts/shortcodes/echarts.html index f3ce6e9f..3ce66b61 100644 --- a/layouts/shortcodes/echarts.html +++ b/layouts/shortcodes/echarts.html @@ -1,7 +1,3 @@ -{{- $content := .Inner | transform.Unmarshal | jsonify -}} -{{- $width := cond .IsNamedParams (.Get "width") (.Get 0) | default "100%" -}} -{{- $height := cond .IsNamedParams (.Get "height") (.Get 1) | default "30rem" -}} -{{- $attrs := printf `style="width: %v; height: %v;"` $width $height -}} -
- -{{- /* EOF */ -}} +{{- $options := .Get "width" | default (.Get 0) | dict "width" -}} +{{- $options = .Get "height" | default (.Get 1) | dict "height" | merge $options -}} +{{- dict "Options" $options "Inner" .Inner | partial "plugin/echarts.html" -}}