mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 12:50:38 -07:00
fix: type compatible with v9 tag definition, support Context as scope in various render APIs, #570
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { assert, isArray, isString, isFunction } from './util'
|
||||
import { LRU, LiquidCache } from './cache'
|
||||
import { FS } from './fs/fs'
|
||||
import { FS, LookupType } from './fs'
|
||||
import * as fs from './fs/node'
|
||||
import { defaultOperators, Operators } from './render'
|
||||
import { json } from './filters/misc'
|
||||
@@ -91,6 +91,10 @@ export interface RenderOptions {
|
||||
ownPropertyOnly?: boolean;
|
||||
}
|
||||
|
||||
export interface RenderFileOptions extends RenderOptions {
|
||||
lookupType?: LookupType;
|
||||
}
|
||||
|
||||
interface NormalizedOptions extends LiquidOptions {
|
||||
root?: string[];
|
||||
partials?: string[];
|
||||
|
||||
+21
-16
@@ -6,7 +6,7 @@ import { Render } from './render'
|
||||
import { Parser } from './parser'
|
||||
import { tags } from './tags'
|
||||
import { filters } from './filters'
|
||||
import { LiquidOptions, normalizeDirectoryList, NormalizedFullOptions, normalize, RenderOptions } from './liquid-options'
|
||||
import { LiquidOptions, normalizeDirectoryList, NormalizedFullOptions, normalize, RenderOptions, RenderFileOptions } from './liquid-options'
|
||||
|
||||
export class Liquid {
|
||||
public readonly options: NormalizedFullOptions
|
||||
@@ -25,8 +25,8 @@ export class Liquid {
|
||||
return this.parser.parse(html, filepath)
|
||||
}
|
||||
|
||||
public _render (tpl: Template[], scope: object | undefined, renderOptions: RenderOptions): IterableIterator<any> {
|
||||
const ctx = new Context(scope, this.options, renderOptions)
|
||||
public _render (tpl: Template[], scope: Context | object | undefined, renderOptions: RenderOptions): IterableIterator<any> {
|
||||
const ctx = scope instanceof Context ? scope : new Context(scope, this.options, renderOptions)
|
||||
return this.renderer.renderTemplates(tpl, ctx)
|
||||
}
|
||||
public async render (tpl: Template[], scope?: object, renderOptions?: RenderOptions): Promise<any> {
|
||||
@@ -40,14 +40,14 @@ export class Liquid {
|
||||
return this.renderer.renderTemplatesToNodeStream(tpl, ctx)
|
||||
}
|
||||
|
||||
public _parseAndRender (html: string, scope: object | undefined, renderOptions: RenderOptions): IterableIterator<any> {
|
||||
public _parseAndRender (html: string, scope: Context | object | undefined, renderOptions: RenderOptions): IterableIterator<any> {
|
||||
const tpl = this.parse(html)
|
||||
return this._render(tpl, scope, renderOptions)
|
||||
}
|
||||
public async parseAndRender (html: string, scope?: object, renderOptions?: RenderOptions): Promise<any> {
|
||||
public async parseAndRender (html: string, scope?: Context | object, renderOptions?: RenderOptions): Promise<any> {
|
||||
return toPromise(this._parseAndRender(html, scope, { ...renderOptions, sync: false }))
|
||||
}
|
||||
public parseAndRenderSync (html: string, scope?: object, renderOptions?: RenderOptions): any {
|
||||
public parseAndRenderSync (html: string, scope?: Context | object, renderOptions?: RenderOptions): any {
|
||||
return toValueSync(this._parseAndRender(html, scope, { ...renderOptions, sync: true }))
|
||||
}
|
||||
|
||||
@@ -57,19 +57,24 @@ export class Liquid {
|
||||
public _parseLayoutFile (file: string, sync?: boolean, currentFile?: string) {
|
||||
return this.parser.parseFile(file, sync, LookupType.Layouts, currentFile)
|
||||
}
|
||||
public async parseFile (file: string): Promise<Template[]> {
|
||||
return toPromise<Template[]>(this.parser.parseFile(file, false))
|
||||
public _parseFile (file: string, sync?: boolean, lookupType?: LookupType, currentFile?: string): Generator<unknown, Template[]> {
|
||||
return this.parser.parseFile(file, sync, lookupType, currentFile)
|
||||
}
|
||||
public parseFileSync (file: string): Template[] {
|
||||
return toValueSync<Template[]>(this.parser.parseFile(file, true))
|
||||
public async parseFile (file: string, lookupType?: LookupType): Promise<Template[]> {
|
||||
return toPromise<Template[]>(this.parser.parseFile(file, false, lookupType))
|
||||
}
|
||||
public async renderFile (file: string, ctx?: object, renderOptions?: RenderOptions) {
|
||||
const templates = await this.parseFile(file)
|
||||
return this.render(templates, ctx, renderOptions)
|
||||
public parseFileSync (file: string, lookupType?: LookupType): Template[] {
|
||||
return toValueSync<Template[]>(this.parser.parseFile(file, true, lookupType))
|
||||
}
|
||||
public renderFileSync (file: string, ctx?: object, renderOptions?: RenderOptions) {
|
||||
const templates = this.parseFileSync(file)
|
||||
return this.renderSync(templates, ctx, renderOptions)
|
||||
public * _renderFile (file: string, ctx: Context | object | undefined, renderFileOptions: RenderFileOptions): Generator<any> {
|
||||
const templates = (yield this._parseFile(file, renderFileOptions.sync, renderFileOptions.lookupType)) as Template[]
|
||||
return yield this._render(templates, ctx, renderFileOptions)
|
||||
}
|
||||
public async renderFile (file: string, ctx?: Context | object, renderFileOptions?: RenderFileOptions) {
|
||||
return toPromise(this._renderFile(file, ctx, { ...renderFileOptions, sync: false }))
|
||||
}
|
||||
public renderFileSync (file: string, ctx?: Context | object, renderFileOptions?: RenderFileOptions) {
|
||||
return toValueSync(this._renderFile(file, ctx, { ...renderFileOptions, sync: true }))
|
||||
}
|
||||
public async renderFileToNodeStream (file: string, scope?: object, renderOptions?: RenderOptions) {
|
||||
const templates = await this.parseFile(file)
|
||||
|
||||
@@ -7,8 +7,9 @@ import { Context } from '../context'
|
||||
import type { Liquid } from '../liquid'
|
||||
|
||||
export interface TagImplOptions {
|
||||
parse?: (this: Tag, token: TagToken, remainingTokens: TopLevelToken[]) => void;
|
||||
render: (this: Tag, ctx: Context, emitter: Emitter, hash: Record<string, any>) => TagRenderReturn;
|
||||
[key: string]: any
|
||||
parse?: (this: Tag & TagImplOptions, token: TagToken, remainingTokens: TopLevelToken[]) => void;
|
||||
render: (this: Tag & TagImplOptions, ctx: Context, emitter: Emitter, hash: Record<string, any>) => TagRenderReturn;
|
||||
}
|
||||
|
||||
export function createTagClass (options: TagImplOptions): TagClass {
|
||||
|
||||
Reference in New Issue
Block a user