Skip to content

Commit 97cd0da

Browse files
authored
refactor: simplify TOC file extraction logic (#658)
- Removed unused extendsFolders parameter from extractFilesFromToc function, streamlining the file extraction process. - Updated related function calls to reflect the removal of the extendsFolders parameter. - Adjusted logic in generateConfig to ensure prefixes are only applied to the index page when appropriate, enhancing clarity and maintainability.
1 parent 49bfc40 commit 97cd0da

File tree

3 files changed

+10
-73
lines changed

3 files changed

+10
-73
lines changed

gatsby/path.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export function generateNav(config: PathConfig) {
3131
return `${config.locale}/${config.repo}/${config.branch}/TOC-tidb-cloud-premium`;
3232
}
3333

34+
const MAIN_PLAN = "premium";
35+
3436
export function generateConfig(slug: string): {
3537
config: PathConfig;
3638
filePath: string;
@@ -52,29 +54,12 @@ export function generateConfig(slug: string): {
5254
name = name === "_index" ? "" : name;
5355
let prefix: CloudPlan | undefined = undefined;
5456

55-
// if (repo === Repo.tidbcloud) {
56-
// const simplePrefixes = ["starter", "essential", "premium"];
57-
// prefix = simplePrefixes.find((p) => slug.includes(`${p}/`)) as
58-
// | CloudPlan
59-
// | undefined;
60-
61-
// // dedicated prefix is only used when the name is not empty
62-
// if (!prefix && slug.includes("dedicated/") && name) {
63-
// prefix = "dedicated";
64-
// }
65-
// }
66-
67-
// for premium-preview branch
68-
69-
if (repo === Repo.tidbcloud) {
57+
// only index page should have a prefix, e.g. /tidbcloud/starter/
58+
if (repo === Repo.tidbcloud && !name && !slug.includes(`${MAIN_PLAN}/`)) {
7059
const simplePrefixes = ["starter", "essential", "dedicated"];
7160
prefix = simplePrefixes.find((p) => slug.includes(`${p}/`)) as
7261
| CloudPlan
7362
| undefined;
74-
75-
if (!prefix && slug.includes("premium/") && name) {
76-
prefix = "premium";
77-
}
7863
}
7964

8065
return {

gatsby/toc-filter.ts

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EXTENDS_FOLDERS, mdxAstToToc, TocQueryData } from "./toc";
1+
import { mdxAstToToc, TocQueryData } from "./toc";
22
import { generateConfig } from "./path";
33

44
// Whitelist of files that should always be built regardless of TOC content
@@ -7,10 +7,7 @@ const WHITELIST = [""];
77
/**
88
* Extract file paths from TOC navigation structure
99
*/
10-
export function extractFilesFromToc(
11-
nav: any[],
12-
extendsFolders?: string[]
13-
): string[] {
10+
export function extractFilesFromToc(nav: any[]): string[] {
1411
const files: string[] = [];
1512

1613
function traverse(navItems: any[]) {
@@ -26,31 +23,7 @@ export function extractFilesFromToc(
2623
if (filenameWithExt && filenameWithExt !== "") {
2724
// Remove .md extension to match the actual file name
2825
const filename = filenameWithExt.replace(/\.md$/, "");
29-
30-
// Check if extends folders are specified and found in path segments
31-
if (extendsFolders && extendsFolders.length > 0) {
32-
let foundExtendsFolder = false;
33-
for (const extendsFolder of extendsFolders) {
34-
const extendsIndex = pathSegments.indexOf(extendsFolder);
35-
if (
36-
extendsIndex !== -1 &&
37-
extendsIndex < pathSegments.length - 1
38-
) {
39-
// Keep the extends folder and everything after it (excluding the .md extension)
40-
const pathFromExtends = pathSegments
41-
.slice(extendsIndex, -1)
42-
.join("/");
43-
files.push(`${pathFromExtends}/${filename}`);
44-
foundExtendsFolder = true;
45-
break;
46-
}
47-
}
48-
if (!foundExtendsFolder) {
49-
files.push(filename);
50-
}
51-
} else {
52-
files.push(filename);
53-
}
26+
files.push(filename);
5427
}
5528
}
5629
if (item.children) {
@@ -98,7 +71,7 @@ export async function getFilesFromTocs(
9871
tocNodes.forEach((node: TocQueryData["allMdx"]["nodes"][0]) => {
9972
const { config } = generateConfig(node.slug);
10073
const toc = mdxAstToToc(node.mdxAST.children, config);
101-
const files = extractFilesFromToc(toc, EXTENDS_FOLDERS);
74+
const files = extractFilesFromToc(toc);
10275

10376
// Create a key for this specific locale/repo/version combination
10477
const key = `${config.locale}/${config.repo}/${
@@ -157,9 +130,7 @@ export function filterNodesByToc(
157130
}
158131

159132
// Only build files that are referenced in the corresponding TOC
160-
const fullPath = `${
161-
node.pathConfig.prefix ? node.pathConfig.prefix + "/" : ""
162-
}${node.name}`;
133+
const fullPath = node.name;
163134

164135
// Check if the file is directly referenced in TOC
165136
let isIncluded = filesForThisToc.has(fullPath);

gatsby/toc.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,9 @@ import {
99
Heading,
1010
} from "mdast";
1111

12-
import {
13-
RepoNav,
14-
RepoNavLink,
15-
PathConfig,
16-
CloudPlan,
17-
} from "../src/shared/interface";
12+
import { RepoNav, RepoNavLink, PathConfig } from "../src/shared/interface";
1813
import { generateUrl } from "./path";
1914

20-
export const EXTENDS_FOLDERS: CloudPlan[] = [
21-
"starter",
22-
"essential",
23-
"dedicated",
24-
"premium",
25-
];
26-
2715
const SKIP_MODE_HEADING = "_BUILD_ALLOWLIST";
2816

2917
function filterWhitelistContent(ast: Content[]): Content[] {
@@ -210,13 +198,6 @@ function getContentFromLink(
210198
const urlSegs = child.url.split("/");
211199
let filename = urlSegs[urlSegs.length - 1].replace(".md", "");
212200

213-
for (const extendsFolder of EXTENDS_FOLDERS) {
214-
if (urlSegs.includes(extendsFolder)) {
215-
filename = `${extendsFolder}/${filename}`;
216-
break;
217-
}
218-
}
219-
220201
return {
221202
type: "nav",
222203
link: generateUrl(filename, tocConfig),

0 commit comments

Comments
 (0)