Skip to content

Commit fe34deb

Browse files
committed
fix: 网址形式的内容在高亮块显示不为herf问题
1 parent b20e06d commit fe34deb

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
feat: 仅新增复制按钮
44

55
feat: 新增自动高亮
6+
7+
fix: 网址形式的内容在高亮块显示不为 herf 问题

scripts/content.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,31 @@ function highlightCodeBlocks() {
134134
continue;
135135
} else {
136136
if (languageFlag) {
137-
codeContentArr.push(memo_p_array[i].innerHTML);
137+
// 处理内容中的a标签
138+
let content = memo_p_array[i].innerHTML;
139+
140+
// 创建临时DOM元素来解析HTML
141+
const tempDiv = document.createElement('div');
142+
tempDiv.innerHTML = content;
143+
144+
// 查找所有a标签
145+
const links = tempDiv.querySelectorAll('a');
146+
links.forEach((link) => {
147+
// 获取href属性
148+
const href = link.getAttribute('href');
149+
if (href) {
150+
// 替换整个a标签为@+href
151+
const linkText = `${href}`;
152+
// 创建文本节点
153+
const textNode = document.createTextNode(linkText);
154+
// 替换a标签为文本节点
155+
link.parentNode.replaceChild(textNode, link);
156+
}
157+
});
158+
159+
// 获取处理后的内容
160+
content = tempDiv.innerHTML;
161+
codeContentArr.push(content);
138162
memo.removeChild(memo_p_array[i]);
139163
}
140164
}

0 commit comments

Comments
 (0)