Skip to content

Commit

Permalink
fix(theme): fix link resolve, close #4044
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Apr 7, 2024
1 parent 3e53619 commit 04c840d
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 30 deletions.
4 changes: 2 additions & 2 deletions packages/theme/src/client/components/AutoLink.ts
Expand Up @@ -74,7 +74,7 @@ export default defineComponent({
);

// Resolve the `rel` attr
const anchorRel = computed(
const linkRel = computed(
() =>
config.value.rel ||
(isBlankTarget.value ? "noopener noreferrer" : null),
Expand Down Expand Up @@ -135,7 +135,7 @@ export default defineComponent({
"a",
{
href: link,
rel: anchorRel.value,
rel: linkRel.value,
target: linkTarget.value,
"aria-label": linkAriaLabel.value,
...attrs,
Expand Down
@@ -1,9 +1,13 @@
import { isLinkExternal, isString } from "@vuepress/helper/client";
import { isString } from "@vuepress/helper/client";
import type { ComputedRefWithControl } from "@vueuse/core";
import { computedWithControl } from "@vueuse/core";

import { useThemeLocaleData } from "@theme-hope/composables/index";
import { resolveLinkInfo, resolvePrefix } from "@theme-hope/utils/index";
import {
isLinkInternal,
resolveLinkInfo,
resolvePrefix,
} from "@theme-hope/utils/index";

import type {
AutoLinkOptions,
Expand All @@ -22,7 +26,7 @@ export const resolveNavbarItem = (
if ("children" in item)
return {
...item,
...(item.link && !isLinkExternal(item.link)
...(item.link && isLinkInternal(item.link)
? resolveLinkInfo(resolvePrefix(prefix, item.link))
: {}),
children: item.children.map(
Expand All @@ -35,9 +39,9 @@ export const resolveNavbarItem = (

return {
...item,
link: isLinkExternal(item.link)
? item.link
: resolveLinkInfo(resolvePrefix(prefix, item.link)).link,
link: isLinkInternal(item.link)
? resolveLinkInfo(resolvePrefix(prefix, item.link)).link
: item.link,
};
};

Expand Down
@@ -1,7 +1,5 @@
/* eslint-disable @typescript-eslint/no-unsafe-argument */
import {
isArray,
isLinkExternal,
isPlainObject,
isString,
keys,
Expand All @@ -10,7 +8,11 @@ import {
import type { PageData } from "vuepress/client";

import { sidebarData } from "@temp/theme-hope/sidebar.js";
import { resolveLinkInfo, resolvePrefix } from "@theme-hope/utils/index";
import {
isLinkInternal,
resolveLinkInfo,
resolvePrefix,
} from "@theme-hope/utils/index";

import type {
SidebarArrayOptions,
Expand Down Expand Up @@ -47,12 +49,12 @@ export const resolveArraySidebarItems = ({
: item.link
? {
...item,
...(isLinkExternal(item.link)
? {}
: {
...(isLinkInternal(item.link)
? {
link: resolveLinkInfo(resolvePrefix(pathPrefix, item.link))
.link,
}),
}
: {}),
}
: item;

Expand Down
6 changes: 4 additions & 2 deletions packages/theme/src/client/utils/index.ts
@@ -1,2 +1,4 @@
export * from "./ancestorLinks.js";
export * from "./linkInfo.js";
export * from "./getAncestorLinks.js";
export * from "./isLinkInternal.js";
export * from "./resolveLinkInfo.js";
export * from "./resolvePrefix.js";
4 changes: 4 additions & 0 deletions packages/theme/src/client/utils/isLinkInternal.ts
@@ -0,0 +1,4 @@
import { isLinkExternal, isLinkWithProtocol } from "@vuepress/helper/client";

export const isLinkInternal = (link: string): boolean =>
!isLinkExternal(link) && !isLinkWithProtocol(link);
4 changes: 0 additions & 4 deletions packages/theme/src/client/utils/prefix.ts

This file was deleted.

@@ -1,15 +1,8 @@
import {
ensureEndingSlash,
isLinkAbsolute,
resolveRoute,
} from "@vuepress/helper/client";
import { resolveRoute } from "@vuepress/helper/client";

import type { AutoLinkOptions, PageInfoData } from "../../shared/index.js";
import { PageInfo } from "../../shared/index.js";

export const resolvePrefix = (prefix = "", path = ""): string =>
isLinkAbsolute(path) ? path : `${ensureEndingSlash(prefix)}${path}`;

/**
* Resolve AutoLink props from string
*
Expand Down
10 changes: 10 additions & 0 deletions packages/theme/src/client/utils/resolvePrefix.ts
@@ -0,0 +1,10 @@
import {
ensureEndingSlash,
isLinkAbsolute,
isLinkWithProtocol,
} from "@vuepress/helper/client";

export const resolvePrefix = (prefix = "", path = ""): string =>
isLinkAbsolute(path) || isLinkWithProtocol(path)
? path
: `${ensureEndingSlash(prefix)}${path}`;
2 changes: 1 addition & 1 deletion packages/theme/src/shims-sidebar-data.d.ts
@@ -1,5 +1,5 @@
declare module "@temp/theme-hope/sidebar.js" {
import type { SidebarGroupItem } from "vuepress-theme-hope/src/shared/index.js";
import type { SidebarGroupItem } from "vuepress-theme-hope";

// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
export const sidebarData: Record<string, (SidebarGroupItem | string)[]>;
Expand Down

0 comments on commit 04c840d

Please sign in to comment.