From 8488d1ddca02da3dd49c7df4aac360c24089bede Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?sunsonliu=28=E5=88=98=E9=98=B3=29?= Date: Sun, 24 Nov 2024 16:22:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20#947=20=E5=AF=B9html=20block=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0urlProcessor=E5=9B=9E=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/hooks/HtmlBlock.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/core/hooks/HtmlBlock.js b/src/core/hooks/HtmlBlock.js index de4b5d63..3bf959f3 100644 --- a/src/core/hooks/HtmlBlock.js +++ b/src/core/hooks/HtmlBlock.js @@ -89,10 +89,29 @@ export default class HtmlBlock extends ParagraphBase { return whole.replace(//g, '>'); } } + let wholeStr = whole; + // 识别标签的href和src属性,并触发urlProcessor回调 + m1.replace(/^a .*? href="([^"]+)"/, (all, href) => { + const processedURL = this.$engine.$cherry.options.callback.urlProcessor(href, 'link'); + wholeStr = wholeStr.replace(/ href="[^"]+"/, ` href="${processedURL}"`); + }); + m1.replace(/^a href="([^"]+)"/, (all, href) => { + const processedURL = this.$engine.$cherry.options.callback.urlProcessor(href, 'link'); + wholeStr = wholeStr.replace(/ href="[^"]+"/, ` href="${processedURL}"`); + }); + m1.replace(/^img .*? src="([^"]+)"/, (all, src) => { + const processedURL = this.$engine.$cherry.options.callback.urlProcessor(src, 'image'); + wholeStr = wholeStr.replace(/ src="[^"]+"/, ` src="${processedURL}"`); + }); + m1.replace(/^img src="([^"]+)"/, (all, src) => { + const processedURL = this.$engine.$cherry.options.callback.urlProcessor(src, 'image'); + wholeStr = wholeStr.replace(/ src="[^"]+"/, ` src="${processedURL}"`); + }); + // 到达此分支的包含被尖括号包裹的AutoLink语法以及在白名单内的HTML标签 // 没有被AutoLink解析并渲染的标签会被DOMPurify过滤掉,正常情况下不会出现遗漏 // 临时替换完整的HTML标签首尾为$#60;和$#62;,供下一步剔除损坏的HTML标签 - return whole.replace(//g, '$#62;'); + return wholeStr.replace(//g, '$#62;'); }); // 替换所有形如「