feat: section link
This commit is contained in:
@@ -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)}
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user