Skip to content

Commit a7ede6e

Browse files
authored
Merge pull request #1599 from maizzle/feat/not-env
feat: add `not-env` tag
2 parents 6658b64 + b07b9ea commit a7ede6e

File tree

4 files changed

+98
-58
lines changed

4 files changed

+98
-58
lines changed

src/posthtml/plugins/envTags.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,38 @@ const plugin = (env => tree => {
77
return node
88
}
99

10-
const tagEnv = node.tag.split(':').pop()
10+
// Handle <env:?> tags (render only if current env matches)
11+
if (
12+
typeof node.tag === 'string'
13+
&& node.tag.startsWith('env:')
14+
) {
15+
const tagEnv = node.tag.slice(4) // Remove 'env:' prefix
1116

12-
// Tag targets current env, remove it and return its content
13-
if (node.tag === `env:${env}`) {
14-
node.tag = false
17+
if (tagEnv === '' || tagEnv !== env) {
18+
// No env specified or tag doesn't target current env, remove everything
19+
node.content = []
20+
node.tag = false
21+
} else {
22+
// Tag targets current env, remove tag and keep content
23+
node.tag = false
24+
}
1525
}
1626

17-
// Tag doesn't target current env, remove it completely
27+
// Handle <not-env:?> tags (render only if current env does not match)
1828
if (
1929
typeof node.tag === 'string'
20-
&& node.tag.startsWith('env:')
21-
&& tagEnv !== env
30+
&& node.tag.startsWith('not-env:')
2231
) {
23-
node.content = []
24-
node.tag = false
32+
const tagEnv = node.tag.slice(8) // Remove 'not-env:' prefix
33+
34+
if (tagEnv === '' || tagEnv === env) {
35+
// No env specified or tag targets current env, remove everything
36+
node.content = []
37+
node.tag = false
38+
} else {
39+
// Tag doesn't target current env, remove tag and keep content
40+
node.tag = false
41+
}
2542
}
2643

2744
return node

test/render.test.js

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -93,43 +93,6 @@ describe.concurrent('Render', () => {
9393
expect(inProduction).toBe('<div title="production"></div>')
9494
})
9595

96-
test('<env:> tags', async () => {
97-
const source = `
98-
<env:local>{{ page.env }}</env:local>
99-
<env:production>{{ page.env }}</env:production>
100-
<fake:production>ignore</fake:production>
101-
<env:>test</env:>
102-
`
103-
104-
const { html: inDev } = await render(source)
105-
106-
const { html: inProduction } = await render(source, {
107-
env: 'production'
108-
})
109-
110-
// we don't pass `env` to the page object so it remains as-is
111-
expect(cleanString(inDev)).toBe('{{ page.env }} <fake:production>ignore</fake:production>')
112-
expect(inProduction.trim()).toBe('production\n <fake:production>ignore</fake:production>')
113-
})
114-
115-
test('fetch component', async () => {
116-
const { html } = await render(`
117-
<x-list>
118-
<fetch url="test/stubs/data.json">
119-
{{ undefinedVariable }}
120-
<each loop="user in response">{{ user.name + (loop.last ? '' : ', ') }}</each>
121-
@{{ ignored }}
122-
</fetch>
123-
</x-list>
124-
`, {
125-
components: {
126-
folders: ['test/stubs/components'],
127-
}
128-
})
129-
130-
expect(cleanString(html)).toBe('<h1>Results</h1> {{ undefinedVariable }} Leanne Graham, Ervin Howell {{ ignored }}')
131-
})
132-
13396
test('uses expressions options', async () => {
13497
const { html } = await render(`
13598
<script locals>

test/tags.test.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { describe, expect, test } from 'vitest'
2+
import { render } from '../src/generators/render.js'
3+
import { run as useTransformers } from '../src/transformers/index.js'
4+
5+
const cleanString = (str) => str.replace(/\s+/g, ' ').trim()
6+
7+
describe.concurrent('Tags', () => {
8+
test('fetch component', async () => {
9+
const { html } = await render(`
10+
<x-list>
11+
<fetch url="test/stubs/data.json">
12+
{{ undefinedVariable }}
13+
<each loop="user in response">{{ user.name + (loop.last ? '' : ', ') }}</each>
14+
@{{ ignored }}
15+
</fetch>
16+
</x-list>
17+
`, {
18+
components: {
19+
folders: ['test/stubs/components'],
20+
}
21+
})
22+
23+
expect(cleanString(html)).toBe('<h1>Results</h1> {{ undefinedVariable }} Leanne Graham, Ervin Howell {{ ignored }}')
24+
})
25+
26+
test('<template> tags', async () => {
27+
const { html } = await useTransformers(`
28+
<template uppercase>test</template>
29+
<template preserve>test</template>
30+
`)
31+
32+
expect(cleanString(html)).toBe('TEST <template>test</template>')
33+
})
34+
35+
test('<env> tags', async () => {
36+
const source = `
37+
<env:local>{{ page.env }}</env:local>
38+
<env:production>{{ page.env }}</env:production>
39+
<fake:production>ignore</fake:production>
40+
<env:>test</env:>
41+
`
42+
43+
const { html: inDev } = await render(source)
44+
45+
const { html: inProduction } = await render(source, {
46+
env: 'production'
47+
})
48+
49+
// we don't pass `env` to the page object so it remains as-is
50+
expect(cleanString(inDev)).toBe('{{ page.env }} <fake:production>ignore</fake:production>')
51+
expect(inProduction.trim()).toBe('production\n <fake:production>ignore</fake:production>')
52+
})
53+
54+
test('<not-env> tags', async () => {
55+
const source = `
56+
<not-env:local>{{ page.env }}</not-env:local>
57+
<not-env:production>{{ page.env }}</not-env:production>
58+
<fake:production>ignore</fake:production>
59+
<not-env:>test</not-env:>
60+
`
61+
62+
const { html: inDev } = await render(source)
63+
64+
const { html: inProduction } = await render(source, {
65+
env: 'production'
66+
})
67+
68+
// we don't pass `env` to the page object so it remains as-is
69+
expect(cleanString(inDev)).toBe('{{ page.env }} <fake:production>ignore</fake:production>')
70+
expect(cleanString(inProduction)).toBe('production <fake:production>ignore</fake:production>')
71+
})
72+
})

test/transformers/customTags.test.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)