mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
fix: skip root check for renderFile()
This commit is contained in:
+4
-4
@@ -23,7 +23,7 @@ export class Loader {
|
||||
public * lookup (file: string, type: LookupType, sync?: boolean, currentFile?: string) {
|
||||
const { fs } = this.options
|
||||
const dirs = this.options[type]
|
||||
for (const filepath of this.candidates(file, dirs, currentFile)) {
|
||||
for (const filepath of this.candidates(file, dirs, currentFile, type !== LookupType.Root)) {
|
||||
if (sync ? fs.existsSync(filepath) : yield fs.exists(filepath)) return filepath
|
||||
}
|
||||
throw this.lookupError(file, dirs)
|
||||
@@ -37,12 +37,12 @@ export class Loader {
|
||||
return path.startsWith('./') || path.startsWith('../')
|
||||
}
|
||||
|
||||
public * candidates (file: string, dirs: string[], currentFile?: string) {
|
||||
public * candidates (file: string, dirs: string[], currentFile?: string, enforceRoot?: boolean) {
|
||||
const { fs, extname } = this.options
|
||||
if (this.shouldLoadRelative(file) && currentFile) {
|
||||
const referenced = fs.resolve(this.dirname(currentFile), file, extname)
|
||||
for (const dir of dirs) {
|
||||
if (referenced.startsWith(dir)) {
|
||||
if (!enforceRoot || referenced.startsWith(dir)) {
|
||||
// the relatively referenced file is within one of root dirs
|
||||
yield referenced
|
||||
return
|
||||
@@ -51,7 +51,7 @@ export class Loader {
|
||||
}
|
||||
for (const dir of dirs) {
|
||||
const referenced = fs.resolve(dir, file, extname)
|
||||
if (referenced.startsWith(dir)) {
|
||||
if (!enforceRoot || referenced.startsWith(dir)) {
|
||||
yield referenced
|
||||
}
|
||||
}
|
||||
|
||||
+7
-2
@@ -5,8 +5,13 @@ import { stat, statSync, readFile as nodeReadFile, readFileSync as nodeReadFileS
|
||||
const statAsync = _.promisify(stat)
|
||||
const readFileAsync = _.promisify<string, string, string>(nodeReadFile)
|
||||
|
||||
export function exists (filepath: string) {
|
||||
return statAsync(filepath).then(() => true).catch(() => false)
|
||||
export async function exists (filepath: string) {
|
||||
try {
|
||||
await statAsync(filepath)
|
||||
return true
|
||||
} catch (err) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
export function readFile (filepath: string) {
|
||||
return readFileAsync(filepath, 'utf8')
|
||||
|
||||
@@ -133,13 +133,13 @@ export const defaultOptions: NormalizedFullOptions = {
|
||||
export function normalize (options?: LiquidOptions): NormalizedOptions {
|
||||
options = options || {}
|
||||
if (options.hasOwnProperty('root')) {
|
||||
options.root = normalizeStringArray(options.root)
|
||||
options.root = normalizeDirectoryList(options.root)
|
||||
}
|
||||
if (options.hasOwnProperty('partials')) {
|
||||
options.partials = normalizeStringArray(options.partials)
|
||||
options.partials = normalizeDirectoryList(options.partials)
|
||||
}
|
||||
if (options.hasOwnProperty('layouts')) {
|
||||
options.layouts = normalizeStringArray(options.layouts)
|
||||
options.layouts = normalizeDirectoryList(options.layouts)
|
||||
}
|
||||
if (options.hasOwnProperty('cache')) {
|
||||
let cache: Cache<Template[]> | undefined
|
||||
@@ -165,8 +165,9 @@ export function applyDefault (options: NormalizedOptions): NormalizedFullOptions
|
||||
return fullOptions
|
||||
}
|
||||
|
||||
export function normalizeStringArray (value: any): string[] {
|
||||
if (_.isArray(value)) return value as string[]
|
||||
if (_.isString(value)) return [value as string]
|
||||
return []
|
||||
export function normalizeDirectoryList (value: any): string[] {
|
||||
let list: string[] = []
|
||||
if (_.isArray(value)) list = value
|
||||
if (_.isString(value)) list = [value]
|
||||
return list.map(str => fs.resolve(str, '.', '')).map(str => str[str.length - 1] !== '/' ? str + '/' : str)
|
||||
}
|
||||
|
||||
+2
-2
@@ -10,7 +10,7 @@ import builtinTags from './builtin/tags'
|
||||
import * as builtinFilters from './builtin/filters'
|
||||
import { TagMap } from './template/tag/tag-map'
|
||||
import { FilterMap } from './template/filter/filter-map'
|
||||
import { LiquidOptions, normalizeStringArray, NormalizedFullOptions, applyDefault, normalize } from './liquid-options'
|
||||
import { LiquidOptions, normalizeDirectoryList, NormalizedFullOptions, applyDefault, normalize } from './liquid-options'
|
||||
import { FilterImplOptions } from './template/filter/filter-impl-options'
|
||||
import { toPromise, toValue } from './util/async'
|
||||
|
||||
@@ -116,7 +116,7 @@ export class Liquid {
|
||||
return function (this: any, filePath: string, ctx: object, callback: (err: Error | null, rendered: string) => void) {
|
||||
if (firstCall) {
|
||||
firstCall = false
|
||||
self.options.root.unshift(...normalizeStringArray(this.root))
|
||||
self.options.root.unshift(...normalizeDirectoryList(this.root))
|
||||
}
|
||||
self.renderFile(filePath, ctx).then(html => callback(null, html) as any, callback as any)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user