11import { renderContent } from '@/content-render/index'
22import Page from '@/frame/lib/page'
33import { TitleFromAutotitleError } from '@/content-render/unified/rewrite-local-links'
4+ import type { Context } from '@/types'
45
56export class EmptyTitleError extends Error { }
67
8+ interface LiquidToken {
9+ file ?: string
10+ getPosition ?: ( ) => [ number , number ]
11+ }
12+
13+ interface LiquidError extends Error {
14+ token ?: LiquidToken
15+ originalError ?: Error
16+ }
17+
18+ interface RenderOptions {
19+ throwIfEmpty ?: boolean
20+ textOnly ?: boolean
21+ cache ?: boolean | ( ( template : string , context : any ) => string )
22+ [ key : string ] : any
23+ }
24+
725const LIQUID_ERROR_NAMES = new Set ( [ 'RenderError' , 'ParseError' , 'TokenizationError' ] )
8- export const isLiquidError = ( error ) =>
9- error instanceof Error && error . name && LIQUID_ERROR_NAMES . has ( error . name )
26+ export const isLiquidError = ( error : unknown ) : error is LiquidError =>
27+ error instanceof Error && error . name !== undefined && LIQUID_ERROR_NAMES . has ( error . name )
1028
11- const isAutotitleError = ( error ) => error instanceof TitleFromAutotitleError
12- const isEmptyTitleError = ( error ) => error instanceof EmptyTitleError
29+ const isAutotitleError = ( error : unknown ) : error is TitleFromAutotitleError =>
30+ error instanceof TitleFromAutotitleError
1331
14- const isFallbackableError = ( error ) =>
32+ const isEmptyTitleError = ( error : unknown ) : error is EmptyTitleError =>
33+ error instanceof EmptyTitleError
34+
35+ const isFallbackableError = ( error : unknown ) : boolean =>
1536 isLiquidError ( error ) || isAutotitleError ( error ) || isEmptyTitleError ( error )
1637
1738/**
1839 * Creates an HTML comment with translation fallback error information
1940 * Includes detailed debugging information for translators
2041 */
21- export function createTranslationFallbackComment ( error , property ) {
42+ export function createTranslationFallbackComment ( error : Error , property : string ) : string {
2243 const errorType = error . name || 'UnknownError'
23- let errorDetails = [ ]
44+ const errorDetails : string [ ] = [ ]
2445
2546 // Add basic error information
2647 errorDetails . push ( `TRANSLATION_FALLBACK` )
@@ -82,14 +103,21 @@ export function createTranslationFallbackComment(error, property) {
82103// function. This means, we can know, in the middleware (which is a
83104// higher level than `lib/`) how to use the URL to figure out the
84105// equivalent English page instance.
85- export async function renderContentWithFallback ( page , property , context , options ) {
106+ export async function renderContentWithFallback (
107+ // Using `any` type for page because the actual Page class from @/frame/lib/page
108+ // has more properties than the Page interface defined in @/types, causing type conflicts
109+ page : any ,
110+ property : string ,
111+ context : Context ,
112+ options ?: RenderOptions ,
113+ ) : Promise < string > {
86114 if ( ! ( page instanceof Page ) ) {
87115 throw new Error ( `The first argument has to be Page instance (not ${ typeof page } )` )
88116 }
89117 if ( typeof property !== 'string' ) {
90118 throw new Error ( `The second argument has to be a string (not ${ typeof property } )` )
91119 }
92- const template = page [ property ]
120+ const template = ( page as any ) [ property ] as string
93121 try {
94122 const output = await renderContent ( template , context , options )
95123 if ( options && options . throwIfEmpty && ! output . trim ( ) ) {
@@ -101,7 +129,7 @@ export async function renderContentWithFallback(page, property, context, options
101129 // on English for.
102130 if ( isFallbackableError ( error ) && context . getEnglishPage ) {
103131 const enPage = context . getEnglishPage ( context )
104- const englishTemplate = enPage [ property ]
132+ const englishTemplate = ( enPage as any ) [ property ] as string
105133 // If you don't change the context, it'll confuse the liquid plugins
106134 // like `data.js` that uses `environment.scope.currentLanguage`
107135 const enContext = Object . assign ( { } , context , { currentLanguage : 'en' } )
@@ -112,7 +140,7 @@ export async function renderContentWithFallback(page, property, context, options
112140 // Add HTML comment with error details for non-English languages
113141 // Skip for textOnly rendering to avoid breaking plain text output
114142 if ( context . currentLanguage !== 'en' && ! options ?. textOnly ) {
115- const errorComment = createTranslationFallbackComment ( error , property )
143+ const errorComment = createTranslationFallbackComment ( error as Error , property )
116144 return errorComment + '\n' + fallbackContent
117145 }
118146
@@ -137,19 +165,23 @@ export async function renderContentWithFallback(page, property, context, options
137165// (enContext) => renderContent(enTrack.title, enContext, renderOpts)
138166// )
139167//
140- export async function executeWithFallback ( context , callable , fallback ) {
168+ export async function executeWithFallback < T > (
169+ context : Context ,
170+ callable : ( context : Context ) => T | Promise < T > ,
171+ fallback : ( enContext : Context ) => T | Promise < T > ,
172+ ) : Promise < T > {
141173 try {
142- return await callable ( context )
174+ return await Promise . resolve ( callable ( context ) )
143175 } catch ( error ) {
144176 if ( isFallbackableError ( error ) && context . currentLanguage !== 'en' ) {
145177 const enContext = Object . assign ( { } , context , { currentLanguage : 'en' } )
146- const fallbackContent = await fallback ( enContext )
178+ const fallbackContent = await Promise . resolve ( fallback ( enContext ) )
147179
148180 // Add HTML comment with error details for non-English languages
149181 // Only for HTML content (detected by presence of HTML tags)
150182 if ( typeof fallbackContent === 'string' && / < [ ^ > ] + > / . test ( fallbackContent ) ) {
151- const errorComment = createTranslationFallbackComment ( error , 'content' )
152- return errorComment + '\n' + fallbackContent
183+ const errorComment = createTranslationFallbackComment ( error as Error , 'content' )
184+ return ( errorComment + '\n' + fallbackContent ) as T
153185 }
154186
155187 return fallbackContent
0 commit comments