From ed9f2b2179000862f7bc8c01b68676f568daa7c8 Mon Sep 17 00:00:00 2001 From: Golmote Date: Mon, 17 Aug 2015 08:47:32 +0200 Subject: [PATCH] Move removal of initial line feed to a plugin --- components.js | 5 ++ components/prism-core.js | 4 -- components/prism-core.min.js | 2 +- plugins/remove-initial-line-feed/index.html | 57 +++++++++++++++++++ .../prism-remove-initial-line-feed.js | 13 +++++ .../prism-remove-initial-line-feed.min.js | 1 + prism.js | 4 -- 7 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 plugins/remove-initial-line-feed/index.html create mode 100644 plugins/remove-initial-line-feed/prism-remove-initial-line-feed.js create mode 100644 plugins/remove-initial-line-feed/prism-remove-initial-line-feed.min.js diff --git a/components.js b/components.js index 919b3c13ae..1129ef8eab 100644 --- a/components.js +++ b/components.js @@ -377,6 +377,11 @@ var components = { "title": "Highlight Keywords", "owner": "vkbansal", "noCSS": true + }, + "remove-initial-line-feed": { + "title": "Remove initial line feed", + "owner": "Golmote", + "noCSS": true } } }; diff --git a/components/prism-core.js b/components/prism-core.js index 6b5799350b..fcccccde29 100644 --- a/components/prism-core.js +++ b/components/prism-core.js @@ -181,10 +181,6 @@ var _ = _self.Prism = { code: code }; - if(code) { - env.code = code.replace(/^(?:\r?\n|\r)/, ''); - } - if (!code || !grammar) { _.hooks.run('complete', env); return; diff --git a/components/prism-core.min.js b/components/prism-core.min.js index bf3d9c3096..9499c01cf6 100644 --- a/components/prism-core.min.js +++ b/components/prism-core.min.js @@ -1 +1 @@ -var _self="undefined"!=typeof window?window:"undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope?self:{},Prism=function(){var e=/\blang(?:uage)?-(?!\*)(\w+)\b/i,t=_self.Prism={util:{encode:function(e){return e instanceof n?new n(e.type,t.util.encode(e.content),e.alias):"Array"===t.util.type(e)?e.map(t.util.encode):e.replace(/&/g,"&").replace(/e.length)break e;if(!(d instanceof a)){u.lastIndex=0;var m=u.exec(d);if(m){g&&(f=m[1].length);var y=m.index-1+f,m=m[0].slice(f),v=m.length,k=y+v,b=d.slice(0,y+1),w=d.slice(k+1),N=[p,1];b&&N.push(b);var O=new a(l,c?t.tokenize(m,c):m,h);N.push(O),w&&N.push(w),Array.prototype.splice.apply(r,N)}}}}}return r},hooks:{all:{},add:function(e,n){var a=t.hooks.all;a[e]=a[e]||[],a[e].push(n)},run:function(e,n){var a=t.hooks.all[e];if(a&&a.length)for(var r,i=0;r=a[i++];)r(n)}}},n=t.Token=function(e,t,n){this.type=e,this.content=t,this.alias=n};if(n.stringify=function(e,a,r){if("string"==typeof e)return e;if("Array"===t.util.type(e))return e.map(function(t){return n.stringify(t,a,e)}).join("");var i={type:e.type,content:n.stringify(e.content,a,r),tag:"span",classes:["token",e.type],attributes:{},language:a,parent:r};if("comment"==i.type&&(i.attributes.spellcheck="true"),e.alias){var l="Array"===t.util.type(e.alias)?e.alias:[e.alias];Array.prototype.push.apply(i.classes,l)}t.hooks.run("wrap",i);var o="";for(var s in i.attributes)o+=s+'="'+(i.attributes[s]||"")+'"';return"<"+i.tag+' class="'+i.classes.join(" ")+'" '+o+">"+i.content+""},!_self.document)return _self.addEventListener?(_self.addEventListener("message",function(e){var n=JSON.parse(e.data),a=n.language,r=n.code;_self.postMessage(JSON.stringify(t.util.encode(t.tokenize(r,t.languages[a])))),_self.close()},!1),_self.Prism):_self.Prism;var a=document.getElementsByTagName("script");return a=a[a.length-1],a&&(t.filename=a.src,document.addEventListener&&!a.hasAttribute("data-manual")&&document.addEventListener("DOMContentLoaded",t.highlightAll)),_self.Prism}();"undefined"!=typeof module&&module.exports&&(module.exports=Prism); \ No newline at end of file +var _self="undefined"!=typeof window?window:"undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope?self:{},Prism=function(){var e=/\blang(?:uage)?-(?!\*)(\w+)\b/i,t=_self.Prism={util:{encode:function(e){return e instanceof n?new n(e.type,t.util.encode(e.content),e.alias):"Array"===t.util.type(e)?e.map(t.util.encode):e.replace(/&/g,"&").replace(/e.length)break e;if(!(d instanceof a)){u.lastIndex=0;var m=u.exec(d);if(m){c&&(f=m[1].length);var y=m.index-1+f,m=m[0].slice(f),v=m.length,k=y+v,b=d.slice(0,y+1),w=d.slice(k+1),N=[p,1];b&&N.push(b);var O=new a(l,g?t.tokenize(m,g):m,h);N.push(O),w&&N.push(w),Array.prototype.splice.apply(r,N)}}}}}return r},hooks:{all:{},add:function(e,n){var a=t.hooks.all;a[e]=a[e]||[],a[e].push(n)},run:function(e,n){var a=t.hooks.all[e];if(a&&a.length)for(var r,i=0;r=a[i++];)r(n)}}},n=t.Token=function(e,t,n){this.type=e,this.content=t,this.alias=n};if(n.stringify=function(e,a,r){if("string"==typeof e)return e;if("Array"===t.util.type(e))return e.map(function(t){return n.stringify(t,a,e)}).join("");var i={type:e.type,content:n.stringify(e.content,a,r),tag:"span",classes:["token",e.type],attributes:{},language:a,parent:r};if("comment"==i.type&&(i.attributes.spellcheck="true"),e.alias){var l="Array"===t.util.type(e.alias)?e.alias:[e.alias];Array.prototype.push.apply(i.classes,l)}t.hooks.run("wrap",i);var o="";for(var s in i.attributes)o+=s+'="'+(i.attributes[s]||"")+'"';return"<"+i.tag+' class="'+i.classes.join(" ")+'" '+o+">"+i.content+""},!_self.document)return _self.addEventListener?(_self.addEventListener("message",function(e){var n=JSON.parse(e.data),a=n.language,r=n.code;_self.postMessage(JSON.stringify(t.util.encode(t.tokenize(r,t.languages[a])))),_self.close()},!1),_self.Prism):_self.Prism;var a=document.getElementsByTagName("script");return a=a[a.length-1],a&&(t.filename=a.src,document.addEventListener&&!a.hasAttribute("data-manual")&&document.addEventListener("DOMContentLoaded",t.highlightAll)),_self.Prism}();"undefined"!=typeof module&&module.exports&&(module.exports=Prism); \ No newline at end of file diff --git a/plugins/remove-initial-line-feed/index.html b/plugins/remove-initial-line-feed/index.html new file mode 100644 index 0000000000..2af0b6697a --- /dev/null +++ b/plugins/remove-initial-line-feed/index.html @@ -0,0 +1,57 @@ + + + + + + + Remove initial line feed ▲ Prism plugins + + + + + + + + + + +
+
+ +

