Skip to content

Commit b679cfe

Browse files
Keep Markup: Added drop-tokens option class (#3166)
1 parent 22d0c6b commit b679cfe

File tree

4 files changed

+66
-18
lines changed

4 files changed

+66
-18
lines changed

plugins/keep-markup/index.html

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<script>var _gaq = [['_setAccount', 'UA-33746269-1'], ['_trackPageview']];</script>
2828
<script src="https://www.google-analytics.com/ga.js" async></script>
2929
</head>
30-
<body>
30+
<body class="language-none">
3131

3232
<header data-plugin-header="keep-markup"></header>
3333

@@ -39,6 +39,12 @@ <h1>How to use</h1>
3939

4040
<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>
4141

42+
<h2>Double highlighting</h2>
43+
44+
<p>Some plugins (e.g. <a href="plugins/autoloader">Autoloader</a>) need to re-highlight code blocks. This is a problem for Keep Markup because it will keep the markup of the first highlighting pass resulting in a lot of unnecessary DOM nodes and causing problems for themes and other plugins.</p>
45+
46+
<p>This problem can be fixed by adding a <code>drop-tokens</code> class to a code block or any of its ancestors. If <code>drop-tokens</code> is present, Keep Markup will ignore all <code class="language-css">span.token</code> elements created by Prism.</p>
47+
4248
<h1>Examples</h1>
4349

4450
<p>The following source code</p>

plugins/keep-markup/prism-keep-markup.js

+38-16
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,53 @@
1515
return;
1616
}
1717

18+
var dropTokens = Prism.util.isActive(env.element, 'drop-tokens', false);
19+
/**
20+
* Returns whether the given element should be kept.
21+
*
22+
* @param {HTMLElement} element
23+
* @returns {boolean}
24+
*/
25+
function shouldKeep(element) {
26+
if (dropTokens && element.nodeName.toLowerCase() === 'span' && element.classList.contains('token')) {
27+
return false;
28+
}
29+
return true;
30+
}
31+
1832
var pos = 0;
1933
var data = [];
20-
var f = function (elt, baseNode) {
21-
var o = {};
22-
if (!baseNode) {
23-
// Clone the original tag to keep all attributes
24-
o.clone = elt.cloneNode(false);
25-
o.posOpen = pos;
26-
data.push(o);
34+
function processElement(element) {
35+
if (!shouldKeep(element)) {
36+
// don't keep this element and just process its children
37+
processChildren(element);
38+
return;
2739
}
28-
for (var i = 0, l = elt.childNodes.length; i < l; i++) {
29-
var child = elt.childNodes[i];
40+
41+
var o = {
42+
// Clone the original tag to keep all attributes
43+
clone: element.cloneNode(false),
44+
posOpen: pos
45+
};
46+
data.push(o);
47+
48+
processChildren(element);
49+
50+
o.posClose = pos;
51+
}
52+
function processChildren(element) {
53+
for (var i = 0, l = element.childNodes.length; i < l; i++) {
54+
var child = element.childNodes[i];
3055
if (child.nodeType === 1) { // element
31-
f(child);
56+
processElement(child);
3257
} else if (child.nodeType === 3) { // text
3358
pos += child.data.length;
3459
}
3560
}
36-
if (!baseNode) {
37-
o.posClose = pos;
38-
}
39-
};
40-
f(env.element, true);
61+
}
62+
processChildren(env.element);
4163

42-
if (data && data.length) {
64+
if (data.length) {
4365
// data is an array of all existing tags
4466
env.keepMarkup = data;
4567
}

plugins/keep-markup/prism-keep-markup.min.js

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

tests/plugins/keep-markup/test.js

+20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { createScopedPrismDom } = require('../../helper/prism-dom-util');
44

55
describe('Keep Markup', function () {
66
const { Prism, document } = createScopedPrismDom(this, {
7+
languages: 'javascript',
78
plugins: 'keep-markup'
89
});
910

@@ -42,6 +43,25 @@ describe('Keep Markup', function () {
4243
keepMarkup(`xy<a>a</a>`);
4344
});
4445

46+
it('should support double highlighting', function () {
47+
const pre = document.createElement('pre');
48+
pre.className = 'language-javascript drop-tokens';
49+
pre.innerHTML = '<code>var <mark>a = 42</mark>;</code>';
50+
const code = pre.childNodes[0];
51+
const initial = code.innerHTML;
52+
53+
Prism.highlightElement(code);
54+
const firstPass = code.innerHTML;
55+
56+
Prism.highlightElement(code);
57+
const secondPass = code.innerHTML;
58+
59+
// check that we actually did some highlighting
60+
assert.notStrictEqual(initial, firstPass);
61+
// check that the highlighting persists
62+
assert.strictEqual(firstPass, secondPass);
63+
});
64+
4565
// The markup is removed if it's the last element and the element's name is a single letter: a(nchor), b(old), i(talic)...
4666
// https://github.com/PrismJS/prism/issues/1618
4767
/*

0 commit comments

Comments
 (0)