fallback on failed autotitle lookups in translations (#34847)
This commit is contained in:
@@ -17,6 +17,11 @@ const externalRedirects = readJsonFile('./lib/redirects/external-sites.json')
|
|||||||
// Meaning it can be 'AUTOTITLE ' or ' AUTOTITLE' or 'AUTOTITLE'
|
// Meaning it can be 'AUTOTITLE ' or ' AUTOTITLE' or 'AUTOTITLE'
|
||||||
const AUTOTITLE = /^\s*AUTOTITLE\s*$/
|
const AUTOTITLE = /^\s*AUTOTITLE\s*$/
|
||||||
|
|
||||||
|
// This is exported because in translations, we need to treat this as
|
||||||
|
// one of those Liquid parsing errors which happens on corrupted translations
|
||||||
|
// which we use to know that we need to fall back to English.
|
||||||
|
export class TitleFromAutotitleError extends Error {}
|
||||||
|
|
||||||
// Matches any <a> tags with an href that starts with `/`
|
// Matches any <a> tags with an href that starts with `/`
|
||||||
const matcher = (node) =>
|
const matcher = (node) =>
|
||||||
node.type === 'element' &&
|
node.type === 'element' &&
|
||||||
@@ -75,7 +80,7 @@ async function getNewTitleSetter(child, href, context) {
|
|||||||
async function getNewTitle(href, context) {
|
async function getNewTitle(href, context) {
|
||||||
const page = findPage(href, context.pages, context.redirects)
|
const page = findPage(href, context.pages, context.redirects)
|
||||||
if (!page) {
|
if (!page) {
|
||||||
throw new Error(`Unable to find Page by href '${href}'`)
|
throw new TitleFromAutotitleError(`Unable to find Page by href '${href}'`)
|
||||||
}
|
}
|
||||||
return await page.renderProp('title', context, { textOnly: true })
|
return await page.renderProp('title', context, { textOnly: true })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,15 @@
|
|||||||
import renderContent from './render-content/index.js'
|
import renderContent from './render-content/index.js'
|
||||||
import Page from './page.js'
|
import Page from './page.js'
|
||||||
|
import { TitleFromAutotitleError } from './render-content/plugins/rewrite-local-links.js'
|
||||||
|
|
||||||
const LIQUID_ERROR_NAMES = new Set(['RenderError', 'ParseError', 'TokenizationError'])
|
const LIQUID_ERROR_NAMES = new Set(['RenderError', 'ParseError', 'TokenizationError'])
|
||||||
const isLiquidError = (error) =>
|
const isLiquidError = (error) =>
|
||||||
error instanceof Error && error.name && LIQUID_ERROR_NAMES.has(error.name)
|
error instanceof Error && error.name && LIQUID_ERROR_NAMES.has(error.name)
|
||||||
|
|
||||||
|
const isAutotitleError = (error) => error instanceof TitleFromAutotitleError
|
||||||
|
|
||||||
|
const isFallbackableError = (error) => isLiquidError(error) || isAutotitleError(error)
|
||||||
|
|
||||||
// Returns a string by wrapping `renderContent()`. The input string to
|
// Returns a string by wrapping `renderContent()`. The input string to
|
||||||
// `renderContent` is one that contains Liquid and Markdown. The output
|
// `renderContent` is one that contains Liquid and Markdown. The output
|
||||||
// is HTML.
|
// is HTML.
|
||||||
@@ -28,7 +33,7 @@ export async function renderContentWithFallback(page, property, context, options
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Only bother trying to fallback if it was an error we *can* fall back
|
// Only bother trying to fallback if it was an error we *can* fall back
|
||||||
// on English for.
|
// on English for.
|
||||||
if (isLiquidError(error) && context.getEnglishPage) {
|
if (isFallbackableError(error) && context.getEnglishPage) {
|
||||||
const enPage = context.getEnglishPage(context)
|
const enPage = context.getEnglishPage(context)
|
||||||
const englishTemplate = enPage[property]
|
const englishTemplate = enPage[property]
|
||||||
// If you don't change the context, it'll confuse the liquid plugins
|
// If you don't change the context, it'll confuse the liquid plugins
|
||||||
@@ -60,7 +65,7 @@ export async function executeWithFallback(context, callable, fallback) {
|
|||||||
try {
|
try {
|
||||||
return await callable(context)
|
return await callable(context)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (isLiquidError(error) && context.currentLanguage !== 'en') {
|
if (isFallbackableError(error) && context.currentLanguage !== 'en') {
|
||||||
const enContext = Object.assign({}, context, { currentLanguage: 'en' })
|
const enContext = Object.assign({}, context, { currentLanguage: 'en' })
|
||||||
return await fallback(enContext)
|
return await fallback(enContext)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user