refactor: more consistent tags to make it easier to iterate over, #524

This commit is contained in:
Jun Yang
2022-11-27 17:29:44 +08:00
parent 4e1a30a20c
commit 1380ac931a
19 changed files with 225 additions and 82 deletions
+9
View File
@@ -0,0 +1,9 @@
# LiquidJS for Node.js
## Get Started
```bash
cd demo/nodejs
npm install
npm start
```
+25
View File
@@ -0,0 +1,25 @@
import { Output, Template, Tag } from 'liquidjs'
import { isLayoutTag, isIfTag, isUnlessTag, isLiquidTag, isCaseTag, isCaptureTag, isTablerowTag, isForTag } from './type-guards'
/**
* iterate over all `{{ output }}`
*/
export function * getOutputs (templates: Template[]): Generator<Output, void> {
for (const template of templates) {
if (template instanceof Tag) {
if (isIfTag(template) || isUnlessTag(template) || isCaseTag(template)) {
for (const branch of template.branches) {
yield * getOutputs(branch.templates)
}
yield * getOutputs(template.elseTemplates)
} else if (isForTag(template)) {
yield * getOutputs(template.templates)
yield * getOutputs(template.elseTemplates)
} else if (isLiquidTag(template) || isCaptureTag(template) || isTablerowTag(template) || isLayoutTag(template)) {
yield * getOutputs(template.templates)
}
} else if (template instanceof Output) {
yield template
}
}
}
+16
View File
@@ -0,0 +1,16 @@
import { Liquid } from 'liquidjs'
import { getOutputs } from './get-outputs'
const engine = new Liquid({
root: __dirname,
extname: '.liquid'
})
const templates = engine.parseFileSync('todolist')
for (const output of getOutputs(templates)) {
const token = output.token
const [line, col] = token.getPosition()
const text = token.getText()
console.log(`[${line}:${col}] ${text}`)
}
+17
View File
@@ -0,0 +1,17 @@
{
"name": "liquidjs-demo-template",
"private": true,
"description": "liquid template parsing",
"main": "index.ts",
"scripts": {
"start": "ts-node index.ts"
},
"dependencies": {
"liquidjs": "latest",
"ts-node": "^8.10.2",
"typescript": "^4.2.4"
},
"devDependencies": {
"@types/node": "^14.14.37"
}
}
+13
View File
@@ -0,0 +1,13 @@
{% layout 'html.liquid' %}
<h1>{{ title | capitalize }}</h1>
<a>
{% if authed %} Sign out {{ username }}
{% else %} Sign in
{% endif %}
</a>
<ul>
{% for todo in todos %}
<p>{{ todo }}</p>
{% endfor %}
</ul>
+8
View File
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"downlevelIteration": true,
"types": [
"node"
]
}
}
+33
View File
@@ -0,0 +1,33 @@
import { LayoutTag, ForTag, LiquidTag, CaptureTag, CaseTag, UnlessTag, TablerowTag, Tag, IfTag } from 'liquidjs'
export function isIfTag (tag: Tag): tag is IfTag {
return tag.name === 'if'
}
export function isUnlessTag (tag: Tag): tag is UnlessTag {
return tag.name === 'unless'
}
export function isLiquidTag (tag: Tag): tag is LiquidTag {
return tag.name === 'liquid'
}
export function isCaseTag (tag: Tag): tag is CaseTag {
return tag.name === 'case'
}
export function isCaptureTag (tag: Tag): tag is CaptureTag {
return tag.name === 'capture'
}
export function isTablerowTag (tag: Tag): tag is TablerowTag {
return tag.name === 'tablerow'
}
export function isForTag (tag: Tag): tag is ForTag {
return tag.name === 'for'
}
export function isLayoutTag (tag: Tag): tag is LayoutTag {
return tag.name === 'layout'
}