mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-17 05:10:40 -07:00
fix: better index.d.ts and a demo
The demo is located at demo/typescript/index.ts , working on #98
This commit is contained in:
@@ -12,3 +12,4 @@ build/
|
|||||||
|
|
||||||
# editors
|
# editors
|
||||||
.*.swp
|
.*.swp
|
||||||
|
.vscode
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
const Liquid = require('../..')
|
const Liquid = require('liquidjs')
|
||||||
|
|
||||||
const engine = new Liquid({
|
const engine = new Liquid({
|
||||||
root: __dirname,
|
root: __dirname,
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import Liquid, {isTruthy} from 'liquidjs'
|
||||||
|
|
||||||
|
const engine = new Liquid({
|
||||||
|
root: __dirname,
|
||||||
|
extname: '.liquid'
|
||||||
|
})
|
||||||
|
const ctx = {
|
||||||
|
todos: ['fork and clone', 'make it better', 'make a pull request'],
|
||||||
|
title: 'Welcome to liquidjs!'
|
||||||
|
}
|
||||||
|
|
||||||
|
// console.log('isTruthy:', isTruthy('a string here'));
|
||||||
|
engine.renderFile('todolist', ctx).then(console.log)
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<ul>
|
||||||
|
{% for todo in todos %}
|
||||||
|
<li>{{forloop.index}} - {{todo}}</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
Generated
+2219
-2219
File diff suppressed because it is too large
Load Diff
Vendored
+65
-77
@@ -1,81 +1,69 @@
|
|||||||
declare function Liquid(): Liquid.LiquidEngine;
|
export as namespace Liquid;
|
||||||
|
|
||||||
declare namespace Liquid {
|
export function isTruthy(val: any): boolean;
|
||||||
export interface LiquidEngine {
|
export function isFalsy(val: any): boolean;
|
||||||
private init(tag, filter, options): LiquidEngine
|
export function evalExp(exp: string, scope: any): any;
|
||||||
private respectCache(key, getter): Promise<any>
|
export function evalValue(str: string, scope: any): any;
|
||||||
private evalValue: typeof Liquid.evalValue
|
|
||||||
|
|
||||||
public parse(html: string, filepath: string): Liquid.Template
|
export default class Liquid {
|
||||||
public render(tpl: Template, ctx: any, opts: Options): Promise<string>
|
constructor(options?: Options);
|
||||||
public parseAndRender(html: string, ctx: any, opts: Options): Promise<string>
|
private init(tag, filter, options): Liquid;
|
||||||
public getTemplate(file: string, root: string): Promise<Liquid.Template>
|
private respectCache(key, getter): Promise<any>
|
||||||
public renderFile(file: string, ctx: any, opts: Options): Promise<string>
|
parse(html: string, filepath?: string): Liquid.Template
|
||||||
public registerFilter(name: string, filter: Filter): void
|
render(tpl: Template, ctx: any, opts?: Options): Promise<string>
|
||||||
public registerTag(name: string, tag: Tag): void
|
parseAndRender(html: string, ctx: any, opts?: Options): Promise<string>
|
||||||
public express(opts: Options): any
|
renderFile(file: string, ctx: any, opts?: Options): Promise<string>
|
||||||
}
|
getTemplate(file: string, root: string): Promise<Liquid.Template>
|
||||||
|
registerFilter(name: string, filter: Filter): void
|
||||||
export interface Template { }
|
registerTag(name: string, tag: Tag): void
|
||||||
export interface Options {
|
express(opts: Options): any
|
||||||
/** `root` is a directory or an array of directories to resolve layouts and includes, as well as the filename passed in when calling `.renderFile()`. If an array, the files are looked up in the order they occur in the array. Defaults to `["."]`*/
|
|
||||||
root: string | string[]
|
|
||||||
/** `extname` is used to lookup the template file when filepath doesn't include an extension name. Eg: setting to `".html"` will allow including file by basename. Defaults to `""`. */
|
|
||||||
extname: string
|
|
||||||
/** `cache` indicates whether or not to cache resolved templates. Defaults to `false`. */
|
|
||||||
cache: boolean
|
|
||||||
/** `dynamicPartials`: if set, treat `<filepath>` parameter in `{%include filepath %}`, `{%layout filepath%}` as a variable, otherwise as a literal value. Defaults to `true`. */
|
|
||||||
dynamicPartials: boolean
|
|
||||||
/** `strict_filters` is used to enable strict filter existence. If set to `false`, undefined filters will be rendered as empty string. Otherwise, undefined filters will cause an exception. Defaults to `false`. */
|
|
||||||
strict_filters: boolean
|
|
||||||
/** `trim_tag_right` is used to strip blank characters (including ` `, `\t`, and `\r`) from the right of tags (`{% %}`) until `\n` (inclusive). Defaults to `false`. */
|
|
||||||
trim_tag_right: boolean
|
|
||||||
/** `trim_tag_left` is similiar to `trim_tag_right`, whereas the `\n` is exclusive. Defaults to `false`. See Whitespace Control for details. */
|
|
||||||
trim_tag_left: boolean
|
|
||||||
/** ``trim_value_right` is used to strip blank characters (including ` `, `\t`, and `\r`) from the right of values (`{{ }}`) until `\n` (inclusive). Defaults to `false`. */
|
|
||||||
trim_value_right: boolean
|
|
||||||
/** `trim_value_left` is similiar to `trim_value_right`, whereas the `\n` is exclusive. Defaults to `false`. See Whitespace Control for details. */
|
|
||||||
trim_value_left: boolean
|
|
||||||
/** `greedy` is used to specify whether `trim_left`/`trim_right` is greedy. When set to `true`, all consecutive blank characters including `\n` will be trimed regardless of line breaks. Defaults to `true`. */
|
|
||||||
greedy: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Tag {
|
|
||||||
// TODO: tokens type; this type
|
|
||||||
parse(this: any, tagToken: any, remainTokens: any): void
|
|
||||||
render(this: any, scope: any, hash: any): void
|
|
||||||
}
|
|
||||||
export type Filter = (...args?: any) => string
|
|
||||||
|
|
||||||
export function isTruthy(val: any): boolean;
|
|
||||||
export function isFalsy(val: any): boolean;
|
|
||||||
export function evalExp(exp: string, scope: any): any;
|
|
||||||
export function evalValue(str: string, scope: any): any;
|
|
||||||
export const Types: Types;
|
|
||||||
|
|
||||||
declare class LiquidError extends Error {
|
|
||||||
public input: string
|
|
||||||
public line: number
|
|
||||||
public file: string
|
|
||||||
}
|
|
||||||
export class ParseError extends LiquidError {
|
|
||||||
public originalError: Error
|
|
||||||
}
|
|
||||||
export class TokenizationError extends LiquidError { }
|
|
||||||
export class RenderBreakError extends LiquidError { }
|
|
||||||
export class AssertionError extends LiquidError { }
|
|
||||||
|
|
||||||
export interface Types {
|
|
||||||
ParseError: typeof ParseError,
|
|
||||||
TokenizationError: typeof TokenizationError,
|
|
||||||
RenderBreakError: typeof RenderBreakError,
|
|
||||||
AssertionError: typeof AssertionError,
|
|
||||||
AssignScope: any,
|
|
||||||
CaptureScope: any,
|
|
||||||
IncrementScope: any,
|
|
||||||
DecrementScope: any
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export = Liquid;
|
export interface Options {
|
||||||
export default Liquid;
|
/** `root` is a directory or an array of directories to resolve layouts and includes, as well as the filename passed in when calling `.renderFile()`. If an array, the files are looked up in the order they occur in the array. Defaults to `["."]`*/
|
||||||
|
root?: string | string[]
|
||||||
|
/** `extname` is used to lookup the template file when filepath doesn't include an extension name. Eg: setting to `".html"` will allow including file by basename. Defaults to `""`. */
|
||||||
|
extname?: string
|
||||||
|
/** `cache` indicates whether or not to cache resolved templates. Defaults to `false`. */
|
||||||
|
cache?: boolean
|
||||||
|
/** `dynamicPartials`: if set, treat `<filepath>` parameter in `{%include filepath %}`, `{%layout filepath%}` as a variable, otherwise as a literal value. Defaults to `true`. */
|
||||||
|
dynamicPartials?: boolean
|
||||||
|
/** `strict_filters` is used to enable strict filter existence. If set to `false`, undefined filters will be rendered as empty string. Otherwise, undefined filters will cause an exception. Defaults to `false`. */
|
||||||
|
strict_filters?: boolean
|
||||||
|
/** `trim_tag_right` is used to strip blank characters (including ` `, `\t`, and `\r`) from the right of tags (`{% %}`) until `\n` (inclusive). Defaults to `false`. */
|
||||||
|
trim_tag_right?: boolean
|
||||||
|
/** `trim_tag_left` is similar to `trim_tag_right`, whereas the `\n` is exclusive. Defaults to `false`. See Whitespace Control for details. */
|
||||||
|
trim_tag_left?: boolean
|
||||||
|
/** ``trim_value_right` is used to strip blank characters (including ` `, `\t`, and `\r`) from the right of values (`{{ }}`) until `\n` (inclusive). Defaults to `false`. */
|
||||||
|
trim_value_right?: boolean
|
||||||
|
/** `trim_value_left` is similar to `trim_value_right`, whereas the `\n` is exclusive. Defaults to `false`. See Whitespace Control for details. */
|
||||||
|
trim_value_left?: boolean
|
||||||
|
/** `greedy` is used to specify whether `trim_left`/`trim_right` is greedy. When set to `true`, all consecutive blank characters including `\n` will be trimed regardless of line breaks. Defaults to `true`. */
|
||||||
|
greedy?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Template { }
|
||||||
|
|
||||||
|
export interface Tag {
|
||||||
|
parse(this: any, tagToken: any, remainTokens: any): void
|
||||||
|
render(this: any, scope: any, hash: any): void
|
||||||
|
}
|
||||||
|
export type Filter = (...args: any) => string
|
||||||
|
|
||||||
|
declare namespace Types {
|
||||||
|
class LiquidError extends Error {
|
||||||
|
input: string
|
||||||
|
line: number
|
||||||
|
file: string
|
||||||
|
}
|
||||||
|
class ParseError extends LiquidError {
|
||||||
|
originalError: Error
|
||||||
|
}
|
||||||
|
class TokenizationError extends LiquidError {}
|
||||||
|
class RenderBreakError extends LiquidError {}
|
||||||
|
class AssertionError extends LiquidError {}
|
||||||
|
class AssignScope {}
|
||||||
|
class CaptureScope {}
|
||||||
|
class IncrementScope {}
|
||||||
|
class DecrementScope {}
|
||||||
|
}
|
||||||
|
|||||||
@@ -117,6 +117,7 @@ export default function Liquid (options) {
|
|||||||
return engine
|
return engine
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Liquid.default = Liquid
|
||||||
Liquid.isTruthy = isTruthy
|
Liquid.isTruthy = isTruthy
|
||||||
Liquid.isFalsy = isFalsy
|
Liquid.isFalsy = isFalsy
|
||||||
Liquid.evalExp = evalExp
|
Liquid.evalExp = evalExp
|
||||||
|
|||||||
Reference in New Issue
Block a user