Skip to content

Commit

Permalink
✨ Feat: add echarts support for code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Lruihao committed Jul 21, 2024
1 parent 9925af0 commit 2a53dba
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions layouts/_default/_markup/render-codeblock-echarts.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{- dict "Options" .Attributes "Inner" .Inner | partial "plugin/echarts.html" -}}
2 changes: 1 addition & 1 deletion layouts/partials/assets.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}
Expand Down
7 changes: 7 additions & 0 deletions layouts/partials/plugin/echarts.html
Original file line number Diff line number Diff line change
@@ -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 -}}
<div class="echarts" {{ $attrs | safeHTMLAttr }}></div>
<template>{{ .Inner | transform.Unmarshal | jsonify }}</template>
{{- page.Store.Set "hasEcharts" true -}}
10 changes: 3 additions & 7 deletions layouts/shortcodes/echarts.html
Original file line number Diff line number Diff line change
@@ -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 -}}
<div class="echarts" {{ $attrs | safeHTMLAttr }}></div>
<template>{{ $content }}</template>
{{- /* 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" -}}

0 comments on commit 2a53dba

Please sign in to comment.