Commit fe3bc526e524c052b9751f8bc86b1a97001e10b6

Jason Kurian 2021-07-15T12:35:26

Liquid: Added `empty` keyword (#2997)

diff --git a/components/prism-liquid.js b/components/prism-liquid.js
index 487dcc5..b337c19 100644
--- a/components/prism-liquid.js
+++ b/components/prism-liquid.js
@@ -32,7 +32,11 @@ Prism.languages.liquid = {
 	// 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': /[.,\[\]()]/
+	'punctuation': /[.,\[\]()]/,
+	'empty': {
+		pattern: /\bempty\b/,
+		alias: 'keyword'
+	},
 };
 
 Prism.hooks.add('before-tokenize', function (env) {
diff --git a/components/prism-liquid.min.js b/components/prism-liquid.min.js
index 9afc80e..f33aafe 100644
--- a/components/prism-liquid.min.js
+++ b/components/prism-liquid.min.js
@@ -1 +1 @@
-Prism.languages.liquid={comment:{pattern:/(^\{%\s*comment\s*%\})[\s\S]+(?=\{%\s*endcomment\s*%\}$)/,lookbehind:!0},delimiter:{pattern:/^\{(?:\{\{|[%\{])-?|-?(?:\}\}|[%\}])\}$/,alias:"punctuation"},string:{pattern:/"[^"]*"|'[^']*'/,greedy:!0},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:!0,alias:"filter"},{pattern:/(\.\s*)(?:first|last|size)/,lookbehind:!0}],boolean:/\b(?:true|false|nil)\b/,range:{pattern:/\.\./,alias:"operator"},number:/\b\d+(?:\.\d+)?\b/,operator:/[!=]=|<>|[<>]=?|[|?:=-]|\b(?:and|or|contains(?=\s))\b/,punctuation:/[.,\[\]()]/},Prism.hooks.add("before-tokenize",function(e){var r=!1;Prism.languages["markup-templating"].buildPlaceholders(e,"liquid",/\{%\s*comment\s*%\}[\s\S]*?\{%\s*endcomment\s*%\}|\{(?:%[\s\S]*?%|\{\{[\s\S]*?\}\}|\{[\s\S]*?\})\}/g,function(e){var n=/^\{%-?\s*(\w+)/.exec(e);if(n){var t=n[1];if("raw"===t&&!r)return r=!0;if("endraw"===t)return!(r=!1)}return!r})}),Prism.hooks.add("after-tokenize",function(e){Prism.languages["markup-templating"].tokenizePlaceholders(e,"liquid")});
\ No newline at end of file
+Prism.languages.liquid={comment:{pattern:/(^\{%\s*comment\s*%\})[\s\S]+(?=\{%\s*endcomment\s*%\}$)/,lookbehind:!0},delimiter:{pattern:/^\{(?:\{\{|[%\{])-?|-?(?:\}\}|[%\}])\}$/,alias:"punctuation"},string:{pattern:/"[^"]*"|'[^']*'/,greedy:!0},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:!0,alias:"filter"},{pattern:/(\.\s*)(?:first|last|size)/,lookbehind:!0}],boolean:/\b(?:true|false|nil)\b/,range:{pattern:/\.\./,alias:"operator"},number:/\b\d+(?:\.\d+)?\b/,operator:/[!=]=|<>|[<>]=?|[|?:=-]|\b(?:and|or|contains(?=\s))\b/,punctuation:/[.,\[\]()]/,empty:{pattern:/\bempty\b/,alias:"keyword"}},Prism.hooks.add("before-tokenize",function(e){var r=!1;Prism.languages["markup-templating"].buildPlaceholders(e,"liquid",/\{%\s*comment\s*%\}[\s\S]*?\{%\s*endcomment\s*%\}|\{(?:%[\s\S]*?%|\{\{[\s\S]*?\}\}|\{[\s\S]*?\})\}/g,function(e){var n=/^\{%-?\s*(\w+)/.exec(e);if(n){var t=n[1];if("raw"===t&&!r)return r=!0;if("endraw"===t)return!(r=!1)}return!r})}),Prism.hooks.add("after-tokenize",function(e){Prism.languages["markup-templating"].tokenizePlaceholders(e,"liquid")});
\ No newline at end of file
diff --git a/tests/languages/liquid/empty_feature.test b/tests/languages/liquid/empty_feature.test
new file mode 100644
index 0000000..bfc7fef
--- /dev/null
+++ b/tests/languages/liquid/empty_feature.test
@@ -0,0 +1,78 @@
+{% unless pages == empty %}
+  <!-- This will only print if the page with handle "about" is not empty -->
+  <h1>{{ pages.frontpage.not_empty_title }}</h1>
+  <div>{{ pages.frontpage.content }}</div>
+{% endunless %}
+
+----------------------------------------------------
+
+[
+	["liquid", [
+		["delimiter", "{%"],
+		["keyword", "unless"],
+		" pages ",
+		["operator", "=="],
+		["empty", "empty"],
+		["delimiter", "%}"]
+	]],
+
+	["comment", "<!-- This will only print if the page with handle \"about\" is not empty -->"],
+
+	["tag", [
+		["tag", [
+			["punctuation", "<"],
+			"h1"
+		]],
+		["punctuation", ">"]
+	]],
+	["liquid", [
+		["delimiter", "{{"],
+		" pages",
+		["punctuation", "."],
+		"frontpage",
+		["punctuation", "."],
+		"not_empty_title ",
+		["delimiter", "}}"]
+	]],
+	["tag", [
+		["tag", [
+			["punctuation", "</"],
+			"h1"
+		]],
+		["punctuation", ">"]
+	]],
+
+	["tag", [
+		["tag", [
+			["punctuation", "<"],
+			"div"
+		]],
+		["punctuation", ">"]
+	]],
+	["liquid", [
+		["delimiter", "{{"],
+		" pages",
+		["punctuation", "."],
+		"frontpage",
+		["punctuation", "."],
+		"content ",
+		["delimiter", "}}"]
+	]],
+	["tag", [
+		["tag", [
+			["punctuation", "</"],
+			"div"
+		]],
+		["punctuation", ">"]
+	]],
+
+	["liquid", [
+		["delimiter", "{%"],
+		["keyword", "endunless"],
+		["delimiter", "%}"]
+	]]
+]
+
+----------------------------------------------------
+
+Test for the 'empty' keyword / special thing.