Skip to content

Commit

Permalink
Normalize Whitespace Plugin: Add configuration via attributes (#3467)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Schmidt <msrd0000@gmail.com>
  • Loading branch information
plasticrake and RunDevelopment committed Jun 10, 2022
1 parent 2815f69 commit 91dea0c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
15 changes: 15 additions & 0 deletions plugins/normalize-whitespace/demo.html
Expand Up @@ -17,6 +17,21 @@

</pre>

<pre data-break-lines="50">

<code>


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) {
};


</code>

</pre>

</section>

<script src="prism.js"></script>
Expand Down
12 changes: 11 additions & 1 deletion plugins/normalize-whitespace/index.html
Expand Up @@ -57,7 +57,7 @@ <h1>How to use</h1>
});
</code></pre>

<p>The following settings are available:</p>
<p>The following settings are available and can be set via the <code>data-[setting]</code> attribute on the <code>&lt;pre&lt;</code> element:</p>

<dl>
<dt>remove-trailing</dt>
Expand Down Expand Up @@ -103,6 +103,16 @@ <h1>Examples</h1>
};


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) {
};


</code>

</pre>
Expand Down
31 changes: 31 additions & 0 deletions plugins/normalize-whitespace/prism-normalize-whitespace.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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 = '';
Expand Down

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

0 comments on commit 91dea0c

Please sign in to comment.