|
58 | 58 | {{- $content := .Inner -}} |
59 | 59 | {{- $jsCode := "" -}} |
60 | 60 | {{- $configContent := "" -}} |
| 61 | +{{- $fnNames := slice -}} |
61 | 62 |
|
62 | | -{{- /* 1. Extract ```js code blocks */ -}} |
| 63 | +{{- /* 1. Extract ```js code blocks and function names */ -}} |
63 | 64 | {{- if findRE "```js[\\s\\S]*?```" $content -}} |
64 | 65 | {{- range findRE "```js([\\s\\S]*?)```" $content -}} |
65 | | - {{- $jsCode = printf "%s\n%s" $jsCode (replaceRE "```js([\\s\\S]*?)```" "$1" .) -}} |
| 66 | + {{- $block := replaceRE "```js([\\s\\S]*?)```" "$1" . -}} |
| 67 | + {{- $jsCode = printf "%s\n%s" $jsCode $block -}} |
| 68 | + {{- /* Extract function names like "var fmt = function" or "function fmt" */ -}} |
| 69 | + {{- range findRE "(?:var|let|const)\\s+(\\w+)\\s*=" $block -}} |
| 70 | + {{- $fnNames = $fnNames | append (replaceRE "(?:var|let|const)\\s+(\\w+)\\s*=.*" "$1" .) -}} |
| 71 | + {{- end -}} |
66 | 72 | {{- end -}} |
67 | 73 | {{- $content = replaceRE "```js[\\s\\S]*?```" "" $content -}} |
68 | 74 | {{- end -}} |
|
92 | 98 | var dom = document.getElementById('{{ $id }}'); |
93 | 99 | if (!dom) return; |
94 | 100 |
|
95 | | - {{- /* Output JavaScript function definitions */ -}} |
| 101 | + // Global function registry (survives minification) |
| 102 | + window._echartsFns = window._echartsFns || {}; |
| 103 | + |
| 104 | + {{- /* Output JavaScript function definitions and register them globally */ -}} |
96 | 105 | {{ $jsCode | safeJS }} |
| 106 | + {{- range $fnNames }} |
| 107 | + window._echartsFns['{{ . }}'] = {{ . | safeJS }}; |
| 108 | + {{- end }} |
97 | 109 |
|
98 | 110 | {{- /* Determine theme */ -}} |
99 | 111 | {{- if $theme }} |
|
105 | 117 | var chart = echarts.init(dom, theme); |
106 | 118 | var optionsJson = {{ $options | jsonify | safeJS }}; |
107 | 119 |
|
108 | | - // Replace $fn:xxx references with actual functions |
| 120 | + // Replace $fn:xxx references with actual functions from global registry |
109 | 121 | function replaceFnRefs(obj) { |
110 | 122 | if (typeof obj === 'string' && obj.startsWith('$fn:')) { |
111 | 123 | var fnName = obj.substring(4); |
112 | | - return eval(fnName); |
| 124 | + return window._echartsFns[fnName]; |
113 | 125 | } |
114 | 126 | if (Array.isArray(obj)) { |
115 | 127 | return obj.map(replaceFnRefs); |
|
0 commit comments