diff --git a/gatsby/url-resolver/__tests__/url-resolver.test.ts b/gatsby/url-resolver/__tests__/url-resolver.test.ts index 971ff035..c3a545f1 100644 --- a/gatsby/url-resolver/__tests__/url-resolver.test.ts +++ b/gatsby/url-resolver/__tests__/url-resolver.test.ts @@ -214,6 +214,42 @@ describe("calculateFileUrl", () => { expect(url).toBe("/en/tidb/dev/page/"); }); + it("should resolve tidb-data-migration _index with release branch alias", () => { + const absolutePath = path.join( + sourceBasePath, + "zh/tidb-data-migration/release-2.0/_index.md" + ); + const url = calculateFileUrlWithConfig(absolutePath, testConfig); + expect(url).toBe("/zh/tidb-data-migration/v2.0"); + }); + + it("should resolve tidb-data-migration pages with release branch alias", () => { + const absolutePath = path.join( + sourceBasePath, + "zh/tidb-data-migration/release-2.0/overview.md" + ); + const url = calculateFileUrlWithConfig(absolutePath, testConfig); + expect(url).toBe("/zh/tidb-data-migration/v2.0/overview/"); + }); + + it("should resolve tidb-data-migration v1.0 pages with release branch alias", () => { + const absolutePath = path.join( + sourceBasePath, + "zh/tidb-data-migration/release-1.0/overview.md" + ); + const url = calculateFileUrlWithConfig(absolutePath, testConfig); + expect(url).toBe("/zh/tidb-data-migration/v1.0/overview/"); + }); + + it("should resolve tidb-data-migration nested _index with folders", () => { + const absolutePath = path.join( + sourceBasePath, + "zh/tidb-data-migration/release-2.0/releases/_index.md" + ); + const url = calculateFileUrlWithConfig(absolutePath, testConfig); + expect(url).toBe("/zh/tidb-data-migration/v2.0/releases"); + }); + it("should resolve api folder", () => { const absolutePath = path.join( sourceBasePath, @@ -469,6 +505,61 @@ describe("calculateFileUrl with defaultLanguage: 'en'", () => { // release-8.5 -> stable via branch-alias-tidb (exact match takes precedence) expect(url).toBe("/tidb/stable/alert-rules"); }); + + it("should omit /en/ prefix for English tidb-data-migration files", () => { + const absolutePath = path.join( + sourceBasePath, + "en/tidb-data-migration/release-5.3/dm-overview.md" + ); + const url = calculateFileUrlWithConfig( + absolutePath, + configWithDefaultLang, + true + ); + expect(url).toBe("/tidb-data-migration/v5.3/dm-overview"); + }); + + it("should omit /en/ prefix for English tidb-data-migration release indexes", () => { + const cases = [ + ["release-5.3", "/tidb-data-migration/v5.3"], + ["release-2.0", "/tidb-data-migration/v2.0"], + ["release-1.0", "/tidb-data-migration/v1.0"], + ]; + + for (const [branch, expected] of cases) { + const absolutePath = path.join( + sourceBasePath, + `en/tidb-data-migration/${branch}/_index.md` + ); + const url = calculateFileUrlWithConfig( + absolutePath, + configWithDefaultLang, + true + ); + expect(url).toBe(expected); + } + }); + + it("should keep /zh/ prefix for Chinese tidb-data-migration release indexes", () => { + const cases = [ + ["release-5.3", "/zh/tidb-data-migration/v5.3"], + ["release-2.0", "/zh/tidb-data-migration/v2.0"], + ["release-1.0", "/zh/tidb-data-migration/v1.0"], + ]; + + for (const [branch, expected] of cases) { + const absolutePath = path.join( + sourceBasePath, + `zh/tidb-data-migration/${branch}/_index.md` + ); + const url = calculateFileUrlWithConfig( + absolutePath, + configWithDefaultLang, + true + ); + expect(url).toBe(expected); + } + }); }); describe("calculateFileUrl with slug format (relative path)", () => { diff --git a/gatsby/url-resolver/config.ts b/gatsby/url-resolver/config.ts index c5e78a4c..7eff6779 100644 --- a/gatsby/url-resolver/config.ts +++ b/gatsby/url-resolver/config.ts @@ -123,6 +123,28 @@ export const defaultUrlResolverConfig: UrlResolverConfig = { ignoreIf: ["_index", "_docHome"], }, }, + // tidb-data-migration index pages with folders (avoid URL collision) + { + sourcePattern: + "/{lang}/tidb-data-migration/{branch}/{...folders}/{filename}", + targetPattern: + "/{lang}/tidb-data-migration/{branch:branch-alias-tidb-data-migration}/{folders}", + conditions: { filename: ["_index"] }, + filenameTransform: { + ignoreIf: ["_index"], + }, + }, + // tidb-data-migration with branch and optional folders + // /en/tidb-data-migration/release-5.3/{...folders}/{filename} -> /en/tidb-data-migration/v5.3/{filename} + { + sourcePattern: + "/{lang}/tidb-data-migration/{branch}/{...folders}/{filename}", + targetPattern: + "/{lang}/tidb-data-migration/{branch:branch-alias-tidb-data-migration}/{filename}", + filenameTransform: { + ignoreIf: ["_index", "_docHome"], + }, + }, // Fallback: /{lang}/{repo}/{...any}/{filename} -> /{lang}/{repo}/{filename} { sourcePattern: "/{lang}/{repo}/{...any}/{filename}", @@ -164,5 +186,15 @@ export const defaultUrlResolverConfig: UrlResolverConfig = { "release-*": "v*", }, }, + // Branch alias for tidb-data-migration: used in {branch:branch-alias-tidb-data-migration} + "branch-alias-tidb-data-migration": { + mappings: { + // Wildcard pattern: release-* -> v* + // Examples: + // release-5.3 -> v5.3 + // release-2.0 -> v2.0 + "release-*": "v*", + }, + }, }, };