From 9a9827e11b8cca383bc0981d33938b0e7af0c0d6 Mon Sep 17 00:00:00 2001 From: reuixiy Date: Sun, 27 Oct 2024 13:03:07 +0800 Subject: [PATCH] fix: regex for image and video --- layouts/partials/utils/auto-detect-images.html | 4 ++-- layouts/partials/utils/content.html | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/layouts/partials/utils/auto-detect-images.html b/layouts/partials/utils/auto-detect-images.html index 1aaf04de..21a5fa70 100644 --- a/layouts/partials/utils/auto-detect-images.html +++ b/layouts/partials/utils/auto-detect-images.html @@ -3,8 +3,8 @@ {{- $imgs := slice -}} {{- if and .IsPage (in .Site.Params.mainSections .Section) -}} - {{- range (findRE `]*?src="/?([^"]+)` .Content | uniq) -}} + {{- $url := replaceRE `]*?src="/?([^"]+)` `$1` . -}} {{- $imgs = union $imgs (slice $url) -}} {{- end -}} {{- end -}} diff --git a/layouts/partials/utils/content.html b/layouts/partials/utils/content.html index 09a1b204..adf21c57 100644 --- a/layouts/partials/utils/content.html +++ b/layouts/partials/utils/content.html @@ -114,12 +114,12 @@ {{- 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 -}} @@ -130,12 +130,12 @@ {{- 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 -}}