prism-liquid.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. Prism.languages.liquid = {
  2. 'comment': {
  3. pattern: /(^\{%\s*comment\s*%\})[\s\S]+(?=\{%\s*endcomment\s*%\}$)/,
  4. lookbehind: true
  5. },
  6. 'delimiter': {
  7. pattern: /^\{(?:\{\{|[%\{])-?|-?(?:\}\}|[%\}])\}$/,
  8. alias: 'punctuation'
  9. },
  10. 'string': {
  11. pattern: /"[^"]*"|'[^']*'/,
  12. greedy: true
  13. },
  14. 'keyword': /\b(?:as|assign|break|(?:end)?(?:capture|case|comment|for|form|if|paginate|raw|style|tablerow|unless)|continue|cycle|decrement|echo|else|elsif|in|include|increment|limit|liquid|offset|range|render|reversed|section|when|with)\b/,
  15. 'object': /\b(?:address|all_country_option_tags|article|block|blog|cart|checkout|collection|color|country|country_option_tags|currency|current_page|current_tags|customer|customer_address|date|discount_allocation|discount_application|external_video|filter|filter_value|font|forloop|fulfillment|generic_file|gift_card|group|handle|image|line_item|link|linklist|localization|location|measurement|media|metafield|model|model_source|order|page|page_description|page_image|page_title|part|policy|product|product_option|recommendations|request|robots|routes|rule|script|search|selling_plan|selling_plan_allocation|selling_plan_group|shipping_method|shop|shop_locale|sitemap|store_availability|tax_line|template|theme|transaction|unit_price_measurement|user_agent|variant|video|video_source)\b/,
  16. 'function': [
  17. {
  18. pattern: /(\|\s*)\w+/,
  19. lookbehind: true,
  20. alias: 'filter'
  21. },
  22. {
  23. // array functions
  24. pattern: /(\.\s*)(?:first|last|size)/,
  25. lookbehind: true
  26. }
  27. ],
  28. 'boolean': /\b(?:false|nil|true)\b/,
  29. 'range': {
  30. pattern: /\.\./,
  31. alias: 'operator'
  32. },
  33. // https://github.com/Shopify/liquid/blob/698f5e0d967423e013f6169d9111bd969bd78337/lib/liquid/lexer.rb#L21
  34. 'number': /\b\d+(?:\.\d+)?\b/,
  35. 'operator': /[!=]=|<>|[<>]=?|[|?:=-]|\b(?:and|contains(?=\s)|or)\b/,
  36. 'punctuation': /[.,\[\]()]/,
  37. 'empty': {
  38. pattern: /\bempty\b/,
  39. alias: 'keyword'
  40. },
  41. };
  42. Prism.hooks.add('before-tokenize', function (env) {
  43. var liquidPattern = /\{%\s*comment\s*%\}[\s\S]*?\{%\s*endcomment\s*%\}|\{(?:%[\s\S]*?%|\{\{[\s\S]*?\}\}|\{[\s\S]*?\})\}/g;
  44. var insideRaw = false;
  45. Prism.languages['markup-templating'].buildPlaceholders(env, 'liquid', liquidPattern, function (match) {
  46. var tagMatch = /^\{%-?\s*(\w+)/.exec(match);
  47. if (tagMatch) {
  48. var tag = tagMatch[1];
  49. if (tag === 'raw' && !insideRaw) {
  50. insideRaw = true;
  51. return true;
  52. } else if (tag === 'endraw') {
  53. insideRaw = false;
  54. return true;
  55. }
  56. }
  57. return !insideRaw;
  58. });
  59. });
  60. Prism.hooks.add('after-tokenize', function (env) {
  61. Prism.languages['markup-templating'].tokenizePlaceholders(env, 'liquid');
  62. });