Skip to content

Commit ac1d12f

Browse files
Liquid: Added Markup support, missing tokens, and other improvements (#2950)
1 parent abab910 commit ac1d12f

16 files changed

+921
-110
lines changed

components.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components.json

+1
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,7 @@
732732
},
733733
"liquid": {
734734
"title": "Liquid",
735+
"require": "markup-templating",
735736
"owner": "cinhtau"
736737
},
737738
"lisp": {

components/prism-liquid.js

+57-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,61 @@
11
Prism.languages.liquid = {
2-
'keyword': /\b(?:comment|endcomment|if|elsif|else|endif|unless|endunless|for|endfor|case|endcase|when|in|break|assign|continue|limit|offset|range|reversed|raw|endraw|capture|endcapture|tablerow|endtablerow)\b/,
3-
'number': /\b0b[01]+\b|\b0x(?:\.[\da-fp-]+|[\da-f]+(?:\.[\da-fp-]+)?)\b|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e[+-]?\d+)?[df]?/i,
4-
'operator': {
5-
pattern: /(^|[^.])(?:\+[+=]?|-[-=]?|!=?|<<?=?|>>?>?=?|==?|&[&=]?|\|[|=]?|\*=?|\/=?|%=?|\^=?|[?:~])/m,
2+
'comment': {
3+
pattern: /(^\{%\s*comment\s*%\})[\s\S]+(?=\{%\s*endcomment\s*%\}$)/,
64
lookbehind: true
75
},
8-
'function': {
9-
pattern: /(^|[\s;|&])(?:append|prepend|capitalize|cycle|cols|increment|decrement|abs|at_least|at_most|ceil|compact|concat|date|default|divided_by|downcase|escape|escape_once|first|floor|join|last|lstrip|map|minus|modulo|newline_to_br|plus|remove|remove_first|replace|replace_first|reverse|round|rstrip|size|slice|sort|sort_natural|split|strip|strip_html|strip_newlines|times|truncate|truncatewords|uniq|upcase|url_decode|url_encode|include|paginate)(?=$|[\s;|&])/,
10-
lookbehind: true
11-
}
6+
'delimiter': {
7+
pattern: /^\{(?:\{\{|[%\{])-?|-?(?:\}\}|[%\}])\}$/,
8+
alias: 'punctuation'
9+
},
10+
'string': {
11+
pattern: /"[^"]*"|'[^']*'/,
12+
greedy: true
13+
},
14+
'keyword': /\b(?:as|assign|break|continue|cycle|decrement|echo|else|elsif|(?:end)?(?:capture|case|comment|for|form|if|paginate|style|raw|tablerow|unless)|in|include|increment|limit|liquid|offset|range|render|reversed|section|when|with)\b/,
15+
'function': [
16+
{
17+
pattern: /(\|\s*)\w+/,
18+
lookbehind: true,
19+
alias: 'filter'
20+
},
21+
{
22+
// array functions
23+
pattern: /(\.\s*)(?:first|last|size)/,
24+
lookbehind: true
25+
}
26+
],
27+
'boolean': /\b(?:true|false|nil)\b/,
28+
'range': {
29+
pattern: /\.\./,
30+
alias: 'operator'
31+
},
32+
// https://github.com/Shopify/liquid/blob/698f5e0d967423e013f6169d9111bd969bd78337/lib/liquid/lexer.rb#L21
33+
'number': /\b\d+(?:\.\d+)?\b/,
34+
'operator': /[!=]=|<>|[<>]=?|[|?:=-]|\b(?:and|or|contains(?=\s))\b/,
35+
'punctuation': /[.,\[\]()]/
1236
};
37+
38+
Prism.hooks.add('before-tokenize', function (env) {
39+
var liquidPattern = /\{%\s*comment\s*%\}[\s\S]*?\{%\s*endcomment\s*%\}|\{(?:%[\s\S]*?%|\{\{[\s\S]*?\}\}|\{[\s\S]*?\})\}/g;
40+
var insideRaw = false;
41+
42+
Prism.languages['markup-templating'].buildPlaceholders(env, 'liquid', liquidPattern, function (match) {
43+
var tagMatch = /^\{%-?\s*(\w+)/.exec(match);
44+
if (tagMatch) {
45+
var tag = tagMatch[1];
46+
if (tag === 'raw' && !insideRaw) {
47+
insideRaw = true;
48+
return true;
49+
} else if (tag === 'endraw') {
50+
insideRaw = false;
51+
return true;
52+
}
53+
}
54+
55+
return !insideRaw;
56+
});
57+
});
58+
59+
Prism.hooks.add('after-tokenize', function (env) {
60+
Prism.languages['markup-templating'].tokenizePlaceholders(env, 'liquid');
61+
});

components/prism-liquid.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/autoloader/prism-autoloader.js

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
],
8989
"less": "css",
9090
"lilypond": "scheme",
91+
"liquid": "markup-templating",
9192
"markdown": "markup",
9293
"markup-templating": "markup",
9394
"mongodb": "javascript",

plugins/autoloader/prism-autoloader.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{% if true and false and false or true %}
2+
3+
{% nil %}
4+
5+
----------------------------------------------------
6+
7+
[
8+
["liquid", [
9+
["delimiter", "{%"],
10+
["keyword", "if"],
11+
["boolean", "true"],
12+
["operator", "and"],
13+
["boolean", "false"],
14+
["operator", "and"],
15+
["boolean", "false"],
16+
["operator", "or"],
17+
["boolean", "true"],
18+
["delimiter", "%}"]
19+
]],
20+
21+
["liquid", [
22+
["delimiter", "{%"],
23+
["boolean", "nil"],
24+
["delimiter", "%}"]
25+
]]
26+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
My name is Wilson Abercrombie{% comment %}, esquire{% endcomment %}.
2+
3+
{% assign verb = "turned" %}
4+
{% comment %}
5+
{% assign verb = "converted" %}
6+
{% endcomment %}
7+
Anything you put between {% comment %} and {% endcomment %} tags
8+
is {{ verb }} into a comment.
9+
10+
----------------------------------------------------
11+
12+
[
13+
"My name is Wilson Abercrombie",
14+
["liquid", [
15+
["delimiter", "{%"],
16+
["keyword", "comment"],
17+
["delimiter", "%}"],
18+
["comment", ", esquire"],
19+
["delimiter", "{%"],
20+
["keyword", "endcomment"],
21+
["delimiter", "%}"]
22+
]],
23+
".\r\n\r\n",
24+
25+
["liquid", [
26+
["delimiter", "{%"],
27+
["keyword", "assign"],
28+
" verb ",
29+
["operator", "="],
30+
["string", "\"turned\""],
31+
["delimiter", "%}"]
32+
]],
33+
34+
["liquid", [
35+
["delimiter", "{%"],
36+
["keyword", "comment"],
37+
["delimiter", "%}"],
38+
["comment", "\r\n{% assign verb = \"converted\" %}\r\n"],
39+
["delimiter", "{%"],
40+
["keyword", "endcomment"],
41+
["delimiter", "%}"]
42+
]],
43+
44+
"\r\nAnything you put between ",
45+
["liquid", [
46+
["delimiter", "{%"],
47+
["keyword", "comment"],
48+
["delimiter", "%}"],
49+
["comment", " and "],
50+
["delimiter", "{%"],
51+
["keyword", "endcomment"],
52+
["delimiter", "%}"]
53+
]],
54+
" tags\r\nis ",
55+
["liquid", [
56+
["delimiter", "{{"],
57+
" verb ",
58+
["delimiter", "}}"]
59+
]],
60+
" into a comment."
61+
]

0 commit comments

Comments
 (0)