Skip to content

Commit

Permalink
Added general de/activation mechanism for plugins (#2434)
Browse files Browse the repository at this point in the history
Keep Markup, Line numbers, and Normalize whitespace now share the same per-element class-based de/activation logic.
  • Loading branch information
RunDevelopment authored Jun 26, 2020
1 parent 3a127c7 commit a36e96a
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 25 deletions.
35 changes: 35 additions & 0 deletions components/prism-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,41 @@ var _ = {
}
return null;
}
},

/**
* Returns whether a given class is active for `element`.
*
* The class can be activated if `element` or one of its ancestors has the given class and it can be deactivated
* if `element` or one of its ancestors has the negated version of the given class. The _negated version_ of the
* given class is just the given class with a `no-` prefix.
*
* Whether the class is active is determined by the closest ancestor of `element` (where `element` itself is
* closest ancestor) that has the given class or the negated version of it. If neither `element` nor any of its
* ancestors have the given class or the negated version of it, then the default activation will be returned.
*
* In the paradoxical situation where the closest ancestor contains __both__ the given class and the negated
* version of it, the class is considered active.
*
* @param {Element} element
* @param {string} className
* @param {boolean} [defaultActivation=false]
* @returns {boolean}
*/
isActive: function (element, className, defaultActivation) {
var no = 'no-' + className;

while (element) {
var classList = element.classList;
if (classList.contains(className)) {
return true;
}
if (classList.contains(no)) {
return false;
}
element = element.parentElement;
}
return !!defaultActivation;
}
},

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.

5 changes: 4 additions & 1 deletion plugins/keep-markup/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
<section>

<h1>How to use</h1>
<p>You have nothing to do. With this plugin loaded, all markup inside code will be kept.</p>

<p>You have nothing to do. The plugin is active by default. With this plugin loaded, all markup inside code will be kept.</p>

<p>However, you can deactivate the plugin for certain code element by adding the <code>no-keep-markup</code> class to it. You can also deactivate the plugin for the whole page by adding the <code>no-keep-markup</code> class to the body of the page and then selectively activate it again by adding the <code>keep-markup</code> class to code elements.</p>

<h1>Examples</h1>

Expand Down
4 changes: 4 additions & 0 deletions plugins/keep-markup/prism-keep-markup.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
return;
}

if (!Prism.util.isActive(env.element, 'keep-markup', true)) {
return;
}

var pos = 0;
var data = [];
var f = function (elt, baseNode) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/keep-markup/prism-keep-markup.min.js

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

22 changes: 5 additions & 17 deletions plugins/line-numbers/prism-line-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@
return;
}

var code = env.element;
var pre = code.parentNode;
var code = /** @type {Element} */ (env.element);
var pre = /** @type {Element} */ (code.parentNode);

// works only for <code> wrapped inside <pre> (not inline)
if (!pre || !/pre/i.test(pre.nodeName)) {
Expand All @@ -87,27 +87,15 @@
return;
}

var addLineNumbers = false;
var lineNumbersRegex = /(?:^|\s)line-numbers(?:\s|$)/;

for (var element = code; element; element = element.parentNode) {
if (lineNumbersRegex.test(element.className)) {
addLineNumbers = true;
break;
}
}

// only add line numbers if <code> or one of its ancestors has the `line-numbers` class
if (!addLineNumbers) {
if (!Prism.util.isActive(code, PLUGIN_NAME)) {
return;
}

// Remove the class 'line-numbers' from the <code>
code.className = code.className.replace(lineNumbersRegex, ' ');
code.classList.remove(PLUGIN_NAME);
// Add the class 'line-numbers' to the <pre>
if (!lineNumbersRegex.test(pre.className)) {
pre.className += ' line-numbers';
}
pre.classList.add(PLUGIN_NAME);

var match = env.code.match(NEW_LINE_EXP);
var linesNum = match ? match.length + 1 : 1;
Expand Down
Loading

0 comments on commit a36e96a

Please sign in to comment.