Skip to content

Commit 3476a28

Browse files
authored
Merge pull request #101 from easyops-cn/steve/fix-trailing-slash
Steve/fix trailing slash
2 parents 5c51edc + 16aa10f commit 3476a28

File tree

7 files changed

+456
-286
lines changed

7 files changed

+456
-286
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
},
2727
"license": "MIT",
2828
"dependencies": {
29-
"@docusaurus/utils": "^2.0.0-beta.0",
30-
"@docusaurus/utils-validation": "^2.0.0-beta.0",
29+
"@docusaurus/utils": "^2.0.0-beta.4",
30+
"@docusaurus/utils-validation": "^2.0.0-beta.4",
3131
"@easyops-cn/autocomplete.js": "^0.38.1",
3232
"cheerio": "^1.0.0-rc.3",
3333
"clsx": "^1.1.1",
@@ -44,7 +44,7 @@
4444
"@babel/preset-env": "^7.12.1",
4545
"@babel/preset-react": "^7.12.1",
4646
"@babel/preset-typescript": "^7.12.1",
47-
"@docusaurus/module-type-aliases": "^2.0.0-beta.0",
47+
"@docusaurus/module-type-aliases": "^2.0.0-beta.4",
4848
"@tsconfig/docusaurus": "^1.0.2",
4949
"@types/debug": "^4.1.5",
5050
"@types/enzyme": "^3.10.7",

src/server/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default function DocusaurusSearchLocalPlugin(
2828
return themePath;
2929
},
3030

31-
postBuild: postBuildFactory(config),
31+
postBuild: postBuildFactory(config, context.siteConfig),
3232

3333
getPathsToWatch() {
3434
return [pagePath];

src/server/utils/postBuildFactory.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { DocusaurusConfig } from "@docusaurus/types";
12
import fs from "fs";
23
import path from "path";
34
import util from "util";
@@ -9,11 +10,14 @@ import { scanDocuments } from "./scanDocuments";
910

1011
const writeFileAsync = util.promisify(fs.writeFile);
1112

12-
export function postBuildFactory(config: ProcessedPluginOptions) {
13+
export function postBuildFactory(
14+
config: ProcessedPluginOptions,
15+
siteConfig: DocusaurusConfig
16+
) {
1317
return async function postBuild(buildData: PostBuildData): Promise<void> {
1418
debugInfo("gathering documents");
1519

16-
const data = processDocInfos(buildData, config);
20+
const data = processDocInfos(buildData, config, siteConfig);
1721

1822
debugInfo("parsing documents");
1923

Lines changed: 188 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { DocusaurusConfig } from "@docusaurus/types";
12
import {
23
DocInfoWithFilePath,
34
PostBuildData,
@@ -6,67 +7,202 @@ import {
67
import { processDocInfos } from "./processDocInfos";
78

89
describe("processDocInfos", () => {
9-
const routesPaths: string[] = [
10-
"/base/",
11-
"/base/docs/a",
12-
"/base/blog",
13-
"/base/blog/tags",
14-
"/base/blog/b",
15-
"/base/404.html",
16-
"/base/page",
17-
"/base/__meta__.md",
18-
"/base/file.md",
19-
];
20-
const buildData: PostBuildData = {
21-
routesPaths,
22-
outDir: "/build",
23-
baseUrl: "/base/",
24-
};
25-
test.each<[Partial<ProcessedPluginOptions>, DocInfoWithFilePath[]]>([
26-
[
27-
{
28-
indexDocs: false,
29-
indexBlog: false,
30-
indexPages: false,
31-
ignoreFiles: [],
32-
},
33-
[],
34-
],
35-
[
36-
{
37-
indexDocs: true,
38-
indexBlog: true,
39-
indexPages: true,
40-
docsRouteBasePath: ["docs"],
41-
blogRouteBasePath: ["blog"],
42-
ignoreFiles: [/^__meta__/, "file.md"],
43-
},
10+
describe("trailingSlash defaults to undefined", () => {
11+
const routesPaths: string[] = [
12+
"/base/",
13+
"/base/docs/a",
14+
"/base/blog",
15+
"/base/blog/tags",
16+
"/base/blog/b",
17+
"/base/404.html",
18+
"/base/page",
19+
"/base/__meta__.md",
20+
"/base/file.md",
21+
];
22+
const buildData: PostBuildData = {
23+
routesPaths,
24+
outDir: "/build",
25+
baseUrl: "/base/",
26+
};
27+
test.each<[Partial<ProcessedPluginOptions>, DocInfoWithFilePath[]]>([
4428
[
4529
{
46-
filePath: "/build/index.html",
47-
type: "page",
48-
url: "/base/",
30+
indexDocs: false,
31+
indexBlog: false,
32+
indexPages: false,
33+
ignoreFiles: [],
4934
},
35+
[],
36+
],
37+
[
38+
{
39+
indexDocs: true,
40+
indexBlog: true,
41+
indexPages: true,
42+
docsRouteBasePath: ["docs"],
43+
blogRouteBasePath: ["blog"],
44+
ignoreFiles: [/^__meta__/, "file.md"],
45+
},
46+
[
47+
{
48+
filePath: "/build/docs/a/index.html",
49+
type: "docs",
50+
url: "/base/docs/a",
51+
},
52+
{
53+
filePath: "/build/blog/b/index.html",
54+
type: "blog",
55+
url: "/base/blog/b",
56+
},
57+
{
58+
filePath: "/build/page/index.html",
59+
type: "page",
60+
url: "/base/page",
61+
},
62+
],
63+
],
64+
])("processDocInfos(...) should work", (config, result) => {
65+
expect(
66+
processDocInfos(
67+
buildData,
68+
config as ProcessedPluginOptions,
69+
{} as DocusaurusConfig
70+
)
71+
).toEqual(result);
72+
});
73+
});
74+
75+
describe("trailingSlash set to false", () => {
76+
const routesPaths: string[] = [
77+
"/base/",
78+
"/base/docs/a",
79+
"/base/blog",
80+
"/base/blog/tags",
81+
"/base/blog/b",
82+
"/base/404.html",
83+
"/base/page",
84+
"/base/__meta__.md",
85+
"/base/file.md",
86+
];
87+
const buildData: PostBuildData = {
88+
routesPaths,
89+
outDir: "/build",
90+
baseUrl: "/base/",
91+
};
92+
test.each<[Partial<ProcessedPluginOptions>, DocInfoWithFilePath[]]>([
93+
[
94+
{
95+
indexDocs: false,
96+
indexBlog: false,
97+
indexPages: false,
98+
ignoreFiles: [],
99+
},
100+
[],
101+
],
102+
[
50103
{
51-
filePath: "/build/docs/a/index.html",
52-
type: "docs",
53-
url: "/base/docs/a",
104+
indexDocs: true,
105+
indexBlog: true,
106+
indexPages: true,
107+
docsRouteBasePath: ["docs"],
108+
blogRouteBasePath: ["blog"],
109+
ignoreFiles: [/^__meta__/, "file.md"],
54110
},
111+
[
112+
{
113+
filePath: "/build/docs/a.html",
114+
type: "docs",
115+
url: "/base/docs/a",
116+
},
117+
{
118+
filePath: "/build/blog/b.html",
119+
type: "blog",
120+
url: "/base/blog/b",
121+
},
122+
{
123+
filePath: "/build/page.html",
124+
type: "page",
125+
url: "/base/page",
126+
},
127+
],
128+
],
129+
])("processDocInfos(...) should work", (config, result) => {
130+
expect(
131+
processDocInfos(
132+
buildData,
133+
config as ProcessedPluginOptions,
134+
{
135+
trailingSlash: false,
136+
} as DocusaurusConfig
137+
)
138+
).toEqual(result);
139+
});
140+
});
141+
142+
describe("trailingSlash set to true", () => {
143+
const routesPaths: string[] = [
144+
"/base/",
145+
"/base/docs/a/",
146+
"/base/blog/",
147+
"/base/blog/tags/",
148+
"/base/blog/b/",
149+
"/base/404.html",
150+
"/base/page/",
151+
"/base/__meta__.md",
152+
"/base/file.md",
153+
];
154+
const buildData: PostBuildData = {
155+
routesPaths,
156+
outDir: "/build",
157+
baseUrl: "/base/",
158+
};
159+
test.each<[Partial<ProcessedPluginOptions>, DocInfoWithFilePath[]]>([
160+
[
55161
{
56-
filePath: "/build/blog/b/index.html",
57-
type: "blog",
58-
url: "/base/blog/b",
162+
indexDocs: false,
163+
indexBlog: false,
164+
indexPages: false,
165+
ignoreFiles: [],
59166
},
167+
[],
168+
],
169+
[
60170
{
61-
filePath: "/build/page/index.html",
62-
type: "page",
63-
url: "/base/page",
171+
indexDocs: true,
172+
indexBlog: true,
173+
indexPages: true,
174+
docsRouteBasePath: ["docs"],
175+
blogRouteBasePath: ["blog"],
176+
ignoreFiles: [/^__meta__/, "file.md"],
64177
},
178+
[
179+
{
180+
filePath: "/build/docs/a/index.html",
181+
type: "docs",
182+
url: "/base/docs/a/",
183+
},
184+
{
185+
filePath: "/build/blog/b/index.html",
186+
type: "blog",
187+
url: "/base/blog/b/",
188+
},
189+
{
190+
filePath: "/build/page/index.html",
191+
type: "page",
192+
url: "/base/page/",
193+
},
194+
],
65195
],
66-
],
67-
])("processDocInfos(...) should work", (config, result) => {
68-
expect(
69-
processDocInfos(buildData, config as ProcessedPluginOptions)
70-
).toEqual(result);
196+
])("processDocInfos(...) should work", (config, result) => {
197+
expect(
198+
processDocInfos(
199+
buildData,
200+
config as ProcessedPluginOptions,
201+
{
202+
trailingSlash: true,
203+
} as DocusaurusConfig
204+
)
205+
).toEqual(result);
206+
});
71207
});
72208
});

0 commit comments

Comments
 (0)