Skip to content

Commit

Permalink
fix: regex for image and video
Browse files Browse the repository at this point in the history
  • Loading branch information
reuixiy committed Oct 27, 2024
1 parent c3e45ed commit 9a9827e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions layouts/partials/utils/auto-detect-images.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<!-- https://regex101.com/ -->
{{- $imgs := slice -}}
{{- if and .IsPage (in .Site.Params.mainSections .Section) -}}
{{- range (findRE `<img src="/?([^"]+)` .Content | uniq) -}}
{{- $url := replaceRE `<img src="/?([^"]+)` `$1` . -}}
{{- range (findRE `<img\s+[^>]*?src="/?([^"]+)` .Content | uniq) -}}
{{- $url := replaceRE `<img\s+[^>]*?src="/?([^"]+)` `$1` . -}}
{{- $imgs = union $imgs (slice $url) -}}
{{- end -}}
{{- end -}}
Expand Down
12 changes: 6 additions & 6 deletions layouts/partials/utils/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@
<!-- Image Hosting -->
{{- if and .Site.Params.enableImageHost (eq hugo.Environment "production") -}}
{{- $hostURL := strings.TrimSuffix "/" .Site.Params.imageHostURL -}}
{{- $temps := findRE `<(img) src="/?([^":]+)` $Content | uniq -}}
{{- $temps := findRE `<(img)\s+[^>]*?src="/?([^":]+)` $Content | uniq -}}
{{- with $temps -}}
{{- range . -}}
{{- $url := replaceRE `<(img) src="/?([^":]+)` `$2` . -}}
{{- $url := replaceRE `<(img)\s+[^>]*?src="/?([^":]+)` `$2` . -}}
{{- if ne (substr $url 0 4) "http" -}}
{{- $prefix := replaceRE `(<(img) src=")/?([^":]+)` `$1` . -}}
{{- $prefix := replaceRE `(<(img)\s+[^>]*?src=")/?([^":]+)` `$1` . -}}
{{- $replacement := (printf `%s%s/%s` $prefix $hostURL $url) -}}
{{- $Content = replace $Content . $replacement -}}
{{- end -}}
Expand All @@ -130,12 +130,12 @@
<!-- Video Hosting -->
{{- if and .Site.Params.enableVideoHost (eq hugo.Environment "production") -}}
{{- $hostURL := strings.TrimSuffix "/" .Site.Params.videoHostURL -}}
{{- $temps := findRE `<(video) src="/?([^":]+)` $Content | uniq -}}
{{- $temps := findRE `<(video)\s+[^>]*?src="/?([^":]+)` $Content | uniq -}}
{{- with $temps -}}
{{- range . -}}
{{- $url := replaceRE `<(video) src="/?([^":]+)` `$2` . -}}
{{- $url := replaceRE `<(video)\s+[^>]*?src="/?([^":]+)` `$2` . -}}
{{- if ne (substr $url 0 4) "http" -}}
{{- $prefix := replaceRE `(<(video) src=")/?([^":]+)` `$1` . -}}
{{- $prefix := replaceRE `(<(video)\s+[^>]*?src=")/?([^":]+)` `$1` . -}}
{{- $replacement := (printf `%s%s/%s` $prefix $hostURL $url) -}}
{{- $Content = replace $Content . $replacement -}}
{{- end -}}
Expand Down

0 comments on commit 9a9827e

Please sign in to comment.