From 91dea0c898253fa288086baa28b8483d7b037b93 Mon Sep 17 00:00:00 2001 From: Patrick Seal Date: Fri, 10 Jun 2022 11:29:28 -0700 Subject: [PATCH] Normalize Whitespace Plugin: Add configuration via attributes (#3467) Co-authored-by: Michael Schmidt --- plugins/normalize-whitespace/demo.html | 15 +++++++++ plugins/normalize-whitespace/index.html | 12 ++++++- .../prism-normalize-whitespace.js | 31 +++++++++++++++++++ .../prism-normalize-whitespace.min.js | 2 +- 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/plugins/normalize-whitespace/demo.html b/plugins/normalize-whitespace/demo.html index a6bcf22bea..068cdd76f5 100644 --- a/plugins/normalize-whitespace/demo.html +++ b/plugins/normalize-whitespace/demo.html @@ -17,6 +17,21 @@ +
+
+	
+
+
+		var there_is_a_very_very_very_very_long_line_it_can_break_it_for_you = true;
+
+		if (there_is_a_very_very_very_very_long_line_it_can_break_it_for_you === true) {
+		};
+
+
+	
+
+
+ diff --git a/plugins/normalize-whitespace/index.html b/plugins/normalize-whitespace/index.html index ce049d8f55..a145fc6332 100644 --- a/plugins/normalize-whitespace/index.html +++ b/plugins/normalize-whitespace/index.html @@ -57,7 +57,7 @@

How to use

}); -

The following settings are available:

+

The following settings are available and can be set via the data-[setting] attribute on the <pre< element:

remove-trailing
@@ -103,6 +103,16 @@

Examples

}; + var + there_is_a_very_very_very_very_long_line_it_can_break_it_for_you + = true; + + if + (there_is_a_very_very_very_very_long_line_it_can_break_it_for_you + === true) { + }; + + diff --git a/plugins/normalize-whitespace/prism-normalize-whitespace.js b/plugins/normalize-whitespace/prism-normalize-whitespace.js index c8bc361bbd..4dc89bcf2d 100644 --- a/plugins/normalize-whitespace/prism-normalize-whitespace.js +++ b/plugins/normalize-whitespace/prism-normalize-whitespace.js @@ -33,6 +33,18 @@ return str.length + res; } + var settingsConfig = { + 'remove-trailing': 'boolean', + 'remove-indent': 'boolean', + 'left-trim': 'boolean', + 'right-trim': 'boolean', + 'break-lines': 'number', + 'indent': 'number', + 'remove-initial-line-feed': 'boolean', + 'tabs-to-spaces': 'number', + 'spaces-to-tabs': 'number', + }; + NormalizeWhitespace.prototype = { setDefaults: function (defaults) { this.defaults = assign(this.defaults, defaults); @@ -161,6 +173,25 @@ return; } + if (env.settings == null) { env.settings = {}; } + + // Read settings from 'data-' attributes + for (var key in settingsConfig) { + if (Object.hasOwnProperty.call(settingsConfig, key)) { + var settingType = settingsConfig[key]; + if (pre.hasAttribute('data-' + key)) { + try { + var value = JSON.parse(pre.getAttribute('data-' + key) || 'true'); + if (typeof value === settingType) { + env.settings[key] = value; + } + } catch (_error) { + // ignore error + } + } + } + } + var children = pre.childNodes; var before = ''; var after = ''; diff --git a/plugins/normalize-whitespace/prism-normalize-whitespace.min.js b/plugins/normalize-whitespace/prism-normalize-whitespace.min.js index db661cd737..56918926b3 100644 --- a/plugins/normalize-whitespace/prism-normalize-whitespace.min.js +++ b/plugins/normalize-whitespace/prism-normalize-whitespace.min.js @@ -1 +1 @@ -!function(){if("undefined"!=typeof Prism){var e=Object.assign||function(e,n){for(var t in n)n.hasOwnProperty(t)&&(e[t]=n[t]);return e};n.prototype={setDefaults:function(n){this.defaults=e(this.defaults,n)},normalize:function(n,t){for(var r in t=e(this.defaults,t)){var i=r.replace(/-(\w)/g,(function(e,n){return n.toUpperCase()}));"normalize"!==r&&"setDefaults"!==i&&t[r]&&this[i]&&(n=this[i].call(this,n,t[r]))}return n},leftTrim:function(e){return e.replace(/^\s+/,"")},rightTrim:function(e){return e.replace(/\s+$/,"")},tabsToSpaces:function(e,n){return n=0|n||4,e.replace(/\t/g,new Array(++n).join(" "))},spacesToTabs:function(e,n){return n=0|n||4,e.replace(RegExp(" {"+n+"}","g"),"\t")},removeTrailing:function(e){return e.replace(/\s*?$/gm,"")},removeInitialLineFeed:function(e){return e.replace(/^(?:\r?\n|\r)/,"")},removeIndent:function(e){var n=e.match(/^[^\S\n\r]*(?=\S)/gm);return n&&n[0].length?(n.sort((function(e,n){return e.length-n.length})),n[0].length?e.replace(RegExp("^"+n[0],"gm"),""):e):e},indent:function(e,n){return e.replace(/^[^\S\n\r]*(?=\S)/gm,new Array(++n).join("\t")+"$&")},breakLines:function(e,n){n=!0===n?80:0|n||80;for(var r=e.split("\n"),i=0;in&&(o[l]="\n"+o[l],a=s)}r[i]=o.join("")}return r.join("\n")}},"undefined"!=typeof module&&module.exports&&(module.exports=n),Prism.plugins.NormalizeWhitespace=new n({"remove-trailing":!0,"remove-indent":!0,"left-trim":!0,"right-trim":!0}),Prism.hooks.add("before-sanity-check",(function(e){var n=Prism.plugins.NormalizeWhitespace;if((!e.settings||!1!==e.settings["whitespace-normalization"])&&Prism.util.isActive(e.element,"whitespace-normalization",!0))if(e.element&&e.element.parentNode||!e.code){var t=e.element.parentNode;if(e.code&&t&&"pre"===t.nodeName.toLowerCase()){for(var r=t.childNodes,i="",o="",a=!1,l=0;lt&&(o[l]="\n"+o[l],a=s)}n[i]=o.join("")}return n.join("\n")}},"undefined"!=typeof module&&module.exports&&(module.exports=n),Prism.plugins.NormalizeWhitespace=new n({"remove-trailing":!0,"remove-indent":!0,"left-trim":!0,"right-trim":!0}),Prism.hooks.add("before-sanity-check",(function(e){var n=Prism.plugins.NormalizeWhitespace;if((!e.settings||!1!==e.settings["whitespace-normalization"])&&Prism.util.isActive(e.element,"whitespace-normalization",!0))if(e.element&&e.element.parentNode||!e.code){var r=e.element.parentNode;if(e.code&&r&&"pre"===r.nodeName.toLowerCase()){for(var i in null==e.settings&&(e.settings={}),t)if(Object.hasOwnProperty.call(t,i)){var o=t[i];if(r.hasAttribute("data-"+i))try{var a=JSON.parse(r.getAttribute("data-"+i)||"true");typeof a===o&&(e.settings[i]=a)}catch(e){}}for(var l=r.childNodes,s="",c="",u=!1,m=0;m