From 20a154cd76850e15bd3fb786ed0b9e51304e525b Mon Sep 17 00:00:00 2001 From: allo Date: Mon, 31 Jul 2023 14:11:21 +0800 Subject: [PATCH 1/9] fix(build): stop report URL to not localized doc as broken --- build/flaws/broken-links.ts | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/build/flaws/broken-links.ts b/build/flaws/broken-links.ts index 33e5c806d867..f00e38dd0a53 100644 --- a/build/flaws/broken-links.ts +++ b/build/flaws/broken-links.ts @@ -63,13 +63,16 @@ function mutateLink( ) { if (isSelfLink) { $element.attr("aria-current", "page"); - } else if (suggestion) { - $element.attr("href", suggestion); } else if (enUSFallback) { + // If we have an English (US) fallback, then we use this first. + // As we still suggest the translated version even if we only + // have an English (US) version. $element.attr("href", enUSFallback); $element.append(` (${DEFAULT_LOCALE})`); $element.addClass("only-in-en-us"); $element.attr("title", "Currently only available in English (US)"); + } else if (suggestion) { + $element.attr("href", suggestion); } else { $element.addClass("page-not-created"); $element.attr("title", "This is a link to an unwritten page"); @@ -293,7 +296,6 @@ export function getBrokenLinksFlaws( resolved + absoluteURL.search + absoluteURL.hash.toLowerCase() ); } else { - let enUSFallbackURL = null; // Test if the document is a translated document and the link isn't // to an en-US URL. We know the link is broken (in this locale!) // but it might be "salvageable" if we link the en-US equivalent. @@ -311,27 +313,32 @@ export function getBrokenLinksFlaws( ); const enUSFound = Document.findByURL(enUSHrefNormalized); if (enUSFound) { - enUSFallbackURL = enUSFound.url; + // Only the en-US document exists + mutateLink(a, null, enUSFound.url); } else { const enUSResolved = Redirect.resolve(enUSHrefNormalized); + let suggestion = null; + let enUSFallbackURL = null; if (enUSResolved !== enUSHrefNormalized) { enUSFallbackURL = enUSResolved + absoluteURL.search + absoluteURL.hash.toLowerCase(); + suggestion = enUSFallbackURL.replace( + `/${DEFAULT_LOCALE}/`, + `/${doc.locale}/` + ); } + addBrokenLink( + a, + checked.get(href), + href, + suggestion, + null, + enUSFallbackURL + ); } } - addBrokenLink( - a, - checked.get(href), - href, - null, - enUSFallbackURL - ? "Can use the English (en-US) link as a fallback" - : null, - enUSFallbackURL - ); } } // But does it have the correct case?! From 2b7d2a7b38d965725b08dffba19cd9426ed958ff Mon Sep 17 00:00:00 2001 From: Allo Date: Fri, 4 Aug 2023 21:08:11 +0800 Subject: [PATCH 2/9] fix: add missing broken link reports --- build/flaws/broken-links.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/build/flaws/broken-links.ts b/build/flaws/broken-links.ts index f00e38dd0a53..6fa6b923aa7e 100644 --- a/build/flaws/broken-links.ts +++ b/build/flaws/broken-links.ts @@ -312,8 +312,10 @@ export function getBrokenLinksFlaws( `/${DEFAULT_LOCALE}/` ); const enUSFound = Document.findByURL(enUSHrefNormalized); + // Note, we still recommend that contributors use localized links, + // even if the target document is still not localized. if (enUSFound) { - // Only the en-US document exists + // Found the en-US version of the document. Just link to that. mutateLink(a, null, enUSFound.url); } else { const enUSResolved = Redirect.resolve(enUSHrefNormalized); @@ -338,6 +340,9 @@ export function getBrokenLinksFlaws( enUSFallbackURL ); } + } else { + // The link is broken and we don't have a suggestion. + addBrokenLink(a, checked.get(href), href); } } } From 58e25ae6858377ae954e37bf9ac55050ad57b2f7 Mon Sep 17 00:00:00 2001 From: Allo Date: Fri, 4 Aug 2023 21:19:27 +0800 Subject: [PATCH 3/9] fix functional testing --- testing/tests/index.test.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/testing/tests/index.test.ts b/testing/tests/index.test.ts index 25c20f221c5d..3a077dae9e58 100644 --- a/testing/tests/index.test.ts +++ b/testing/tests/index.test.ts @@ -1603,19 +1603,13 @@ test("translated content broken links can fall back to en-us", () => { const { doc } = JSON.parse(fs.readFileSync(jsonFile, "utf-8")) as { doc: Doc; }; - const map = new Map(doc.flaws.broken_links.map((x) => [x.href, x])); - expect(map.get("/fr/docs/Web/CSS/dumber").explanation).toBe( - "Can use the English (en-US) link as a fallback" - ); - expect(map.get("/fr/docs/Web/CSS/number").explanation).toBe( - "Can use the English (en-US) link as a fallback" - ); const htmlFile = path.join(builtFolder, "index.html"); const html = fs.readFileSync(htmlFile, "utf-8"); const $ = cheerio.load(html); expect($('article a[href="/fr/docs/Web/CSS/dumber"]')).toHaveLength(0); expect($('article a[href="/fr/docs/Web/CSS/number"]')).toHaveLength(0); + // Localized docs don't exist, but fallback to en-US is possible expect($('article a[href="/en-US/docs/Web/CSS/number"]')).toHaveLength(2); expect($("article a.only-in-en-us")).toHaveLength(2); expect($("article a.only-in-en-us").attr("title")).toBe( From 98c543508b81f9d17af50583de94f27f7d1769eb Mon Sep 17 00:00:00 2001 From: Claas Augner Date: Fri, 15 Mar 2024 13:18:24 +0100 Subject: [PATCH 4/9] test(flaws): test that fallback links are not in broken links --- testing/tests/index.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/testing/tests/index.test.ts b/testing/tests/index.test.ts index 9811992bb1ce..5ffbe56834b4 100644 --- a/testing/tests/index.test.ts +++ b/testing/tests/index.test.ts @@ -1614,6 +1614,10 @@ test("translated content broken links can fall back to en-us", () => { doc: Doc; }; + const map = new Map(doc.flaws.broken_links.map((x) => [x.href, x])); + expect(map.keys()).not.toContain("/fr/docs/Web/CSS/dumber"); + expect(map.keys()).not.toContain("/fr/docs/Web/CSS/number"); + const htmlFile = path.join(builtFolder, "index.html"); const html = fs.readFileSync(htmlFile, "utf-8"); const $ = cheerio.load(html); From 40fddf317362a1b48e9c5ca61d00f45a079bd455 Mon Sep 17 00:00:00 2001 From: Claas Augner Date: Fri, 15 Mar 2024 13:30:06 +0100 Subject: [PATCH 5/9] fixup! test(flaws): test that fallback links are not in broken links --- testing/tests/index.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/testing/tests/index.test.ts b/testing/tests/index.test.ts index 5ffbe56834b4..da37f82f4c99 100644 --- a/testing/tests/index.test.ts +++ b/testing/tests/index.test.ts @@ -1614,9 +1614,9 @@ test("translated content broken links can fall back to en-us", () => { doc: Doc; }; - const map = new Map(doc.flaws.broken_links.map((x) => [x.href, x])); - expect(map.keys()).not.toContain("/fr/docs/Web/CSS/dumber"); - expect(map.keys()).not.toContain("/fr/docs/Web/CSS/number"); + const brokenHrefs = doc.flaws.broken_links.map((x) => x.href); + expect(brokenHrefs).not.toContain("/fr/docs/Web/CSS/dumber"); + expect(brokenHrefs).not.toContain("/fr/docs/Web/CSS/number"); const htmlFile = path.join(builtFolder, "index.html"); const html = fs.readFileSync(htmlFile, "utf-8"); From 6ee38efe45b928f5ac4143dcf78441c77e7464ea Mon Sep 17 00:00:00 2001 From: Claas Augner Date: Fri, 15 Mar 2024 13:47:25 +0100 Subject: [PATCH 6/9] test(flaws): test that fallback links are fixabln broken link flaws --- testing/tests/index.test.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/testing/tests/index.test.ts b/testing/tests/index.test.ts index da37f82f4c99..29815ae8cb70 100644 --- a/testing/tests/index.test.ts +++ b/testing/tests/index.test.ts @@ -1614,9 +1614,21 @@ test("translated content broken links can fall back to en-us", () => { doc: Doc; }; - const brokenHrefs = doc.flaws.broken_links.map((x) => x.href); - expect(brokenHrefs).not.toContain("/fr/docs/Web/CSS/dumber"); - expect(brokenHrefs).not.toContain("/fr/docs/Web/CSS/number"); + const map = new Map(doc.flaws.broken_links.map((x) => [x.href, x])); + expect(map.get("/fr/docs/Web/CSS/dumber")).toMatchObject({ + explanation: "Can't resolve /fr/docs/Web/CSS/dumber", + suggestion: "/en-US/docs/Web/CSS/dumber", + fixable: true, + line: 19, + column: 16, + }); + expect(map.get("/fr/docs/Web/CSS/number")).toMatchObject({ + explanation: "Can't resolve /fr/docs/Web/CSS/number", + suggestion: "/en-US/docs/Web/CSS/number", + fixable: true, + line: 21, + column: 14, + }); const htmlFile = path.join(builtFolder, "index.html"); const html = fs.readFileSync(htmlFile, "utf-8"); From 8ae0a596dae052df78fbeaf7d5f81da8ed7c46f2 Mon Sep 17 00:00:00 2001 From: Claas Augner Date: Fri, 15 Mar 2024 13:55:52 +0100 Subject: [PATCH 7/9] fixup! test(flaws): test that fallback links are fixabln broken link flaws --- testing/tests/index.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testing/tests/index.test.ts b/testing/tests/index.test.ts index 29815ae8cb70..1e8c2c603bb9 100644 --- a/testing/tests/index.test.ts +++ b/testing/tests/index.test.ts @@ -1617,14 +1617,14 @@ test("translated content broken links can fall back to en-us", () => { const map = new Map(doc.flaws.broken_links.map((x) => [x.href, x])); expect(map.get("/fr/docs/Web/CSS/dumber")).toMatchObject({ explanation: "Can't resolve /fr/docs/Web/CSS/dumber", - suggestion: "/en-US/docs/Web/CSS/dumber", + suggestion: "/fr/docs/Web/CSS/dumber", fixable: true, line: 19, column: 16, }); expect(map.get("/fr/docs/Web/CSS/number")).toMatchObject({ explanation: "Can't resolve /fr/docs/Web/CSS/number", - suggestion: "/en-US/docs/Web/CSS/number", + suggestion: "/fr/docs/Web/CSS/number", fixable: true, line: 21, column: 14, From be2c2d357f4c996ea7f47b06a0b9cd4e1862d16e Mon Sep 17 00:00:00 2001 From: Claas Augner <495429+caugner@users.noreply.github.com> Date: Fri, 15 Mar 2024 14:03:38 +0100 Subject: [PATCH 8/9] Update testing/tests/index.test.ts --- testing/tests/index.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/tests/index.test.ts b/testing/tests/index.test.ts index 1e8c2c603bb9..3758e9e1290f 100644 --- a/testing/tests/index.test.ts +++ b/testing/tests/index.test.ts @@ -1617,7 +1617,7 @@ test("translated content broken links can fall back to en-us", () => { const map = new Map(doc.flaws.broken_links.map((x) => [x.href, x])); expect(map.get("/fr/docs/Web/CSS/dumber")).toMatchObject({ explanation: "Can't resolve /fr/docs/Web/CSS/dumber", - suggestion: "/fr/docs/Web/CSS/dumber", + suggestion: "/fr/docs/Web/CSS/number", fixable: true, line: 19, column: 16, From 65cb9c55f35adf01abbaab13c7119892dc211d70 Mon Sep 17 00:00:00 2001 From: Claas Augner <495429+caugner@users.noreply.github.com> Date: Fri, 15 Mar 2024 14:08:02 +0100 Subject: [PATCH 9/9] Update testing/tests/index.test.ts --- testing/tests/index.test.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/testing/tests/index.test.ts b/testing/tests/index.test.ts index 3758e9e1290f..83c06cafe614 100644 --- a/testing/tests/index.test.ts +++ b/testing/tests/index.test.ts @@ -1622,13 +1622,7 @@ test("translated content broken links can fall back to en-us", () => { line: 19, column: 16, }); - expect(map.get("/fr/docs/Web/CSS/number")).toMatchObject({ - explanation: "Can't resolve /fr/docs/Web/CSS/number", - suggestion: "/fr/docs/Web/CSS/number", - fixable: true, - line: 21, - column: 14, - }); + expect(map.get("/fr/docs/Web/CSS/number")).toBeUndefined(); const htmlFile = path.join(builtFolder, "index.html"); const html = fs.readFileSync(htmlFile, "utf-8");