I want realized the CDN refer URL with differnet vendors and can match the vendor by automatic. Then I designed use data in template like below:
data/resources.yaml
# Public CDN vendor list
vendors: {
"cdnjs": "//cdnjs.cloudflare.com/ajax/libs/${name}/${version}/${file}",
"unpkg": "//unpkg.com/${name}@${version}/${file}"
}
# Javascript Resources
js:
- name: anime
version: 3.2.1
file: lib/anime.min.js
integrity: sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=
partial/head/js.html
{{- $vendors := .Page.Site.Data.resources.vendors }}
{{- $pluginVen := .P.vendors.plugins }}
{{- $pluginCDN := index $vendors $pluginVen }}
<!-- Plugin js files -->
{{- $jsRes := .Page.Site.Data.resources.js }}
{{- range $js := $jsRes }}
{{- $npm := $js.name }}
{{- $file := $js.file }}
{{- if eq $pluginVen "cndjs" }}
{{- with $js.alias }}
{{- $npm = . }}
{{- end }}
{{ $file }}
{{- $file = replaceRE "(dist|lib|source\\/js)\\/" "" $file }}
{{- end }}
{{- $pluginJS := $pluginCDN }}
{{- $pluginJS = replace $pluginJS "${name}" $npm }}
{{- $pluginJS = replace $pluginJS "${version}" $js.version }}
{{- $pluginJS = replace $pluginJS "${file}" $file }}
<script src="{{ $pluginJS }}"></script>
{{- end }}
for example :
cdnjs: https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js
unpkg: https://unpkg.com/animejs@3.2.1/lib/anime.min.js
At first time I try use the regex pattern was:
{{- $file = replaceRE "(dist|lib|source\/js)\/" "" $file }}
But runing failed throws an exception message :
parse failed invalid syntax.
I change the regex parttern as the begining, and then got the empty result.
So how could I do?