fix: path relative
This commit is contained in:
parent
f4a6d6978b
commit
dfb763c1d9
|
|
@ -11,7 +11,22 @@ customElement("md-link", {}, (props, { element }) => {
|
|||
let disposeArticle: (() => void) | null = null;
|
||||
|
||||
// 从 element 的 textContent 获取链接目标
|
||||
const linkSrc = element?.textContent?.trim() || "";
|
||||
const rawLinkSrc = element?.textContent?.trim() || "";
|
||||
|
||||
// 从父节点 article 的 data-src 获取当前 markdown 文件完整路径
|
||||
const articleEl = element?.closest('article[data-src]');
|
||||
const articlePath = articleEl?.getAttribute('data-src') || '';
|
||||
|
||||
// 解析相对路径(相对于 markdown 文件所在目录)
|
||||
const resolvePath = (base: string, relative: string): string => {
|
||||
if (relative.startsWith('/')) {
|
||||
return relative;
|
||||
}
|
||||
const baseDir = base.substring(0, base.lastIndexOf('/') + 1);
|
||||
return baseDir + relative;
|
||||
};
|
||||
|
||||
const linkSrc = resolvePath(articlePath, rawLinkSrc);
|
||||
|
||||
// 查找包含此元素的 p 标签
|
||||
const parentP = element?.closest("p");
|
||||
|
|
@ -66,7 +81,7 @@ customElement("md-link", {}, (props, { element }) => {
|
|||
}}
|
||||
class="text-blue-600 hover:text-blue-800 hover:underline"
|
||||
>
|
||||
{linkSrc}
|
||||
{rawLinkSrc}
|
||||
</a>
|
||||
);
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue