-
-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* style: remove unnecessary intermediate constant * feat: add inline comment tag * refactor: use readIdentifier when reading tag names * docs: add inline comment tag
- Loading branch information
Showing
9 changed files
with
198 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--- | ||
title: "# (inline comment)" | ||
--- | ||
|
||
{% since %}v9.38.0{% endsince %} | ||
|
||
Add comments to a Liquid template using an inline tag. Text enclosed in an inline comment tag will not be printed. | ||
|
||
Input | ||
```liquid | ||
Anything inside an inline comment tag will not be printed. | ||
{% # this is an inline comment %} | ||
But every line must start with a '#'. | ||
{% | ||
# this is a comment | ||
# that spans multiple lines | ||
%} | ||
``` | ||
|
||
Output | ||
```text | ||
Anything inside an inline comment tag will not be printed. | ||
But every line must start with a '#'. | ||
``` | ||
|
||
Inline comments are useful inside <a href="./liquid.html">`liquid`</a> tags too. | ||
|
||
```liquid | ||
{% liquid | ||
# required args | ||
assign product = collection.products.first | ||
# optional args | ||
assign should_show_border = should_show_border | default: true | ||
assign should_highlight = should_highlight | default: false | ||
%} | ||
``` | ||
|
||
But they don't work well for commenting out blocks of Liquid code. The <a href="./comment.html">`comment`</a> block tag is the better option when you need to temporarily stop other tags from being executed. | ||
|
||
Input | ||
```liquid | ||
{%- # {% echo 'Welcome to LiquidJS!' %} -%} | ||
{% comment %}{% echo 'Welcome to LiquidJS!' %}{% endcomment %} | ||
``` | ||
|
||
Output | ||
```text | ||
-%} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { TagToken } from '../../tokens/tag-token' | ||
import { TopLevelToken } from '../../tokens/toplevel-token' | ||
import { TagImplOptions } from '../../template/tag/tag-impl-options' | ||
|
||
export default { | ||
parse: function (tagToken: TagToken, remainTokens: TopLevelToken[]) { | ||
if (tagToken.args.search(/\n\s*[^#\s]/g) !== -1) { | ||
throw new Error('every line of an inline comment must start with a \'#\' character') | ||
} | ||
} | ||
} as TagImplOptions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { Liquid } from '../../../../src/liquid' | ||
import { expect, use } from 'chai' | ||
import * as chaiAsPromised from 'chai-as-promised' | ||
|
||
use(chaiAsPromised) | ||
|
||
describe('tags/inline-comment', function () { | ||
const liquid = new Liquid() | ||
it('should ignore plain string', async function () { | ||
const src = 'My name is {% # super %} Shopify.' | ||
const html = await liquid.parseAndRender(src) | ||
return expect(html).to.equal('My name is Shopify.') | ||
}) | ||
it('should ignore output tokens', async function () { | ||
const src = '{% #\n{{ foo}} \n %}' | ||
const html = await liquid.parseAndRender(src) | ||
return expect(html).to.equal('') | ||
}) | ||
it('should support whitespace control', async function () { | ||
const src = '{%- # some comment \n -%}\nfoo' | ||
const html = await liquid.parseAndRender(src) | ||
return expect(html).to.equal('foo') | ||
}) | ||
it('should handle hash without trailing whitespace', async function () { | ||
const src = '{% #some comment %}' | ||
const html = await liquid.parseAndRender(src) | ||
return expect(html).to.equal('') | ||
}) | ||
it('should handle hash without leading whitespace', async function () { | ||
const src = '{%#some comment %}' | ||
const html = await liquid.parseAndRender(src) | ||
return expect(html).to.equal('') | ||
}) | ||
it('should handle empty comment', async function () { | ||
const src = '{%#%}' | ||
const html = await liquid.parseAndRender(src) | ||
return expect(html).to.equal('') | ||
}) | ||
it('should support multiple lines', async function () { | ||
const src = [ | ||
'{%-', | ||
' # spread inline comments', | ||
' # over multiple lines', | ||
'-%}' | ||
].join('\n') | ||
const html = await liquid.parseAndRender(src) | ||
return expect(html).to.equal('') | ||
}) | ||
it('should enforce leading hashes', async function () { | ||
const src = [ | ||
'{%-', | ||
' # spread inline comments', | ||
' over multiple lines', | ||
'-%}' | ||
].join('\n') | ||
return expect(liquid.parseAndRender(src)) | ||
.to.be.rejectedWith(/every line of an inline comment must start with a '#' character/) | ||
}) | ||
describe('sync support', function () { | ||
it('should ignore plain string', function () { | ||
const src = 'My name is {% # super %} Shopify.' | ||
const html = liquid.parseAndRenderSync(src) | ||
return expect(html).to.equal('My name is Shopify.') | ||
}) | ||
}) | ||
describe('liquid tag', function () { | ||
it('should treat lines starting with a hash as a comment', async function () { | ||
const src = [ | ||
'{% liquid ', | ||
' # first comment line', | ||
' # second comment line', | ||
'', | ||
' # another comment line', | ||
' echo \'Hello \'', | ||
'', | ||
' # more comments', | ||
' echo \'goodbye\'', | ||
'-%}' | ||
].join('\n') | ||
const html = await liquid.parseAndRender(src) | ||
return expect(html).to.equal('Hello goodbye') | ||
}) | ||
it('should handle lots of hashes', async function () { | ||
const src = [ | ||
'{% liquid', | ||
' ##########################', | ||
' # spread inline comments #', | ||
' ##########################', | ||
'-%}' | ||
].join('\n') | ||
const html = await liquid.parseAndRender(src) | ||
return expect(html).to.equal('') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters