|
1 | 1 | 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*%\}$)/, |
6 | 4 | lookbehind: true |
7 | 5 | }, |
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': /[.,\[\]()]/ |
12 | 36 | }; |
| 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 | +}); |
0 commit comments