Usage
nebula.pre()
Parameters
This function does not accept any parameters. Is this incorrect?
Additional Notes
This function is called automatically if Nebula pre tags are used.
Source File
Located in /assets/js/modules/helpers.js on line 492.
No Hooks
This function does not have any filters or actions available. Request one?nebula.pre = async function(){
//Format non-shortcode pre tags to be styled properly
jQuery('pre.nebula-code').each(async function(){
await nebula.yield();
if ( !jQuery(this).parent('.nebula-code-con').length ){
let lang = jQuery(this).attr('data-lang') || '';
if ( lang === '' ){
let langMatches = jQuery(this).attr('class').match(/lang(?:uage)?-(\S*)/i);
lang = ( langMatches )? langMatches[0] : ''; //Use a class that starts with "lang-" or "language-" Ex: "lang-JavaScript"
}
if ( lang === '' ){
lang = jQuery(this).attr('class').replace('nebula-code', '').replaceAll(/(\s*)((wp|m.|p.|nebula)-\S+)(\s*)/gi, '').trim(); //Remove expected classes and use remaining class as language
}
lang = escape(lang); //Escape for reuse into the DOM
jQuery(this).addClass(lang.toLowerCase()).wrap('<div class="nebula-code-con via-helper-js clearfix ' + lang.toLowerCase() + '"></div>');
jQuery(this).closest('.nebula-code-con').prepend('<span class="nebula-code codetitle ' + lang.toLowerCase() + '">' + lang + '</span>');
}
});
//Manage copying snippets to clipboard
if ( 'clipboard' in navigator ){
jQuery('.nebula-code-con').each(function(){
jQuery(this).append('<a href="#" class="nebula-selectcopy-code">Copy to Clipboard</a>');
jQuery(this).find('p:empty').remove(); //Sometimes WordPress adds extra/empty <p> tags. These mess with spacing, so we remove them.
});
nebula.dom.document.on('click', '.nebula-selectcopy-code', function(){
let $oThis = jQuery(this);
if ( $oThis.hasClass('error') ){ //If we already errored, stop trying
return false;
}
let text = jQuery(this).closest('.nebula-code-con').find('pre').text();
navigator.clipboard.writeText(text).then(function(){
$oThis.text('Copied!').removeClass('error').addClass('success');
setTimeout(function(){
$oThis.text('Copy to clipboard').removeClass('success');
}, 1500);
}).catch(function(error){ //This can happen if the user denies clipboard permissions
gtag('event', 'Exception', { //Report the error to Google Analytics to log it
message: '(JS) Clipboard API error: ' + error,
fatal: false
});
$oThis.text('Unable to copy.').addClass('error');
});
return false;
});
}
};
Override
To override or disable this JavaScript function, simply redeclare it with the exact same function name. Remember: Some functionality is conditionally loaded via dynamic imports, so if your function is not overriding properly, try listening for a DOM event (described below).
For non-module import functions:
nebula.pre = function(){
//Write your own code here, leave it blank, or return false.
}
For dynamically imported module function overrides:
jQuery(window).on('load', function(){
nebula.pre = function(){
//Write your own code here, leave it blank, or return false.
}
});
Custom Nebula DOM events do also exist, so you could also try the following if the Window Load listener does not work:
jQuery(document).on('nebula_module_loaded', function(module){
//Note that the module variable is also available to know which module specifically was imported
if ( typeof nebula.pre === 'function' ){
nebula.pre = function(){
//Write your own code here, leave it blank, or return false.
}
}
});