Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions gatsby/__tests__/cloud-plan.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
jest.mock("../toc", () => ({}));
jest.mock("../path", () => ({}));
jest.mock("../toc-filter", () => ({}));
jest.mock("../url-resolver", () => ({}));

const { determineInDefaultPlan } = require("../cloud-plan");
const { CloudPlan, Repo, Locale } = require("../../src/shared/interface");

const pathConfig = {
locale: Locale.en,
repo: Repo.tidbcloud,
branch: "master",
version: null,
};

describe("cloud plan detection", () => {
it("detects BYOC-only TiDB Cloud articles", () => {
const tocMap = new Map([
[
"en/tidbcloud/master",
{
dedicated: new Set(),
starter: new Set(),
essential: new Set(),
premium: new Set(),
byoc: new Set(["byoc-overview"]),
},
],
]);

expect(determineInDefaultPlan("byoc-overview", pathConfig, tocMap)).toBe(
CloudPlan.Byoc
);
});

it("keeps dedicated as the default when a BYOC page is also in the dedicated TOC", () => {
const tocMap = new Map([
[
"en/tidbcloud/master",
{
dedicated: new Set(["shared-overview"]),
starter: new Set(),
essential: new Set(),
premium: new Set(),
byoc: new Set(["shared-overview"]),
},
],
]);

expect(determineInDefaultPlan("shared-overview", pathConfig, tocMap)).toBe(
CloudPlan.Dedicated
);
});
});
11 changes: 11 additions & 0 deletions gatsby/__tests__/toc-filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ describe("toc-filter ignore rules", () => {
).toBe(true);
});

it("does not ignore the BYOC TiDB Cloud TOC", () => {
jest.resetModules();
const { isIgnoredTocRelativePath } = require("../toc-ignore");

expect(
isIgnoredTocRelativePath(
"markdown-pages/en/tidbcloud/master/TOC-tidb-cloud-byoc.md"
)
).toBe(false);
});

