diff --git a/src/posthtml/plugins/envTags.js b/src/posthtml/plugins/envTags.js index 02990446..10ab25d6 100644 --- a/src/posthtml/plugins/envTags.js +++ b/src/posthtml/plugins/envTags.js @@ -7,21 +7,38 @@ const plugin = (env => tree => { return node } - const tagEnv = node.tag.split(':').pop() + // Handle tags (render only if current env matches) + if ( + typeof node.tag === 'string' + && node.tag.startsWith('env:') + ) { + const tagEnv = node.tag.slice(4) // Remove 'env:' prefix - // Tag targets current env, remove it and return its content - if (node.tag === `env:${env}`) { - node.tag = false + if (tagEnv === '' || tagEnv !== env) { + // No env specified or tag doesn't target current env, remove everything + node.content = [] + node.tag = false + } else { + // Tag targets current env, remove tag and keep content + node.tag = false + } } - // Tag doesn't target current env, remove it completely + // Handle tags (render only if current env does not match) if ( typeof node.tag === 'string' - && node.tag.startsWith('env:') - && tagEnv !== env + && node.tag.startsWith('not-env:') ) { - node.content = [] - node.tag = false + const tagEnv = node.tag.slice(8) // Remove 'not-env:' prefix + + if (tagEnv === '' || tagEnv === env) { + // No env specified or tag targets current env, remove everything + node.content = [] + node.tag = false + } else { + // Tag doesn't target current env, remove tag and keep content + node.tag = false + } } return node diff --git a/test/render.test.js b/test/render.test.js index 12f2de28..0f9fa858 100644 --- a/test/render.test.js +++ b/test/render.test.js @@ -93,43 +93,6 @@ describe.concurrent('Render', () => { expect(inProduction).toBe('
') }) - test(' tags', async () => { - const source = ` - {{ page.env }} - {{ page.env }} - ignore - test - ` - - const { html: inDev } = await render(source) - - const { html: inProduction } = await render(source, { - env: 'production' - }) - - // we don't pass `env` to the page object so it remains as-is - expect(cleanString(inDev)).toBe('{{ page.env }} ignore') - expect(inProduction.trim()).toBe('production\n ignore') - }) - - test('fetch component', async () => { - const { html } = await render(` - - - {{ undefinedVariable }} - {{ user.name + (loop.last ? '' : ', ') }} - @{{ ignored }} - - - `, { - components: { - folders: ['test/stubs/components'], - } - }) - - expect(cleanString(html)).toBe('

Results

{{ undefinedVariable }} Leanne Graham, Ervin Howell {{ ignored }}') - }) - test('uses expressions options', async () => { const { html } = await render(`