Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Liquid: Added Markup support, missing tokens, and other improvements #2950

Merged
merged 10 commits into from
Jun 15, 2021
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@
},
"liquid": {
"title": "Liquid",
"require": "markup-templating",
"owner": "cinhtau"
},
"lisp": {
Expand Down
65 changes: 57 additions & 8 deletions components/prism-liquid.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,61 @@
Prism.languages.liquid = {
'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/,
'number': /\b0b[01]+\b|\b0x(?:\.[\da-fp-]+|[\da-f]+(?:\.[\da-fp-]+)?)\b|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e[+-]?\d+)?[df]?/i,
'operator': {
pattern: /(^|[^.])(?:\+[+=]?|-[-=]?|!=?|<<?=?|>>?>?=?|==?|&[&=]?|\|[|=]?|\*=?|\/=?|%=?|\^=?|[?:~])/m,
'comment': {
pattern: /(^\{%\s*comment\s*%\})[\s\S]+(?=\{%\s*endcomment\s*%\}$)/,
lookbehind: true
},
'function': {
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;|&])/,
lookbehind: true
}
'delimiter': {
pattern: /^\{(?:\{\{|[%\{])-?|-?(?:\}\}|[%\}])\}$/,
alias: 'punctuation'
},
'string': {
pattern: /"[^"]*"|'[^']*'/,
greedy: true
},
'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/,
'function': [
{
pattern: /(\|\s*)\w+/,
lookbehind: true,
alias: 'filter'
},
{
// array functions
pattern: /(\.\s*)(?:first|last|size)/,
lookbehind: true
}
],
'boolean': /\b(?:true|false|nil)\b/,
'range': {
pattern: /\.\./,
alias: 'operator'
},
// https://github.com/Shopify/liquid/blob/698f5e0d967423e013f6169d9111bd969bd78337/lib/liquid/lexer.rb#L21
'number': /\b\d+(?:\.\d+)?\b/,
'operator': /[!=]=|<>|[<>]=?|[|?:=-]|\b(?:and|or|contains(?=\s))\b/,
'punctuation': /[.,\[\]()]/
};

Prism.hooks.add('before-tokenize', function (env) {
var liquidPattern = /\{%\s*comment\s*%\}[\s\S]*?\{%\s*endcomment\s*%\}|\{(?:%[\s\S]*?%|\{\{[\s\S]*?\}\}|\{[\s\S]*?\})\}/g;
var insideRaw = false;

Prism.languages['markup-templating'].buildPlaceholders(env, 'liquid', liquidPattern, function (match) {
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
var tagMatch = /^\{%-?\s*(\w+)/.exec(match);
if (tagMatch) {
var tag = tagMatch[1];
if (tag === 'raw' && !insideRaw) {
insideRaw = true;
return true;
} else if (tag === 'endraw') {
insideRaw = false;
return true;
}
}

return !insideRaw;
});
});

Prism.hooks.add('after-tokenize', function (env) {
Prism.languages['markup-templating'].tokenizePlaceholders(env, 'liquid');
});
2 changes: 1 addition & 1 deletion components/prism-liquid.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions plugins/autoloader/prism-autoloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
],
"less": "css",
"lilypond": "scheme",
"liquid": "markup-templating",
"markdown": "markup",
"markup-templating": "markup",
"mongodb": "javascript",
Expand Down
2 changes: 1 addition & 1 deletion plugins/autoloader/prism-autoloader.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions tests/languages/liquid/boolean_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% if true and false and false or true %}

{% nil %}

----------------------------------------------------

[
["liquid", [
["delimiter", "{%"],
["keyword", "if"],
["boolean", "true"],
["operator", "and"],
["boolean", "false"],
["operator", "and"],
["boolean", "false"],
["operator", "or"],
["boolean", "true"],
["delimiter", "%}"]
]],

["liquid", [
["delimiter", "{%"],
["boolean", "nil"],
["delimiter", "%}"]
]]
]
61 changes: 61 additions & 0 deletions tests/languages/liquid/comment_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
My name is Wilson Abercrombie{% comment %}, esquire{% endcomment %}.

{% assign verb = "turned" %}
{% comment %}
{% assign verb = "converted" %}
{% endcomment %}
Anything you put between {% comment %} and {% endcomment %} tags
is {{ verb }} into a comment.

----------------------------------------------------

[
"My name is Wilson Abercrombie",
["liquid", [
["delimiter", "{%"],
["keyword", "comment"],
["delimiter", "%}"],
["comment", ", esquire"],
["delimiter", "{%"],
["keyword", "endcomment"],
["delimiter", "%}"]
]],
".\r\n\r\n",

["liquid", [
["delimiter", "{%"],
["keyword", "assign"],
" verb ",
["operator", "="],
["string", "\"turned\""],
["delimiter", "%}"]
]],

["liquid", [
["delimiter", "{%"],
["keyword", "comment"],
["delimiter", "%}"],
["comment", "\r\n{% assign verb = \"converted\" %}\r\n"],
["delimiter", "{%"],
["keyword", "endcomment"],
["delimiter", "%}"]
]],

"\r\nAnything you put between ",
["liquid", [
["delimiter", "{%"],
["keyword", "comment"],
["delimiter", "%}"],
["comment", " and "],
["delimiter", "{%"],
["keyword", "endcomment"],
["delimiter", "%}"]
]],
" tags\r\nis ",
["liquid", [
["delimiter", "{{"],
" verb ",
["delimiter", "}}"]
]],
" into a comment."
]
Loading