it("supports env-based ignore substrings", () => {
process.env.WEBSITE_IGNORED_TOC_RELATIVE_PATH_SUBSTRINGS =
"custom/TOC-ignore-me.md, TOC-some-other.md";
Expand Down
18 changes: 16 additions & 2 deletions gatsby/cloud-plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ type TocMap = Map<
starter: Set<string>;
essential: Set<string>;
premium: Set<string>;
byoc: Set<string>;
}
>;

/**
* Get files from different TOC types for tidbcloud
* Returns a Map where key is "locale/repo/version" and value is object with dedicated, starter, essential file sets
* Returns a Map where key is "locale/repo/version" and value is object with dedicated, starter, essential, premium, and byoc file sets
*/
export async function getTidbCloudFilesFromTocs(graphql: any): Promise<TocMap> {
const tocQuery = await graphql(`
Expand Down Expand Up @@ -68,6 +69,8 @@ export async function getTidbCloudFilesFromTocs(graphql: any): Promise<TocMap> {
tocType = CloudPlan.Essential;
} else if (relativePath.includes("TOC-tidb-cloud-premium")) {
tocType = CloudPlan.Premium;
} else if (relativePath.includes("TOC-tidb-cloud-byoc")) {
tocType = CloudPlan.Byoc;
}

// Initialize the entry if it doesn't exist
Expand All @@ -77,6 +80,7 @@ export async function getTidbCloudFilesFromTocs(graphql: any): Promise<TocMap> {
starter: new Set(),
essential: new Set(),
premium: new Set(),
byoc: new Set(),
});
}

Expand Down Expand Up @@ -116,7 +120,7 @@ export function determineInDefaultPlan(
return null;
}

const { dedicated, starter, essential, premium } = tocData;
const { dedicated, starter, essential, premium, byoc } = tocData;

// Check if article is in TOC.md (dedicated)
if (dedicated.has(fileName)) {
Expand Down Expand Up @@ -146,5 +150,15 @@ export function determineInDefaultPlan(
return CloudPlan.Premium;
}

if (
byoc.has(fileName) &&
!premium.has(fileName) &&
!essential.has(fileName) &&
!dedicated.has(fileName) &&
!starter.has(fileName)
) {
return CloudPlan.Byoc;
}
Comment on lines +153 to +161

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since this is the final check in a sequence of if-else style conditions, we already know that fileName is not present in dedicated, starter, essential, or premium (otherwise, the function would have already returned). Therefore, we can simplify this condition to just check byoc.has(fileName).

  if (byoc.has(fileName)) {
    return CloudPlan.Byoc;
  }


return null;
}
2 changes: 2 additions & 0 deletions gatsby/create-pages/create-doc-home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export const createDocHome = async ({
pathConfig,
"tidb-cloud-essential"
);
const byocNavUrl = generateNavTOCPath(pathConfig, "tidb-cloud-byoc");
const locale =
process.env.WEBSITE_BUILD_TYPE === "archive"
? [Locale.en, Locale.zh]
Expand All @@ -114,6 +115,7 @@ export const createDocHome = async ({
navUrl,
starterNavUrl,
essentialNavUrl,
byocNavUrl,
pageUrl: path,
availIn: {
locale,
Expand Down
2 changes: 2 additions & 0 deletions gatsby/create-pages/create-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export const createDocs = async (createPagesArgs: CreatePagesArgs) => {
pathConfig,
"tidb-cloud-essential"
);
const byocNavUrl = generateNavTOCPath(pathConfig, "tidb-cloud-byoc");

const locale = [Locale.en, Locale.zh, Locale.ja]
.map((l) =>
Expand Down Expand Up @@ -162,6 +163,7 @@ export const createDocs = async (createPagesArgs: CreatePagesArgs) => {
navUrl,
starterNavUrl,
essentialNavUrl,
byocNavUrl,
availIn: {
locale,
version: versionRecord[pathConfig.locale][pathConfig.repo][name],
Expand Down
117 changes: 48 additions & 69 deletions gatsby/create-types/create-navs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,39 @@ import { mdxAstToToc } from "../toc";
import { Root } from "mdast";
import { calculateFileUrl } from "../url-resolver";

function createCloudPlanNavigationField(cacheKey: string, tocSuffix: string) {
return {
async resolve(mdxNode: any, args: unknown, context: unknown, info: any) {
if (mdxNode[cacheKey]) return mdxNode[cacheKey];
const types = info.schema.getType("Mdx").getFields();
const slug = await types["slug"].resolve(mdxNode, args, context, {
fieldName: "slug",
});

const mdxAST: Root = await types["mdxAST"].resolve(
mdxNode,
args,
context,
{
fieldName: "mdxAST",
}
);

if (!slug.endsWith(tocSuffix))
throw new Error(`unsupported query in ${slug}`);
const tocPath = calculateFileUrl(slug);
const res = mdxAstToToc(
mdxAST.children,
tocPath || slug,
undefined,
true
);
mdxNode[cacheKey] = res;
return res;
},
};
}

export const createNavs = ({ actions }: CreatePagesArgs) => {
const { createTypes, createFieldExtension } = actions;

Expand Down Expand Up @@ -48,82 +81,27 @@ export const createNavs = ({ actions }: CreatePagesArgs) => {
createFieldExtension({
name: "starterNavigation",
extend() {
return {
async resolve(
mdxNode: any,
args: unknown,
context: unknown,
info: any
) {
if (mdxNode.starterNav) return mdxNode.starterNav;
const types = info.schema.getType("Mdx").getFields();
const slug = await types["slug"].resolve(mdxNode, args, context, {
fieldName: "slug",
});

const mdxAST: Root = await types["mdxAST"].resolve(
mdxNode,
args,
context,
{
fieldName: "mdxAST",
}
);

if (!slug.endsWith("TOC-tidb-cloud-starter"))
throw new Error(`unsupported query in ${slug}`);
const tocPath = calculateFileUrl(slug);
const res = mdxAstToToc(
mdxAST.children,
tocPath || slug,
undefined,
true
);
mdxNode.starterNav = res;
return res;
},
};
return createCloudPlanNavigationField(
"starterNav",
"TOC-tidb-cloud-starter"
);
},
});

createFieldExtension({
name: "essentialNavigation",
extend() {
return {
async resolve(
mdxNode: any,
args: unknown,
context: unknown,
info: any
) {
if (mdxNode.essentialNav) return mdxNode.essentialNav;
const types = info.schema.getType("Mdx").getFields();
const slug = await types["slug"].resolve(mdxNode, args, context, {
fieldName: "slug",
});

const mdxAST: Root = await types["mdxAST"].resolve(
mdxNode,
args,
context,
{
fieldName: "mdxAST",
}
);
return createCloudPlanNavigationField(
"essentialNav",
"TOC-tidb-cloud-essential"
);
},
});

if (!slug.endsWith("TOC-tidb-cloud-essential"))
throw new Error(`unsupported query in ${slug}`);
const tocPath = calculateFileUrl(slug);
const res = mdxAstToToc(
mdxAST.children,
tocPath || slug,
undefined,
true
);
mdxNode.essentialNav = res;
return res;
},
};
createFieldExtension({
name: "byocNavigation",
extend() {
return createCloudPlanNavigationField("byocNav", "TOC-tidb-cloud-byoc");
},
});

Expand All @@ -132,6 +110,7 @@ export const createNavs = ({ actions }: CreatePagesArgs) => {
navigation: JSON! @navigation
starterNavigation: JSON! @starterNavigation
essentialNavigation: JSON! @essentialNavigation
byocNavigation: JSON! @byocNavigation
}
`);
};
1 change: 1 addition & 0 deletions gatsby/path/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export function generateConfig(slug: string): {
CloudPlan.Starter,
CloudPlan.Essential,
CloudPlan.Premium,
CloudPlan.Byoc,
] as const;
prefix = simplePrefixes.find((p) => slug.includes(`${p}/`));
}
Expand Down
1 change: 1 addition & 0 deletions gatsby/toc-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function sortTocNames(tocNames: Iterable<string>): string[] {
"TOC-tidb-cloud-starter",
"TOC-tidb-cloud-essential",
"TOC-tidb-cloud-premium",
"TOC-tidb-cloud-byoc",
];
const priorityIndex = new Map(priority.map((name, idx) => [name, idx]));
return [...new Set(tocNames)].sort((a, b) => {
Expand Down
1 change: 0 additions & 1 deletion gatsby/toc-ignore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@ export function isIgnoredTocRelativePath(relativePath: string): boolean {
(substr) => substr && relativePath.includes(substr)
);
}

1 change: 1 addition & 0 deletions locale/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"tidbCloudEssential": "TiDB Cloud Essential",
"tidbCloudDedicated": "TiDB Cloud Dedicated",
"tidbCloudLake": "TiDB Cloud Lake",
"tidbCloudByoc": "TiDB Cloud BYOC",
"tidbShortTerm": "TiDB",
"tidbOnKubernetes": "TiDB on Kubernetes",
"tidbCloudReleases": "TiDB Cloud Releases",
Expand Down
1 change: 1 addition & 0 deletions locale/ja/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"tidbCloudEssential": "TiDB Cloud Essential",
"tidbCloudDedicated": "TiDB Cloud Dedicated",
"tidbCloudLake": "TiDB Cloud Lake",
"tidbCloudByoc": "TiDB Cloud BYOC",
"tidbShortTerm": "TiDB",
"tidbOnKubernetes": "TiDB on Kubernetes",
"tidbCloudReleases": "TiDB Cloud リリース",
Expand Down
1 change: 1 addition & 0 deletions locale/zh/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"tidbCloudEssential": "TiDB Cloud Essential",
"tidbCloudDedicated": "TiDB Cloud Dedicated",
"tidbCloudLake": "TiDB Cloud Lake",
"tidbCloudByoc": "TiDB Cloud BYOC",
"tidbShortTerm": "TiDB",
"tidbOnKubernetes": "TiDB on Kubernetes",
"tidbCloudReleases": "TiDB Cloud 发布记录",
Expand Down
13 changes: 13 additions & 0 deletions src/components/Layout/Header/HeaderNavConfigData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ const getDefaultNavConfig = (
}
},
},
{
type: "item",
label: t("navbar.tidbCloudByoc"),
to: `/tidbcloud/premium/?${CLOUD_MODE_KEY}=${CloudPlan.Byoc}`,
selected: (namespace) =>
namespace === TOCNamespace.TiDBCloud &&
cloudPlan === CloudPlan.Byoc,
onClick: () => {
if (typeof window !== "undefined") {
sessionStorage.setItem(CLOUD_MODE_KEY, CloudPlan.Byoc);
}
},
},
],
},
{
Expand Down
Loading