feat: support jekyll-like include, see #433

This commit is contained in:
Harttle
2021-12-18 15:26:15 +08:00
committed by Harttle
parent 2c14a9541d
commit 23279a816a
8 changed files with 229 additions and 65 deletions
+16
View File
@@ -161,4 +161,20 @@ describe('Issues', function () {
const tpl = engine.parse('Welcome to {{ now | date: "%Y-%m-%d" }}!')
expect(engine.render(tpl, { now: new Date('2019/02/01') })).to.eventually.equal('Welcome to 2019-02-01')
})
it('#433 Support Jekyll-like includes', async () => {
const engine = new Liquid({
dynamicPartials: false,
root: '/tmp',
fs: {
readFileSync: (file: string) => file,
async readFile (file: string) { return `CONTENT for ${file}` },
existsSync (file: string) { return true },
async exists (file: string) { return true },
resolve: (dir: string, file: string) => dir + '/' + file
}
})
const tpl = engine.parse('{% include prefix/{{ my_variable | append: "-bar" }}/suffix %}')
const html = await engine.render(tpl, { my_variable: 'foo' })
expect(html).to.equal('CONTENT for /tmp/prefix/foo-bar/suffix')
})
})
+21 -3
View File
@@ -35,6 +35,14 @@ describe('tags/include', function () {
const html = await liquid.renderFile('/current.html', { name: 'foo.html' })
return expect(html).to.equal('barfoobar')
})
it('should allow escape in template string', async function () {
mock({
'/current.html': 'bar{% include "bar/{{name | append: \\".html\\"}}" %}bar',
'/bar/foo.html': 'foo'
})
const html = await liquid.renderFile('/current.html', { name: 'foo' })
return expect(html).to.equal('barfoobar')
})
it('should throw when not specified', function () {
mock({
@@ -155,7 +163,7 @@ describe('tags/include', function () {
})
describe('static partial', function () {
it('should support filename with extention', async function () {
it('should support filename with extension', async function () {
mock({
'/parent.html': 'X{% include child.html color:"red" %}Y',
'/child.html': 'child with {{color}}'
@@ -194,6 +202,16 @@ describe('tags/include', function () {
const html = await staticLiquid.renderFile('parent.html')
return expect(html).to.equal('Xchild with redY')
})
it('should support single liquid output', async function () {
mock({
'/parent.html': 'X{% include {{child}}, color:"red" %}Y',
'/child.html': 'child with {{color}}'
})
const staticLiquid = new Liquid({ dynamicPartials: false, root: '/' })
const html = await staticLiquid.renderFile('parent.html', { child: 'child.html' })
return expect(html).to.equal('Xchild with redY')
})
})
describe('sync support', function () {
it('should support quoted string', function () {
@@ -204,7 +222,7 @@ describe('tags/include', function () {
const html = liquid.renderFileSync('/current.html')
return expect(html).to.equal('barfoobar')
})
it('should support template string', function () {
it('should support variable', function () {
mock({
'/current.html': 'bar{% include name %}bar',
'/bar/foo.html': 'foo'
@@ -220,7 +238,7 @@ describe('tags/include', function () {
const html = liquid.renderFileSync('with.html')
return expect(html).to.equal('color:red, shape:rect')
})
it('should support filename with extention', function () {
it('should support filename with extension', function () {
mock({
'/parent.html': 'X{% include child.html color:"red" %}Y',
'/child.html': 'child with {{color}}'
+22 -4
View File
@@ -260,12 +260,15 @@ describe('tags/render', function () {
})
describe('static partial', function () {
let staticLiquid: Liquid
beforeEach(() => {
staticLiquid = new Liquid({ dynamicPartials: false, root: '/' })
})
it('should support filename with extension', async function () {
mock({
'/parent.html': 'X{% render child.html color:"red" %}Y',
'/child.html': 'child with {{color}}'
})
const staticLiquid = new Liquid({ dynamicPartials: false, root: '/' })
const html = await staticLiquid.renderFile('parent.html')
expect(html).to.equal('Xchild with redY')
})
@@ -275,7 +278,6 @@ describe('tags/render', function () {
'/parent.html': 'X{% render bar/./../foo/child.html %}Y',
'/foo/child.html': 'child'
})
const staticLiquid = new Liquid({ dynamicPartials: false, root: '/' })
const html = await staticLiquid.renderFile('parent.html')
expect(html).to.equal('XchildY')
})
@@ -285,7 +287,6 @@ describe('tags/render', function () {
'/parent.html': 'X{% render foo/child.html %}Y',
'/foo/child.html': 'child'
})
const staticLiquid = new Liquid({ dynamicPartials: false, root: '/' })
const html = await staticLiquid.renderFile('parent.html')
expect(html).to.equal('XchildY')
})
@@ -295,10 +296,27 @@ describe('tags/render', function () {
'/parent.html': 'X{% render child.html, color:"red" %}Y',
'/child.html': 'child with {{color}}'
})
const staticLiquid = new Liquid({ dynamicPartials: false, root: '/' })
const html = await staticLiquid.renderFile('parent.html')
expect(html).to.equal('Xchild with redY')
})
it('should support template string', async function () {
mock({
'/current.html': 'bar{% render bar/{{name}} %}bar',
'/bar/foo.html': 'foo'
})
const html = await staticLiquid.renderFile('/current.html', { name: 'foo.html' })
expect(html).to.equal('barfoobar')
})
it('should support filters in template string', async function () {
mock({
'/current.html': 'bar{% render bar/{{name | append: ".html"}} %}bar',
'/bar/foo.html': 'foo'
})
const html = await staticLiquid.renderFile('/current.html', { name: 'foo' })
expect(html).to.equal('barfoobar')
})
})
describe('sync support', function () {
it('should support quoted string', function () {