replaceRE: Regex URL issues with regex tested on regex101

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?

Do you have a public repository?

Also try this (note the backticks instead of double quotes):

{{- $file = replaceRE `(dist|lib|source\/js)\/` "" $file }}

You might try messing around with regular expressions in go to see if it’s matching as expected Go Playground - The Go Programming Language

1 Like

@IanMadd Thanks for you reply.

Sorrory , at first that these code was in my computer local status not push remote repository.

And I try you suggest use backticks then got the empty result again, But your suggestion there can run correct under Go playground (Seems not fast visit in China) environment.

Maybe there Hugo engine had some logic to handler the Regex pattern, I guess.

I tried this locally:

{{- $file := "lib/anime.min.js" -}}
{{- $pluginJS := "//cdnjs.cloudflare.com/ajax/libs/${name}/${version}/${file}" -}}
{{- $file = replaceRE `(dist|lib|source\/js)\/` "" $file -}}
{{- $file -}}
{{- $pluginJS = replace $pluginJS "${file}" $file }}
{{- $pluginJS -}}

And it returned:

  • anime.min.js
  • //cdnjs.cloudflare.com/ajax/libs/${name}/${version}/anime.min.js

So the problem might not be the regex but the data file formatting or functions to access the data.

lib vs libs ?

So sorry, It’s my mistake, your suggest was right when the first reply.

When I debug the code was found that the variable was got nil value which name call $pluginCDN that is the root case why the result always got empty. Because I set an error value in config.yaml file, replace the vendor’s name from cdnjs to cndjs, and is not equal with data file which vendor list.

Then change the vender variable value to cdnjs, and all thing was good.

Thanks all.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.