feat: section link

This commit is contained in:
2026-02-26 09:53:30 +08:00
parent a250762ebe
commit dd4068926c
2 changed files with 59 additions and 3 deletions
+8 -2
View File
@@ -10,9 +10,14 @@ customElement("md-link", {}, (props, { element }) => {
let articleContainer: HTMLDivElement | undefined;
let disposeArticle: (() => void) | null = null;
// 从 element 的 textContent 获取链接目标
// 从 element 的 textContent 获取链接目标(支持 path#section 语法)
const rawLinkSrc = element?.textContent?.trim() || "";
// 解析 section(支持 path#section 语法)
const hashIndex = rawLinkSrc.indexOf('#');
const path = hashIndex >= 0 ? rawLinkSrc.slice(0, hashIndex) : rawLinkSrc;
const section = hashIndex >= 0 ? rawLinkSrc.slice(hashIndex + 1) : undefined;
// 隐藏原始文本内容
if (element) {
element.textContent = "";
@@ -31,7 +36,7 @@ customElement("md-link", {}, (props, { element }) => {
return baseDir + relative;
};
const linkSrc = resolvePath(articlePath, rawLinkSrc);
const linkSrc = resolvePath(articlePath, path);
// 查找包含此元素的 p 标签
const parentP = element?.closest("p");
@@ -61,6 +66,7 @@ customElement("md-link", {}, (props, { element }) => {
disposeArticle = render(() => (
<Article
src={linkSrc}
section={section}
onLoaded={() => console.log("Article loaded:", linkSrc)}
onError={(err) => console.error("Article error:", err)}
/>