Remove initial line feed

+

Removes the initial line feed in code blocks.

+
+ +
+

How to use

+ +

Obviously, this is supposed to work only for code blocks (<pre><code>) and not for inline code.

+

Add class remove-initial-line-feed to your desired <pre>.

+
+ +
+

Examples

+ +

Without adding the class

+

+<div></div>
+
+ +

With the class added

+

+<div></div>
+
+ +
+ + + + + + + + + + + + \ No newline at end of file diff --git a/plugins/remove-initial-line-feed/prism-remove-initial-line-feed.js b/plugins/remove-initial-line-feed/prism-remove-initial-line-feed.js new file mode 100644 index 0000000000..c4d3d0ecae --- /dev/null +++ b/plugins/remove-initial-line-feed/prism-remove-initial-line-feed.js @@ -0,0 +1,13 @@ +Prism.hooks.add('before-highlight', function(env) { + if (env.code) { + var pre = env.element.parentNode; + var clsReg = /\s*\bremove-initial-line-feed\b\s*/; + if ( + pre && pre.nodeName.toLowerCase() === 'pre' && + // Apply only if the
 or the  have the class
+			(clsReg.test(pre.className) || clsReg.test(env.element.className))
+		) {
+			env.code = env.code.replace(/^(?:\r?\n|\r)/, '');
+		}
+	}
+});
\ No newline at end of file
diff --git a/plugins/remove-initial-line-feed/prism-remove-initial-line-feed.min.js b/plugins/remove-initial-line-feed/prism-remove-initial-line-feed.min.js
new file mode 100644
index 0000000000..083381afdc
--- /dev/null
+++ b/plugins/remove-initial-line-feed/prism-remove-initial-line-feed.min.js
@@ -0,0 +1 @@
+Prism.hooks.add("before-highlight",function(e){if(e.code){var o=e.element.parentNode,a=/\s*\bremove-initial-line-feed\b\s*/;o&&"pre"===o.nodeName.toLowerCase()&&(a.test(o.className)||a.test(e.element.className))&&(e.code=e.code.replace(/^(?:\r?\n|\r)/,""))}});
\ No newline at end of file
diff --git a/prism.js b/prism.js
index 61bbaf307e..fd979fbf77 100644
--- a/prism.js
+++ b/prism.js
@@ -186,10 +186,6 @@ var _ = _self.Prism = {
 			code: code
 		};
 
-		if(code) {
-			env.code = code.replace(/^(?:\r?\n|\r)/, '');
-		}
-
 		if (!code || !grammar) {
 			_.hooks.run('complete', env);
 			return;