Skip to content

Commit

Permalink
fix(md-enhance): fix cjk character issue with link check, fix #2547
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Dec 13, 2022
1 parent cd47d6f commit 9d0f8d4
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 17 deletions.
@@ -0,0 +1,23 @@
# Links Check

- [a](./a.md)
- [b](./b.md)
- [中文](./中文.md)
- [Not exist](./not-exist.md)

- [a](a.md)
- [b](b.md)
- [中文](中文.md)
- [Not exist](not-exist.md)

## Sub Folder

- [a](./sub-folder/a.md)
- [b](./sub-folder/b.md)
- [中文](./sub-folder/中文.md)
- [Not exist](./sub-folder/not-exist.md)

- [a](sub-folder/a.md)
- [b](sub-folder/b.md)
- [中文](sub-folder/中文.md)
- [Not exist](sub-folder/not-exist.md)
Empty file.
Empty file.
@@ -0,0 +1,11 @@
# Links Check

- [a](./a.md)
- [b](./b.md)
- [中文](./中文.md)
- [Not exist](./not-exist.md)

- [a](a.md)
- [b](b.md)
- [中文](中文.md)
- [Not exist](not-exist.md)
Empty file.
Empty file.
Empty file.
Empty file.
@@ -0,0 +1,5 @@
import type { Theme } from "@vuepress/core";

export const emptyTheme: Theme = {
name: "vuepress-theme-empty",
};
31 changes: 16 additions & 15 deletions packages/md-enhance/__tests__/unit/include.spec.ts
Expand Up @@ -10,9 +10,9 @@ interface IncludeEnv extends MarkdownEnv {
includedFiles?: string[];
}

const mdFixturePathRelative = "./__fixtures__/include.md";
const mdFixturePathRelative = "./__fixtures__/src/include.md";
const mdFixturePath = path.resolve(__dirname, mdFixturePathRelative);
const mdFixtureDeepIncludeRelative = "./__fixtures__/deepInclude.md";
const mdFixtureDeepIncludeRelative = "./__fixtures__/src/deepInclude.md";
const mdFixtureDeepIncludePath = path.resolve(
__dirname,
mdFixtureDeepIncludeRelative
Expand Down Expand Up @@ -161,7 +161,7 @@ describe("include", () => {

it("should handle import path correctly", () => {
const source = `\
@include(@fixtures/include.md)
@include(@source/include.md)
`;
const expected = `\
<h2>Heading 2</h2>
Expand All @@ -175,8 +175,8 @@ describe("include", () => {
.use(include, {
getPath: (str: string): string =>
str.replace(
/^@fixtures/,
path.resolve(__dirname, "./__fixtures__")
/^@source/,
path.resolve(__dirname, "./__fixtures__/src")
),
})
.use(MarkdownContainer, "tip");
Expand Down Expand Up @@ -280,9 +280,10 @@ foo
});

describe("the relative path of link/image", () => {
const mdFixturePathRelative = "./__fixtures__/relative/includeLink.md";
const mdFixturePathRelative = "./__fixtures__/src/relative/includeLink.md";
const mdFixturePath = path.resolve(__dirname, mdFixturePathRelative);
const mdFixtureDeepIncludeRelative = "./__fixtures__/deepIncludeLink.md";
const mdFixtureDeepIncludeRelative =
"./__fixtures__/src/deepIncludeLink.md";
const mdFixtureDeepIncludePath = path.resolve(
__dirname,
mdFixtureDeepIncludeRelative
Expand All @@ -294,8 +295,8 @@ foo
`;

const expected = `\
<p><img src="./__fixtures__/relative/a.jpg" alt="Image1">
<a href="./__fixtures__/relative/a.md">Markdown</a></p>
<p><img src="./__fixtures__/src/relative/a.jpg" alt="Image1">
<a href="./__fixtures__/src/relative/a.md">Markdown</a></p>
`;

const env: IncludeEnv = {
Expand All @@ -312,7 +313,7 @@ foo
@include(${mdFixturePathRelative})
`;

const expected = `<p><img src="./__fixtures__/relative/a.jpg" alt="Image1">
const expected = `<p><img src="./__fixtures__/src/relative/a.jpg" alt="Image1">
<a href="./a.md">Markdown</a></p>
`;

Expand All @@ -334,7 +335,7 @@ foo
`;

const expected = `<p><img src="./a.jpg" alt="Image1">
<a href="./__fixtures__/relative/a.md">Markdown</a></p>
<a href="./__fixtures__/src/relative/a.md">Markdown</a></p>
`;

const env: IncludeEnv = {
Expand Down Expand Up @@ -378,8 +379,8 @@ foo

const expected = `\
<h3>Heading 3</h3>
<p><img src="./__fixtures__/relative/a.jpg" alt="Image1">
<a href="./__fixtures__/relative/a.md">Markdown</a></p>
<p><img src="./__fixtures__/src/relative/a.jpg" alt="Image1">
<a href="./__fixtures__/src/relative/a.md">Markdown</a></p>
`;

const env: IncludeEnv = {
Expand All @@ -402,8 +403,8 @@ foo
`;

const expected = `\
<p><img src="./__fixtures__/relative/a.jpg" alt="Image1">
<a href="./__fixtures__/relative/a.md">Markdown</a></p>
<p><img src="./__fixtures__/src/relative/a.jpg" alt="Image1">
<a href="./__fixtures__/src/relative/a.md">Markdown</a></p>
<p><a href="./b.md">B</a></p>
`;

Expand Down
18 changes: 18 additions & 0 deletions packages/md-enhance/__tests__/unit/linkCheck.spec.ts
@@ -0,0 +1,18 @@
import { createBaseApp } from "@vuepress/core";
import { path } from "@vuepress/utils";
import { it } from "vitest";
import { checkLinks } from "../../src/node/checkLink.js";

import { emptyTheme } from "./__fixtures__/theme/empty.js";

it("should check links correctly", async () => {
const app = createBaseApp({
bundler: {} as any,
source: path.resolve(__dirname, "./__fixtures__/src"),
theme: emptyTheme,
});

await app.init();

app.pages.forEach((page) => checkLinks(page, app));
});
6 changes: 4 additions & 2 deletions packages/md-enhance/src/node/checkLink.ts
Expand Up @@ -34,7 +34,9 @@ export const checkLinks = (page: Page, app: App): void => {
.filter(({ raw }) => !raw.startsWith("/"))
.filter(({ relative }) =>
// check whether the page exists
pages.every(({ filePathRelative }) => filePathRelative !== relative)
pages.every(
({ filePathRelative }) => filePathRelative !== decodeURI(relative)
)
),
...markdownLinks
// absolute markdown links
Expand All @@ -44,7 +46,7 @@ export const checkLinks = (page: Page, app: App): void => {
pages.every(
({ filePathRelative }) =>
!filePathRelative ||
`${app.options.base}${filePathRelative}` !== absolute
`${app.options.base}${filePathRelative}` !== decodeURI(absolute)
)
),
].map(({ raw }) => raw);
Expand Down

0 comments on commit 9d0f8d4

Please sign in to comment.