Skip to content

Commit

Permalink
Add Node.js support for the normalize-whitespace plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitgeist87 committed Feb 11, 2017
1 parent 867c11d commit 6c7dae2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 8 deletions.
35 changes: 35 additions & 0 deletions plugins/normalize-whitespace/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,41 @@ <h1>Examples</h1>

</pre>

<p>This plugin can also be used on the server or on the command line with Node.js:</p>

<pre><code class="language-javascript">
var Prism = require('prismjs');
var Normalizer = require('prismjs/plugins/normalize-whitespace/prism-normalize-whitespace');
// Create a new Normalizer object
var nw = new Normalizer({
'remove-trailing': true,
'remove-indent': true,
'left-trim': true,
'right-trim': true,
/*'break-lines': 80,
'indent': 2,
'remove-initial-line-feed': false,
'tabs-to-spaces': 4,
'spaces-to-tabs': 4*/
});

// ..or use the default object from Prism
nw = Prism.plugins.NormalizeWhitespace;

// The code snippet you want to highlight, as a string
var code = "\t\t\tvar data = 1; ";

// Removes leading and trailing whitespace
// and then indents by 1 tab
code = nw.normalize(code, {
// Extra settings
indent: 1
});

// Returns a highlighted HTML string
var html = Prism.highlight(code, Prism.languages.javascript);
</code></pre>


</section>

Expand Down
32 changes: 25 additions & 7 deletions plugins/normalize-whitespace/prism-normalize-whitespace.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
(function() {

if (typeof self === 'undefined' || !self.Prism || !self.document) {
return;
}

var assign = Object.assign || function (obj1, obj2) {
for (var name in obj2) {
if (obj2.hasOwnProperty(name))
Expand Down Expand Up @@ -114,6 +110,16 @@ NormalizeWhitespace.prototype = {
}
};

// Support node modules
if (typeof module !== 'undefined' && module.exports) {
module.exports = NormalizeWhitespace;
}

// Exit if prism is not loaded
if (typeof Prism === 'undefined') {
return;
}

Prism.plugins.NormalizeWhitespace = new NormalizeWhitespace({
'remove-trailing': true,
'remove-indent': true,
Expand All @@ -127,18 +133,30 @@ Prism.plugins.NormalizeWhitespace = new NormalizeWhitespace({
});

Prism.hooks.add('before-sanity-check', function (env) {
var Normalizer = Prism.plugins.NormalizeWhitespace;

// Check settings
if (env.settings && env.settings['whitespace-normalization'] === false) {
return;
}

// Simple mode if there is no env.element
if ((!env.element || !env.element.parentNode) && env.code) {
env.code = Normalizer.normalize(env.code, env.settings);
return;
}

// Normal mode
var pre = env.element.parentNode;
var clsReg = /\bno-whitespace-normalization\b/;
if (!env.code || !pre || pre.nodeName.toLowerCase() !== 'pre' ||
(env.settings && env.settings['whitespace-normalization'] === false) ||
clsReg.test(pre.className) || clsReg.test(env.element.className))
return;

var children = pre.childNodes,
before = '',
after = '',
codeFound = false,
Normalizer = Prism.plugins.NormalizeWhitespace;
codeFound = false;

// Move surrounding whitespace from the <pre> tag into the <code> tag
for (var i = 0; i < children.length; ++i) {
Expand Down

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

0 comments on commit 6c7dae2

Please sign in to comment.