1
1
import { renderContent } from '@/content-render/index'
2
2
import Page from '@/frame/lib/page'
3
3
import { TitleFromAutotitleError } from '@/content-render/unified/rewrite-local-links'
4
+ import type { Context } from '@/types'
4
5
5
6
export class EmptyTitleError extends Error { }
6
7
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
+
7
25
const 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 )
10
28
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
13
31
14
- const isFallbackableError = ( error ) =>
32
+ const isEmptyTitleError = ( error : unknown ) : error is EmptyTitleError =>
33
+ error instanceof EmptyTitleError
34
+
35
+ const isFallbackableError = ( error : unknown ) : boolean =>
15
36
isLiquidError ( error ) || isAutotitleError ( error ) || isEmptyTitleError ( error )
16
37
17
38
/**
18
39
* Creates an HTML comment with translation fallback error information
19
40
* Includes detailed debugging information for translators
20
41
*/
21
- export function createTranslationFallbackComment ( error , property ) {
42
+ export function createTranslationFallbackComment ( error : Error , property : string ) : string {
22
43
const errorType = error . name || 'UnknownError'
23
- let errorDetails = [ ]
44
+ const errorDetails : string [ ] = [ ]
24
45
25
46
// Add basic error information
26
47
errorDetails . push ( `TRANSLATION_FALLBACK` )
@@ -82,14 +103,21 @@ export function createTranslationFallbackComment(error, property) {
82
103
// function. This means, we can know, in the middleware (which is a
83
104
// higher level than `lib/`) how to use the URL to figure out the
84
105
// 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 > {
86
114
if ( ! ( page instanceof Page ) ) {
87
115
throw new Error ( `The first argument has to be Page instance (not ${ typeof page } )` )
88
116
}
89
117
if ( typeof property !== 'string' ) {
90
118
throw new Error ( `The second argument has to be a string (not ${ typeof property } )` )
91
119
}
92
- const template = page [ property ]
120
+ const template = ( page as any ) [ property ] as string
93
121
try {
94
122
const output = await renderContent ( template , context , options )
95
123
if ( options && options . throwIfEmpty && ! output . trim ( ) ) {
@@ -101,7 +129,7 @@ export async function renderContentWithFallback(page, property, context, options
101
129
// on English for.
102
130
if ( isFallbackableError ( error ) && context . getEnglishPage ) {
103
131
const enPage = context . getEnglishPage ( context )
104
- const englishTemplate = enPage [ property ]
132
+ const englishTemplate = ( enPage as any ) [ property ] as string
105
133
// If you don't change the context, it'll confuse the liquid plugins
106
134
// like `data.js` that uses `environment.scope.currentLanguage`
107
135
const enContext = Object . assign ( { } , context , { currentLanguage : 'en' } )
@@ -112,7 +140,7 @@ export async function renderContentWithFallback(page, property, context, options
112
140
// Add HTML comment with error details for non-English languages
113
141
// Skip for textOnly rendering to avoid breaking plain text output
114
142
if ( context . currentLanguage !== 'en' && ! options ?. textOnly ) {
115
- const errorComment = createTranslationFallbackComment ( error , property )
143
+ const errorComment = createTranslationFallbackComment ( error as Error , property )
116
144
return errorComment + '\n' + fallbackContent
117
145
}
118
146
@@ -137,19 +165,23 @@ export async function renderContentWithFallback(page, property, context, options
137
165
// (enContext) => renderContent(enTrack.title, enContext, renderOpts)
138
166
// )
139
167
//
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 > {
141
173
try {
142
- return await callable ( context )
174
+ return await Promise . resolve ( callable ( context ) )
143
175
} catch ( error ) {
144
176
if ( isFallbackableError ( error ) && context . currentLanguage !== 'en' ) {
145
177
const enContext = Object . assign ( { } , context , { currentLanguage : 'en' } )
146
- const fallbackContent = await fallback ( enContext )
178
+ const fallbackContent = await Promise . resolve ( fallback ( enContext ) )
147
179
148
180
// Add HTML comment with error details for non-English languages
149
181
// Only for HTML content (detected by presence of HTML tags)
150
182
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
153
185
}
154
186
155
187
return fallbackContent
0 commit comments