08-27-周三_17-09-29
This commit is contained in:
99
node_modules/prismjs/plugins/keep-markup/prism-keep-markup.js
generated
vendored
Normal file
99
node_modules/prismjs/plugins/keep-markup/prism-keep-markup.js
generated
vendored
Normal file
@@ -0,0 +1,99 @@
|
||||
(function () {
|
||||
|
||||
if (typeof self === 'undefined' || !self.Prism || !self.document || !document.createRange) {
|
||||
return;
|
||||
}
|
||||
|
||||
Prism.plugins.KeepMarkup = true;
|
||||
|
||||
Prism.hooks.add('before-highlight', function (env) {
|
||||
if (!env.element.children.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
var pos = 0;
|
||||
var data = [];
|
||||
var f = function (elt, baseNode) {
|
||||
var o = {};
|
||||
if (!baseNode) {
|
||||
// Clone the original tag to keep all attributes
|
||||
o.clone = elt.cloneNode(false);
|
||||
o.posOpen = pos;
|
||||
data.push(o);
|
||||
}
|
||||
for (var i = 0, l = elt.childNodes.length; i < l; i++) {
|
||||
var child = elt.childNodes[i];
|
||||
if (child.nodeType === 1) { // element
|
||||
f(child);
|
||||
} else if(child.nodeType === 3) { // text
|
||||
pos += child.data.length;
|
||||
}
|
||||
}
|
||||
if (!baseNode) {
|
||||
o.posClose = pos;
|
||||
}
|
||||
};
|
||||
f(env.element, true);
|
||||
|
||||
if (data && data.length) {
|
||||
// data is an array of all existing tags
|
||||
env.keepMarkup = data;
|
||||
}
|
||||
});
|
||||
|
||||
Prism.hooks.add('after-highlight', function (env) {
|
||||
if(env.keepMarkup && env.keepMarkup.length) {
|
||||
|
||||
var walk = function (elt, nodeState) {
|
||||
for (var i = 0, l = elt.childNodes.length; i < l; i++) {
|
||||
|
||||
var child = elt.childNodes[i];
|
||||
|
||||
if (child.nodeType === 1) { // element
|
||||
if (!walk(child, nodeState)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
} else if (child.nodeType === 3) { // text
|
||||
if(!nodeState.nodeStart && nodeState.pos + child.data.length > nodeState.node.posOpen) {
|
||||
// We found the start position
|
||||
nodeState.nodeStart = child;
|
||||
nodeState.nodeStartPos = nodeState.node.posOpen - nodeState.pos;
|
||||
}
|
||||
if(nodeState.nodeStart && nodeState.pos + child.data.length >= nodeState.node.posClose) {
|
||||
// We found the end position
|
||||
nodeState.nodeEnd = child;
|
||||
nodeState.nodeEndPos = nodeState.node.posClose - nodeState.pos;
|
||||
}
|
||||
|
||||
nodeState.pos += child.data.length;
|
||||
}
|
||||
|
||||
if (nodeState.nodeStart && nodeState.nodeEnd) {
|
||||
// Select the range and wrap it with the clone
|
||||
var range = document.createRange();
|
||||
range.setStart(nodeState.nodeStart, nodeState.nodeStartPos);
|
||||
range.setEnd(nodeState.nodeEnd, nodeState.nodeEndPos);
|
||||
nodeState.node.clone.appendChild(range.extractContents());
|
||||
range.insertNode(nodeState.node.clone);
|
||||
range.detach();
|
||||
|
||||
// Process is over
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
// For each tag, we walk the DOM to reinsert it
|
||||
env.keepMarkup.forEach(function (node) {
|
||||
walk(env.element, {
|
||||
node: node,
|
||||
pos: 0
|
||||
});
|
||||
});
|
||||
// Store new highlightedCode for later hooks calls
|
||||
env.highlightedCode = env.element.innerHTML;
|
||||
}
|
||||
});
|
||||
}());
|
1
node_modules/prismjs/plugins/keep-markup/prism-keep-markup.min.js
generated
vendored
Normal file
1
node_modules/prismjs/plugins/keep-markup/prism-keep-markup.min.js
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
"undefined"!=typeof self&&self.Prism&&self.document&&document.createRange&&(Prism.plugins.KeepMarkup=!0,Prism.hooks.add("before-highlight",function(e){if(e.element.children.length){var a=0,s=[],l=function(e,n){var o={};n||(o.clone=e.cloneNode(!1),o.posOpen=a,s.push(o));for(var t=0,d=e.childNodes.length;t<d;t++){var r=e.childNodes[t];1===r.nodeType?l(r):3===r.nodeType&&(a+=r.data.length)}n||(o.posClose=a)};l(e.element,!0),s&&s.length&&(e.keepMarkup=s)}}),Prism.hooks.add("after-highlight",function(n){if(n.keepMarkup&&n.keepMarkup.length){var a=function(e,n){for(var o=0,t=e.childNodes.length;o<t;o++){var d=e.childNodes[o];if(1===d.nodeType){if(!a(d,n))return!1}else 3===d.nodeType&&(!n.nodeStart&&n.pos+d.data.length>n.node.posOpen&&(n.nodeStart=d,n.nodeStartPos=n.node.posOpen-n.pos),n.nodeStart&&n.pos+d.data.length>=n.node.posClose&&(n.nodeEnd=d,n.nodeEndPos=n.node.posClose-n.pos),n.pos+=d.data.length);if(n.nodeStart&&n.nodeEnd){var r=document.createRange();return r.setStart(n.nodeStart,n.nodeStartPos),r.setEnd(n.nodeEnd,n.nodeEndPos),n.node.clone.appendChild(r.extractContents()),r.insertNode(n.node.clone),r.detach(),!1}}return!0};n.keepMarkup.forEach(function(e){a(n.element,{node:e,pos:0})}),n.highlightedCode=n.element.innerHTML}}));
|
Reference in New Issue
Block a user