mirror of
https://github.com/McShelby/hugo-theme-relearn.git
synced 2024-11-23 07:47:54 +00:00
mark: remove jQuery #452
This commit is contained in:
parent
844fb9214e
commit
e57358006a
1 changed files with 129 additions and 100 deletions
|
@ -772,41 +772,144 @@ function scrollToFragment() {
|
||||||
}, 10);
|
}, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
function mark(){
|
function mark() {
|
||||||
// mark some additonal stuff as searchable
|
// mark some additional stuff as searchable
|
||||||
$('#topbar a:not(:has(img)):not(.btn)').addClass('highlight');
|
var topbarLinks = document.querySelectorAll( '#topbar a:not(:has(img)):not(.btn)' );
|
||||||
$('#body-inner a:not(:has(img)):not(.btn):not(.lightbox):not(a[rel="footnote"])').addClass('highlight');
|
for( var i = 0; i < topbarLinks.length; i++ ){
|
||||||
|
topbarLinks[i].classList.add( 'highlight' );
|
||||||
|
}
|
||||||
|
|
||||||
var value = sessionStorage.getItem(baseUriFull+'search-value');
|
var bodyInnerLinks = document.querySelectorAll( '#body-inner a:not(:has(img)):not(.btn):not(.lightbox):not(a[rel="footnote"])' );
|
||||||
$(".highlightable").highlight(value, { element: 'mark' });
|
for( var i = 0; i < bodyInnerLinks.length; i++ ){
|
||||||
$("mark").parents(".expand").addClass("expand-marked");
|
bodyInnerLinks[i].classList.add( 'highlight' );
|
||||||
$("mark").parents("li").each( function(){
|
}
|
||||||
var i = jQuery(this).children("input.toggle:not(.menu-marked)");
|
|
||||||
if( i.length ){
|
var value = sessionStorage.getItem( baseUriFull + 'search-value' );
|
||||||
e = jQuery(i[0]);
|
var highlightableElements = document.querySelectorAll( '.highlightable' );
|
||||||
e.attr("data-checked", (e.prop('checked')?"true":"false")).addClass("menu-marked");
|
highlight( highlightableElements, value, { element: 'mark' } );
|
||||||
i[0].checked = true;
|
|
||||||
}
|
var markedElements = document.querySelectorAll( 'mark' );
|
||||||
});
|
for( var i = 0; i < markedElements.length; i++ ){
|
||||||
|
var parent = markedElements[i].parentNode;
|
||||||
|
while( parent && parent.classList ){
|
||||||
|
if( parent.classList.contains( 'expand' ) ){
|
||||||
|
parent.classList.add( 'expand-marked' );
|
||||||
|
}
|
||||||
|
if( parent.tagName.toLowerCase() === 'li' ){
|
||||||
|
var toggleInputs = parent.querySelectorAll( 'input.toggle:not(.menu-marked)' );
|
||||||
|
if( i.length ){
|
||||||
|
toggleInputs[0].classList.add( 'menu-marked' );
|
||||||
|
toggleInputs[0].dataset.checked = toggleInputs[0].checked ? 'true' : 'false';
|
||||||
|
toggleInputs[0].checked = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
parent = parent.parentNode;
|
||||||
|
}
|
||||||
|
}
|
||||||
psm && psm.update();
|
psm && psm.update();
|
||||||
}
|
}
|
||||||
window.relearn.markSearch = mark;
|
window.relearn.markSearch = mark;
|
||||||
|
|
||||||
function unmark(){
|
function highlight( es, words, options ){
|
||||||
sessionStorage.removeItem(baseUriFull+'search-value');
|
var settings = {
|
||||||
$("mark").parents("li").each( function(){
|
className: 'highlight',
|
||||||
var i = jQuery(this).children("input.toggle.menu-marked");
|
element: 'span',
|
||||||
if( i.length ){
|
caseSensitive: false,
|
||||||
e = jQuery(i[0]);
|
wordsOnly: false
|
||||||
i[0].checked = (e.attr("data-checked")=="true");
|
};
|
||||||
e.attr("data-checked", null).removeClass("menu-marked");
|
Object.assign( settings, options );
|
||||||
}
|
|
||||||
|
if( !words ){ return; }
|
||||||
|
if( words.constructor === String ){
|
||||||
|
words = [ words ];
|
||||||
|
}
|
||||||
|
words = words.filter( function( word, i ){
|
||||||
|
return word != '';
|
||||||
});
|
});
|
||||||
$("mark").parents(".expand-marked").removeClass("expand-marked");
|
words = words.map( function( word, i ){
|
||||||
$(".highlightable").unhighlight({ element: 'mark' })
|
return word.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
|
||||||
|
});
|
||||||
|
if( words.length == 0 ){ return this; }
|
||||||
|
|
||||||
|
var flag = settings.caseSensitive ? '' : 'i';
|
||||||
|
var pattern = "(" + words.join( '|' ) + ')';
|
||||||
|
if( settings.wordsOnly ){
|
||||||
|
pattern = '\\b' + pattern + '\\b';
|
||||||
|
}
|
||||||
|
var re = new RegExp( pattern, flag );
|
||||||
|
|
||||||
|
for( var i = 0; i < es.length; i++ ){
|
||||||
|
highlightNode( es[i], re, settings.element, settings.className );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function highlightNode( node, re, nodeName, className ){
|
||||||
|
if( node.nodeType === 3 && node.parentElement && node.parentElement.namespaceURI == 'http://www.w3.org/1999/xhtml' ) { // text nodes
|
||||||
|
var match = node.data.match( re );
|
||||||
|
if( match ){
|
||||||
|
var highlight = document.createElement( nodeName || 'span' );
|
||||||
|
highlight.className = className || 'highlight';
|
||||||
|
var wordNode = node.splitText( match.index );
|
||||||
|
wordNode.splitText( match[0].length );
|
||||||
|
var wordClone = wordNode.cloneNode( true );
|
||||||
|
highlight.appendChild( wordClone );
|
||||||
|
wordNode.parentNode.replaceChild( highlight, wordNode );
|
||||||
|
return 1; //skip added node in parent
|
||||||
|
}
|
||||||
|
} else if( (node.nodeType === 1 && node.childNodes) && // only element nodes that have children
|
||||||
|
!/(script|style)/i.test(node.tagName) && // ignore script and style nodes
|
||||||
|
!(node.tagName === nodeName.toUpperCase() && node.className === className) ){ // skip if already highlighted
|
||||||
|
for( var i = 0; i < node.childNodes.length; i++ ){
|
||||||
|
i += highlightNode( node.childNodes[i], re, nodeName, className );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
function unmark() {
|
||||||
|
sessionStorage.removeItem( baseUriFull + 'search-value' );
|
||||||
|
var markedElements = document.querySelectorAll( 'mark' );
|
||||||
|
for( var i = 0; i < markedElements.length; i++ ){
|
||||||
|
var parent = markedElements[i].parentNode;
|
||||||
|
while( parent && parent.classList ){
|
||||||
|
if( parent.tagName.toLowerCase() === 'li' ){
|
||||||
|
var toggleInputs = parent.querySelectorAll( 'input.toggle.menu-marked' );
|
||||||
|
if( i.length ){
|
||||||
|
toggleInputs[0].checked = toggleInputs[0].dataset.checked === 'true';
|
||||||
|
toggleInputs[0].dataset.checked = null;
|
||||||
|
toggleInputs[0].classList.remove( 'menu-marked' );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if( parent.classList.contains( 'expand-marked' ) ){
|
||||||
|
parent.classList.remove( 'expand-marked' );
|
||||||
|
}
|
||||||
|
parent = parent.parentNode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var highlighted = document.querySelectorAll( '.highlightable' );
|
||||||
|
unhighlight( highlighted, { element: 'mark' } );
|
||||||
psm && psm.update();
|
psm && psm.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function unhighlight( es, options ){
|
||||||
|
var settings = {
|
||||||
|
className: 'highlight',
|
||||||
|
element: 'span'
|
||||||
|
};
|
||||||
|
Object.assign( settings, options );
|
||||||
|
|
||||||
|
for( var i = 0; i < es.length; i++ ){
|
||||||
|
var highlightedElements = es[i].querySelectorAll( settings.element + '.' + settings.className );
|
||||||
|
for( var j = 0; j < highlightedElements.length; j++ ){
|
||||||
|
var parent = highlightedElements[j].parentNode;
|
||||||
|
parent.replaceChild( highlightedElements[j].firstChild, highlightedElements[j] );
|
||||||
|
parent.normalize();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
// replace jQuery.createPseudo with https://stackoverflow.com/a/66318392
|
// replace jQuery.createPseudo with https://stackoverflow.com/a/66318392
|
||||||
function elementContains( txt, e ){
|
function elementContains( txt, e ){
|
||||||
var regex = RegExp( txt, 'i' );
|
var regex = RegExp( txt, 'i' );
|
||||||
|
@ -943,80 +1046,6 @@ jQuery(function() {
|
||||||
initSearch();
|
initSearch();
|
||||||
});
|
});
|
||||||
|
|
||||||
jQuery.extend({
|
|
||||||
highlight: function(node, re, nodeName, className) {
|
|
||||||
if (node.nodeType === 3 && node.parentElement && node.parentElement.namespaceURI == 'http://www.w3.org/1999/xhtml') { // text nodes
|
|
||||||
var match = node.data.match(re);
|
|
||||||
if (match) {
|
|
||||||
var highlight = document.createElement(nodeName || 'span');
|
|
||||||
highlight.className = className || 'highlight';
|
|
||||||
var wordNode = node.splitText(match.index);
|
|
||||||
wordNode.splitText(match[0].length);
|
|
||||||
var wordClone = wordNode.cloneNode(true);
|
|
||||||
highlight.appendChild(wordClone);
|
|
||||||
wordNode.parentNode.replaceChild(highlight, wordNode);
|
|
||||||
return 1; //skip added node in parent
|
|
||||||
}
|
|
||||||
} else if ((node.nodeType === 1 && node.childNodes) && // only element nodes that have children
|
|
||||||
!/(script|style)/i.test(node.tagName) && // ignore script and style nodes
|
|
||||||
!(node.tagName === nodeName.toUpperCase() && node.className === className)) { // skip if already highlighted
|
|
||||||
for (var i = 0; i < node.childNodes.length; i++) {
|
|
||||||
i += jQuery.highlight(node.childNodes[i], re, nodeName, className);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
jQuery.fn.unhighlight = function(options) {
|
|
||||||
var settings = {
|
|
||||||
className: 'highlight',
|
|
||||||
element: 'span'
|
|
||||||
};
|
|
||||||
jQuery.extend(settings, options);
|
|
||||||
|
|
||||||
return this.find(settings.element + "." + settings.className).each(function() {
|
|
||||||
var parent = this.parentNode;
|
|
||||||
parent.replaceChild(this.firstChild, this);
|
|
||||||
parent.normalize();
|
|
||||||
}).end();
|
|
||||||
};
|
|
||||||
|
|
||||||
jQuery.fn.highlight = function(words, options) {
|
|
||||||
var settings = {
|
|
||||||
className: 'highlight',
|
|
||||||
element: 'span',
|
|
||||||
caseSensitive: false,
|
|
||||||
wordsOnly: false
|
|
||||||
};
|
|
||||||
jQuery.extend(settings, options);
|
|
||||||
|
|
||||||
if (!words) { return; }
|
|
||||||
|
|
||||||
if (words.constructor === String) {
|
|
||||||
words = [words];
|
|
||||||
}
|
|
||||||
words = jQuery.grep(words, function(word, i) {
|
|
||||||
return word != '';
|
|
||||||
});
|
|
||||||
words = jQuery.map(words, function(word, i) {
|
|
||||||
return word.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
|
|
||||||
});
|
|
||||||
if (words.length == 0) { return this; }
|
|
||||||
;
|
|
||||||
|
|
||||||
var flag = settings.caseSensitive ? "" : "i";
|
|
||||||
var pattern = "(" + words.join("|") + ")";
|
|
||||||
if (settings.wordsOnly) {
|
|
||||||
pattern = "\\b" + pattern + "\\b";
|
|
||||||
}
|
|
||||||
var re = new RegExp(pattern, flag);
|
|
||||||
|
|
||||||
return this.each(function() {
|
|
||||||
jQuery.highlight(this, re, settings.element, settings.className);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
function useMermaid( config ){
|
function useMermaid( config ){
|
||||||
if( !Object.assign ){
|
if( !Object.assign ){
|
||||||
// We don't support Mermaid for IE11 anyways, so bail out early
|
// We don't support Mermaid for IE11 anyways, so bail out early
|
||||||
|
|
Loading…
Reference in a new issue