Skip to content

Commit

Permalink
feat: #947 对html block增加urlProcessor回调
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsonliu committed Nov 24, 2024
1 parent ab2eb4a commit 8488d1d
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/core/hooks/HtmlBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,29 @@ export default class HtmlBlock extends ParagraphBase {
return whole.replace(/</g, '&#60;').replace(/>/g, '&#62;');
}
}
let wholeStr = whole;
// 识别<a>和<img>标签的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, '$#60;').replace(/>/g, '$#62;');
return wholeStr.replace(/</g, '$#60;').replace(/>/g, '$#62;');
});
// 替换所有形如「<abcd」和「</abcd」的左尖括号
$str = $str.replace(/<(?=\/?(\w|\n|$))/g, '&#60;');
Expand Down

0 comments on commit 8488d1d

Please sign in to comment.