Skip to content

Commit

Permalink
Cleanup left-over line-highlight tags before other plugins run
Browse files Browse the repository at this point in the history
The tags used by the line-highlight plugin are appended to the <code>
tag. This causes problems with the autoloader plugin, because the
tags contain whitespace and therefore change the content of the <code>
tag.

This patch fixes the issue #1101, by removing any line-highlight tags
with the `before-sanity-check` hook.
  • Loading branch information
zeitgeist87 committed Feb 13, 2017
1 parent 6c7dae2 commit 79b723d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
48 changes: 33 additions & 15 deletions plugins/line-highlight/prism-line-highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ function highlightLines(pre, lines, classes) {

for (var i=0, range; range = ranges[i++];) {
range = range.split('-');

var start = +range[0],
end = +range[1] || start;

var line = document.createElement('div');

line.textContent = Array(end - start + 2).join(' \n');
line.setAttribute('aria-hidden', 'true');
line.className = (classes || '') + ' line-highlight';
Expand Down Expand Up @@ -77,25 +77,25 @@ function highlightLines(pre, lines, classes) {

function applyHash() {
var hash = location.hash.slice(1);

// Remove pre-existing temporary lines
$$('.temporary.line-highlight').forEach(function (line) {
line.parentNode.removeChild(line);
});

var range = (hash.match(/\.([\d,-]+)$/) || [,''])[1];

if (!range || document.getElementById(hash)) {
return;
}

var id = hash.slice(0, hash.lastIndexOf('.')),
pre = document.getElementById(id);

if (!pre) {
return;
}

if (!pre.hasAttribute('data-line')) {
pre.setAttribute('data-line', '');
}
Expand All @@ -107,22 +107,40 @@ function applyHash() {

var fakeTimer = 0; // Hack to limit the number of times applyHash() runs

Prism.hooks.add('complete', function(env) {
Prism.hooks.add('before-sanity-check', function(env) {
var pre = env.element.parentNode;
var lines = pre && pre.getAttribute('data-line');

if (!pre || !lines || !/pre/i.test(pre.nodeName)) {
return;
}

clearTimeout(fakeTimer);


/*
* Cleanup for other plugins (e.g. autoloader).
*
* Sometimes <code> blocks are highlighted multiple times. It is necessary
* to cleanup any left-over tags, because the whitespace inside of the <div>
* tags change the content of the <code> tag.
*/
$$('.line-highlight', pre).forEach(function (line) {
line.parentNode.removeChild(line);
});


env.code = env.element.textContent;
});

Prism.hooks.add('complete', function(env) {
var pre = env.element.parentNode;
var lines = pre && pre.getAttribute('data-line');

if (!pre || !lines || !/pre/i.test(pre.nodeName)) {
return;
}

clearTimeout(fakeTimer);

highlightLines(pre, lines);

fakeTimer = setTimeout(applyHash, 1);
});

Expand Down
2 changes: 1 addition & 1 deletion plugins/line-highlight/prism-line-highlight.min.js

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

0 comments on commit 79b723d

Please sign in to comment.