From 787ab3e818c694521f6de7efb4f8e1ba5c49059a Mon Sep 17 00:00:00 2001 From: liuycy Date: Thu, 9 May 2024 14:25:17 +0800 Subject: [PATCH] fix: base path for relative path in markdown (#170 close alist-org/alist#6419) --- src/components/Markdown.tsx | 9 ++++----- src/utils/path.ts | 4 ++++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/components/Markdown.tsx b/src/components/Markdown.tsx index d55b28657c..0fd91017d7 100644 --- a/src/components/Markdown.tsx +++ b/src/components/Markdown.tsx @@ -12,12 +12,12 @@ import { Anchor, Box, List, ListItem } from "@hope-ui/solid" import { useParseText, useRouter } from "~/hooks" import { EncodingSelect } from "." import once from "just-once" -import { pathDir, pathJoin, api } from "~/utils" +import { pathDir, pathJoin, api, pathResolve } from "~/utils" import { createStorageSignal } from "@solid-primitives/storage" import { isMobile } from "~/utils/compatibility.js" import { useScrollListener } from "~/pages/home/toolbar/BackTop.jsx" import { Motion } from "@motionone/solid" -import { getMainColor } from "~/store" +import { getMainColor, me } from "~/store" type TocItem = { indent: number; text: string; tagName: string; key: string } @@ -194,10 +194,9 @@ export function Markdown(props: { if (url.startsWith("/")) { url = `${api}/d${url}` } else { - url = url.replace("./", "") url = `${api}/d${pathJoin( - props.readme ? pathname() : pathDir(pathname()), - url, + me().base_path, + pathResolve(props.readme ? pathname() : pathDir(pathname()), url), )}` } const ans = `![${name}](${url})` diff --git a/src/utils/path.ts b/src/utils/path.ts index bc4a2885cf..67549b7558 100644 --- a/src/utils/path.ts +++ b/src/utils/path.ts @@ -13,6 +13,10 @@ export const standardizePath = (path: string, noRootSlash?: boolean) => { return path } +export const pathResolve = (...paths: string[]) => { + return new URL(pathJoin(...paths), location.origin).pathname +} + export const pathJoin = (...paths: string[]) => { return paths.join("/").replace(/\/{2,}/g, "/") }