Skip to content

Commit

Permalink
Added new Filter highlightAll plugin (#2074)
Browse files Browse the repository at this point in the history
This adds a new plugin to Prism which allows users to filter which elements will be highlighted by the `highlightAll` and `highlightAllUnder` methods.
  • Loading branch information
RunDevelopment committed Dec 2, 2019
1 parent 9908ca6 commit a7f7009
Show file tree
Hide file tree
Showing 8 changed files with 328 additions and 44 deletions.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,12 @@
"description": "Highlights the code inside diff blocks.",
"owner": "RunDevelopment",
"require": "diff"
},
"filter-highlight-all": {
"title": "Filter highlightAll",
"description": "Filters the elements the <code>highlightAll</code> and <code>highlightAllUnder</code> methods actually highlight.",
"owner": "RunDevelopment",
"noCSS": true
}
}
}
45 changes: 24 additions & 21 deletions components/prism-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,6 @@ var Prism = (function (_self){
var lang = /\blang(?:uage)?-([\w-]+)\b/i;
var uniqueId = 0;

/**
* Returns the Prism language of the given element set by a `language-xxxx` or `lang-xxxx` class.
*
* If no language is set for the element or the element is `null` or `undefined`, `none` will be returned.
*
* @param {Element} element
* @returns {string}
*/
function getLanguage(element) {
while (element && !lang.test(element.className)) {
element = element.parentNode;
}
if (element) {
return (element.className.match(lang) || [, 'none'])[1].toLowerCase();
}
return 'none';
}


var _ = {
manual: _self.Prism && _self.Prism.manual,
Expand Down Expand Up @@ -103,6 +85,24 @@ var _ = {
}
},

/**
* Returns the Prism language of the given element set by a `language-xxxx` or `lang-xxxx` class.
*
* If no language is set for the element or the element is `null` or `undefined`, `none` will be returned.
*
* @param {Element} element
* @returns {string}
*/
getLanguage: function (element) {
while (element && !lang.test(element.className)) {
element = element.parentElement;
}
if (element) {
return (element.className.match(lang) || [, 'none'])[1].toLowerCase();
}
return 'none';
},

/**
* Returns the script element that is currently executing.
*
Expand Down Expand Up @@ -236,21 +236,24 @@ var _ = {
highlightAllUnder: function(container, async, callback) {
var env = {
callback: callback,
container: container,
selector: 'code[class*="language-"], [class*="language-"] code, code[class*="lang-"], [class*="lang-"] code'
};

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

var elements = container.querySelectorAll(env.selector);
env.elements = Array.prototype.slice.apply(env.container.querySelectorAll(env.selector));

_.hooks.run('before-all-elements-highlight', env);

for (var i=0, element; element = elements[i++];) {
for (var i = 0, element; element = env.elements[i++];) {
_.highlightElement(element, async === true, env.callback);
}
},

highlightElement: function(element, async, callback) {
// Find language
var language = getLanguage(element);
var language = _.util.getLanguage(element);
var grammar = _.languages[language];

// Set language on the element, if not present
Expand Down
2 changes: 1 addition & 1 deletion components/prism-core.min.js

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

Loading

0 comments on commit a7f7009

Please sign in to comment.