Skip to content

Commit 3294389

Browse files
heiskrCopilot
andauthored
Fall back to English when translated frontmatter parses to a non-object (#62093)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 30302a2 commit 3294389

5 files changed

Lines changed: 51 additions & 5 deletions

File tree

src/article-api/tests/pageinfo.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ describe('pageinfo api', () => {
5858
expect(res.headers['surrogate-key']).toBe(makeLanguageSurrogateKey('en'))
5959
})
6060

61+
test('corrupted translation frontmatter falls back to English title', async () => {
62+
// The Japanese counterpart of this page has frontmatter whose ASCII `:`
63+
// key/value separators were replaced with fullwidth colons (`:`), so the
64+
// YAML parses to a scalar string rather than an object. When that happens
65+
// none of the frontmatter keys exist. Without the non-object fallback in
66+
// page-data, `meta.title` comes back empty and the search scraper rejects
67+
// the record with "Record has empty title". It must fall back to English.
68+
const res = await get(makeURL('/ja/get-started/foo/broken-frontmatter-translation'))
69+
expect(res.statusCode).toBe(200)
70+
const meta = JSON.parse(res.body) as PageMetadata
71+
expect(meta.title).toBe('Broken frontmatter translation fallback')
72+
})
73+
6174
test('a pathname that does not exist', async () => {
6275
const res = await get(makeURL('/en/never/heard/of'))
6376
expect(res.statusCode).toBe(404)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
title: Broken frontmatter translation fallback
3+
intro: Demonstrates falling back to English when a translation's frontmatter is corrupted.
4+
versions:
5+
fpt: "*"
6+
ghes: "*"
7+
ghec: "*"
8+
---
9+
10+
## Intro
11+
12+
This page exists to test that a translated file whose frontmatter is corrupted
13+
into a non-object (for example, fullwidth colons replacing the ASCII `:`
14+
separators) falls back to the English title instead of rendering an empty title.

src/fixtures/fixtures/content/get-started/foo/index.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
title: Fooing Around
33
shortTitle: Foo
4-
intro: 'The most basic of fixture data for {% data variables.product.product_name %}'
4+
intro: "The most basic of fixture data for {% data variables.product.product_name %}"
55
versions:
6-
fpt: '*'
7-
ghes: '*'
8-
ghec: '*'
6+
fpt: "*"
7+
ghes: "*"
8+
ghec: "*"
99
children:
1010
- /bar
1111
- /autotitling
@@ -19,4 +19,5 @@ children:
1919
- /table-with-ifversions
2020
- /code-snippet-with-hashbang
2121
- /journey-test-article
22+
- /broken-frontmatter-translation
2223
---
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: 壊れたフロントマターのフォールバック shortTitle: フォールバック intro: 翻訳のフロントマターが壊れているときに英語にフォールバックすることを示します。
3+
versions: fpt: '*' ghes: '*' ghec: '*'
4+
---
5+
6+
## Intro
7+
8+
このページは、翻訳ファイルのフロントマターが非オブジェクトに壊れている場合に、
9+
空のタイトルではなく英語のタイトルにフォールバックすることをテストするために存在します。

src/frame/lib/page-data.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ async function translateTree(
164164
content = read.content
165165
data = read.data as Record<string, unknown>
166166

167-
if (!data) {
167+
if (!data || typeof data !== 'object' || Array.isArray(data)) {
168168
// If the file's frontmatter Yaml is entirely broken,
169169
// the result of `readFileContents()` is that you just
170170
// get a `errors` key. E.g.
@@ -177,6 +177,15 @@ async function translateTree(
177177
// }
178178
// ]
179179
//
180+
// A translated file can also be corrupted so that the frontmatter
181+
// parses to a non-object. For example, if machine translation replaces
182+
// the ASCII `:` key/value separators with fullwidth colons (`:`), YAML
183+
// parses the whole block as a single scalar string. `data` is then a
184+
// string rather than an object, so none of the frontmatter keys (like
185+
// `title`) exist and the per-key English fallback below never fires,
186+
// leaving the page with an empty title. Treat that the same as entirely
187+
// broken frontmatter and fall back to English.
188+
//
180189
// If this the case throw error so we can lump this error with
181190
// how we deal with the file not even being present on disk.
182191
throw new FrontmatterParsingError(JSON.stringify(read.errors), true)

0 commit comments

Comments
 (0)