Skip to content

Commit

Permalink
Merge pull request #101 from easyops-cn/steve/fix-trailing-slash
Browse files Browse the repository at this point in the history
Steve/fix trailing slash
  • Loading branch information
weareoutman authored Aug 3, 2021
2 parents 5c51edc + 16aa10f commit 3476a28
Show file tree
Hide file tree
Showing 7 changed files with 456 additions and 286 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
},
"license": "MIT",
"dependencies": {
"@docusaurus/utils": "^2.0.0-beta.0",
"@docusaurus/utils-validation": "^2.0.0-beta.0",
"@docusaurus/utils": "^2.0.0-beta.4",
"@docusaurus/utils-validation": "^2.0.0-beta.4",
"@easyops-cn/autocomplete.js": "^0.38.1",
"cheerio": "^1.0.0-rc.3",
"clsx": "^1.1.1",
Expand All @@ -44,7 +44,7 @@
"@babel/preset-env": "^7.12.1",
"@babel/preset-react": "^7.12.1",
"@babel/preset-typescript": "^7.12.1",
"@docusaurus/module-type-aliases": "^2.0.0-beta.0",
"@docusaurus/module-type-aliases": "^2.0.0-beta.4",
"@tsconfig/docusaurus": "^1.0.2",
"@types/debug": "^4.1.5",
"@types/enzyme": "^3.10.7",
Expand Down
2 changes: 1 addition & 1 deletion src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function DocusaurusSearchLocalPlugin(
return themePath;
},

postBuild: postBuildFactory(config),
postBuild: postBuildFactory(config, context.siteConfig),

getPathsToWatch() {
return [pagePath];
Expand Down
8 changes: 6 additions & 2 deletions src/server/utils/postBuildFactory.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DocusaurusConfig } from "@docusaurus/types";
import fs from "fs";
import path from "path";
import util from "util";
Expand All @@ -9,11 +10,14 @@ import { scanDocuments } from "./scanDocuments";

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

export function postBuildFactory(config: ProcessedPluginOptions) {
export function postBuildFactory(
config: ProcessedPluginOptions,
siteConfig: DocusaurusConfig
) {
return async function postBuild(buildData: PostBuildData): Promise<void> {
debugInfo("gathering documents");

const data = processDocInfos(buildData, config);
const data = processDocInfos(buildData, config, siteConfig);

debugInfo("parsing documents");

Expand Down
240 changes: 188 additions & 52 deletions src/server/utils/processDocInfos.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DocusaurusConfig } from "@docusaurus/types";
import {
DocInfoWithFilePath,
PostBuildData,
Expand All @@ -6,67 +7,202 @@ import {
import { processDocInfos } from "./processDocInfos";

describe("processDocInfos", () => {
const routesPaths: string[] = [
"/base/",
"/base/docs/a",
"/base/blog",
"/base/blog/tags",
"/base/blog/b",
"/base/404.html",
"/base/page",
"/base/__meta__.md",
"/base/file.md",
];
const buildData: PostBuildData = {
routesPaths,
outDir: "/build",
baseUrl: "/base/",
};
test.each<[Partial<ProcessedPluginOptions>, DocInfoWithFilePath[]]>([
[
{
indexDocs: false,
indexBlog: false,
indexPages: false,
ignoreFiles: [],
},
[],
],
[
{
indexDocs: true,
indexBlog: true,
indexPages: true,
docsRouteBasePath: ["docs"],
blogRouteBasePath: ["blog"],
ignoreFiles: [/^__meta__/, "file.md"],
},
describe("trailingSlash defaults to undefined", () => {
const routesPaths: string[] = [
"/base/",
"/base/docs/a",
"/base/blog",
"/base/blog/tags",
"/base/blog/b",
"/base/404.html",
"/base/page",
"/base/__meta__.md",
"/base/file.md",
];
const buildData: PostBuildData = {
routesPaths,
outDir: "/build",
baseUrl: "/base/",
};
test.each<[Partial<ProcessedPluginOptions>, DocInfoWithFilePath[]]>([
[
{
filePath: "/build/index.html",
type: "page",
url: "/base/",
indexDocs: false,
indexBlog: false,
indexPages: false,
ignoreFiles: [],
},
[],
],
[
{
indexDocs: true,
indexBlog: true,
indexPages: true,
docsRouteBasePath: ["docs"],
blogRouteBasePath: ["blog"],
ignoreFiles: [/^__meta__/, "file.md"],
},
[
{
filePath: "/build/docs/a/index.html",
type: "docs",
url: "/base/docs/a",
},
{
filePath: "/build/blog/b/index.html",
type: "blog",
url: "/base/blog/b",
},
{
filePath: "/build/page/index.html",
type: "page",
url: "/base/page",
},
],
],
])("processDocInfos(...) should work", (config, result) => {
expect(
processDocInfos(
buildData,
config as ProcessedPluginOptions,
{} as DocusaurusConfig
)
).toEqual(result);
});
});

describe("trailingSlash set to false", () => {
const routesPaths: string[] = [
"/base/",
"/base/docs/a",
"/base/blog",
"/base/blog/tags",
"/base/blog/b",
"/base/404.html",
"/base/page",
"/base/__meta__.md",
"/base/file.md",
];
const buildData: PostBuildData = {
routesPaths,
outDir: "/build",
baseUrl: "/base/",
};
test.each<[Partial<ProcessedPluginOptions>, DocInfoWithFilePath[]]>([
[
{
indexDocs: false,
indexBlog: false,
indexPages: false,
ignoreFiles: [],
},
[],
],
[
{
filePath: "/build/docs/a/index.html",
type: "docs",
url: "/base/docs/a",
indexDocs: true,
indexBlog: true,
indexPages: true,
docsRouteBasePath: ["docs"],
blogRouteBasePath: ["blog"],
ignoreFiles: [/^__meta__/, "file.md"],
},
[
{
filePath: "/build/docs/a.html",
type: "docs",
url: "/base/docs/a",
},
{
filePath: "/build/blog/b.html",
type: "blog",
url: "/base/blog/b",
},
{
filePath: "/build/page.html",
type: "page",
url: "/base/page",
},
],
],
])("processDocInfos(...) should work", (config, result) => {
expect(
processDocInfos(
buildData,
config as ProcessedPluginOptions,
{
trailingSlash: false,
} as DocusaurusConfig
)
).toEqual(result);
});
});

describe("trailingSlash set to true", () => {
const routesPaths: string[] = [
"/base/",
"/base/docs/a/",
"/base/blog/",
"/base/blog/tags/",
"/base/blog/b/",
"/base/404.html",
"/base/page/",
"/base/__meta__.md",
"/base/file.md",
];
const buildData: PostBuildData = {
routesPaths,
outDir: "/build",
baseUrl: "/base/",
};
test.each<[Partial<ProcessedPluginOptions>, DocInfoWithFilePath[]]>([
[
{
filePath: "/build/blog/b/index.html",
type: "blog",
url: "/base/blog/b",
indexDocs: false,
indexBlog: false,
indexPages: false,
ignoreFiles: [],
},
[],
],
[
{
filePath: "/build/page/index.html",
type: "page",
url: "/base/page",
indexDocs: true,
indexBlog: true,
indexPages: true,
docsRouteBasePath: ["docs"],
blogRouteBasePath: ["blog"],
ignoreFiles: [/^__meta__/, "file.md"],
},
[
{
filePath: "/build/docs/a/index.html",
type: "docs",
url: "/base/docs/a/",
},
{
filePath: "/build/blog/b/index.html",
type: "blog",
url: "/base/blog/b/",
},
{
filePath: "/build/page/index.html",
type: "page",
url: "/base/page/",
},
],
],
],
])("processDocInfos(...) should work", (config, result) => {
expect(
processDocInfos(buildData, config as ProcessedPluginOptions)
).toEqual(result);
])("processDocInfos(...) should work", (config, result) => {
expect(
processDocInfos(
buildData,
config as ProcessedPluginOptions,
{
trailingSlash: true,
} as DocusaurusConfig
)
).toEqual(result);
});
});
});
Loading

0 comments on commit 3476a28

Please sign in to comment.