Skip to content

Commit a7f7009

Browse files
Added new Filter highlightAll plugin (#2074)
This adds a new plugin to Prism which allows users to filter which elements will be highlighted by the `highlightAll` and `highlightAllUnder` methods.
1 parent 9908ca6 commit a7f7009

File tree

8 files changed

+328
-44
lines changed

8 files changed

+328
-44
lines changed

components.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components.json

+6
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,12 @@
12091209
"description": "Highlights the code inside diff blocks.",
12101210
"owner": "RunDevelopment",
12111211
"require": "diff"
1212+
},
1213+
"filter-highlight-all": {
1214+
"title": "Filter highlightAll",
1215+
"description": "Filters the elements the <code>highlightAll</code> and <code>highlightAllUnder</code> methods actually highlight.",
1216+
"owner": "RunDevelopment",
1217+
"noCSS": true
12121218
}
12131219
}
12141220
}

components/prism-core.js

+24-21
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,6 @@ var Prism = (function (_self){
1818
var lang = /\blang(?:uage)?-([\w-]+)\b/i;
1919
var uniqueId = 0;
2020

21-
/**
22-
* Returns the Prism language of the given element set by a `language-xxxx` or `lang-xxxx` class.
23-
*
24-
* If no language is set for the element or the element is `null` or `undefined`, `none` will be returned.
25-
*
26-
* @param {Element} element
27-
* @returns {string}
28-
*/
29-
function getLanguage(element) {
30-
while (element && !lang.test(element.className)) {
31-
element = element.parentNode;
32-
}
33-
if (element) {
34-
return (element.className.match(lang) || [, 'none'])[1].toLowerCase();
35-
}
36-
return 'none';
37-
}
38-
3921

4022
var _ = {
4123
manual: _self.Prism && _self.Prism.manual,
@@ -103,6 +85,24 @@ var _ = {
10385
}
10486
},
10587

88+
/**
89+
* Returns the Prism language of the given element set by a `language-xxxx` or `lang-xxxx` class.
90+
*
91+
* If no language is set for the element or the element is `null` or `undefined`, `none` will be returned.
92+
*
93+
* @param {Element} element
94+
* @returns {string}
95+
*/
96+
getLanguage: function (element) {
97+
while (element && !lang.test(element.className)) {
98+
element = element.parentElement;
99+
}
100+
if (element) {
101+
return (element.className.match(lang) || [, 'none'])[1].toLowerCase();
102+
}
103+
return 'none';
104+
},
105+
106106
/**
107107
* Returns the script element that is currently executing.
108108
*
@@ -236,21 +236,24 @@ var _ = {
236236
highlightAllUnder: function(container, async, callback) {
237237
var env = {
238238
callback: callback,
239+
container: container,
239240
selector: 'code[class*="language-"], [class*="language-"] code, code[class*="lang-"], [class*="lang-"] code'
240241
};
241242

242243
_.hooks.run('before-highlightall', env);
243244

244-
var elements = container.querySelectorAll(env.selector);
245+
env.elements = Array.prototype.slice.apply(env.container.querySelectorAll(env.selector));
246+
247+
_.hooks.run('before-all-elements-highlight', env);
245248

246-
for (var i=0, element; element = elements[i++];) {
249+
for (var i = 0, element; element = env.elements[i++];) {
247250
_.highlightElement(element, async === true, env.callback);
248251
}
249252
},
250253

251254
highlightElement: function(element, async, callback) {
252255
// Find language
253-
var language = getLanguage(element);
256+
var language = _.util.getLanguage(element);
254257
var grammar = _.languages[language];
255258

256259
// Set language on the element, if not present

components/prism-core.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)