} userOptions
*/
function configure(userOptions) {
if (userOptions.useBR) {
deprecated("10.3.0", "'useBR' will be removed entirely in v11.0");
deprecated("10.3.0", "Please see https://github.com/highlightjs/highlight.js/issues/2559");
}
options = inherit$1(options, userOptions);
}
/**
* Highlights to all blocks on a page
*
* @type {Function & {called?: boolean}}
*/
// TODO: remove v12, deprecated
const initHighlighting = () => {
if (initHighlighting.called) return;
initHighlighting.called = true;
deprecated("10.6.0", "initHighlighting() is deprecated. Use highlightAll() instead.");
const blocks = document.querySelectorAll('pre code');
blocks.forEach(highlightElement);
};
// Higlights all when DOMContentLoaded fires
// TODO: remove v12, deprecated
function initHighlightingOnLoad() {
deprecated("10.6.0", "initHighlightingOnLoad() is deprecated. Use highlightAll() instead.");
wantsHighlight = true;
}
let wantsHighlight = false;
/**
* auto-highlights all pre>code elements on the page
*/
function highlightAll() {
// if we are called too early in the loading process
if (document.readyState === "loading") {
wantsHighlight = true;
return;
}
const blocks = document.querySelectorAll('pre code');
blocks.forEach(highlightElement);
}
function boot() {
// if a highlight was requested before DOM was loaded, do now
if (wantsHighlight) highlightAll();
}
// make sure we are in the browser environment
if (typeof window !== 'undefined' && window.addEventListener) {
window.addEventListener('DOMContentLoaded', boot, false);
}
/**
* Register a language grammar module
*
* @param {string} languageName
* @param {LanguageFn} languageDefinition
*/
function registerLanguage(languageName, languageDefinition) {
let lang = null;
try {
lang = languageDefinition(hljs);
} catch (error$1) {
error("Language definition for '{}' could not be registered.".replace("{}", languageName));
// hard or soft error
if (!SAFE_MODE) { throw error$1; } else { error(error$1); }
// languages that have serious errors are replaced with essentially a
// "plaintext" stand-in so that the code blocks will still get normal
// css classes applied to them - and one bad language won't break the
// entire highlighter
lang = PLAINTEXT_LANGUAGE;
}
// give it a temporary name if it doesn't have one in the meta-data
if (!lang.name) lang.name = languageName;
languages[languageName] = lang;
lang.rawDefinition = languageDefinition.bind(null, hljs);
if (lang.aliases) {
registerAliases(lang.aliases, { languageName });
}
}
/**
* Remove a language grammar module
*
* @param {string} languageName
*/
function unregisterLanguage(languageName) {
delete languages[languageName];
for (const alias of Object.keys(aliases)) {
if (aliases[alias] === languageName) {
delete aliases[alias];
}
}
}
/**
* @returns {string[]} List of language internal names
*/
function listLanguages() {
return Object.keys(languages);
}
/**
intended usage: When one language truly requires another
Unlike `getLanguage`, this will throw when the requested language
is not available.
@param {string} name - name of the language to fetch/require
@returns {Language | never}
*/
function requireLanguage(name) {
deprecated("10.4.0", "requireLanguage will be removed entirely in v11.");
deprecated("10.4.0", "Please see https://github.com/highlightjs/highlight.js/pull/2844");
const lang = getLanguage(name);
if (lang) { return lang; }
const err = new Error('The \'{}\' language is required, but not loaded.'.replace('{}', name));
throw err;
}
/**
* @param {string} name - name of the language to retrieve
* @returns {Language | undefined}
*/
function getLanguage(name) {
name = (name || '').toLowerCase();
return languages[name] || languages[aliases[name]];
}
/**
*
* @param {string|string[]} aliasList - single alias or list of aliases
* @param {{languageName: string}} opts
*/
function registerAliases(aliasList, { languageName }) {
if (typeof aliasList === 'string') {
aliasList = [aliasList];
}
aliasList.forEach(alias => { aliases[alias.toLowerCase()] = languageName; });
}
/**
* Determines if a given language has auto-detection enabled
* @param {string} name - name of the language
*/
function autoDetection(name) {
const lang = getLanguage(name);
return lang && !lang.disableAutodetect;
}
/**
* Upgrades the old highlightBlock plugins to the new
* highlightElement API
* @param {HLJSPlugin} plugin
*/
function upgradePluginAPI(plugin) {
// TODO: remove with v12
if (plugin["before:highlightBlock"] && !plugin["before:highlightElement"]) {
plugin["before:highlightElement"] = (data) => {
plugin["before:highlightBlock"](
Object.assign({ block: data.el }, data)
);
};
}
if (plugin["after:highlightBlock"] && !plugin["after:highlightElement"]) {
plugin["after:highlightElement"] = (data) => {
plugin["after:highlightBlock"](
Object.assign({ block: data.el }, data)
);
};
}
}
/**
* @param {HLJSPlugin} plugin
*/
function addPlugin(plugin) {
upgradePluginAPI(plugin);
plugins.push(plugin);
}
/**
*
* @param {PluginEvent} event
* @param {any} args
*/
function fire(event, args) {
const cb = event;
plugins.forEach(function(plugin) {
if (plugin[cb]) {
plugin[cb](args);
}
});
}
/**
Note: fixMarkup is deprecated and will be removed entirely in v11
@param {string} arg
@returns {string}
*/
function deprecateFixMarkup(arg) {
deprecated("10.2.0", "fixMarkup will be removed entirely in v11.0");
deprecated("10.2.0", "Please see https://github.com/highlightjs/highlight.js/issues/2534");
return fixMarkup(arg);
}
/**
*
* @param {HighlightedHTMLElement} el
*/
function deprecateHighlightBlock(el) {
deprecated("10.7.0", "highlightBlock will be removed entirely in v12.0");
deprecated("10.7.0", "Please use highlightElement now.");
return highlightElement(el);
}
/* Interface definition */
Object.assign(hljs, {
highlight,
highlightAuto,
highlightAll,
fixMarkup: deprecateFixMarkup,
highlightElement,
// TODO: Remove with v12 API
highlightBlock: deprecateHighlightBlock,
configure,
initHighlighting,
initHighlightingOnLoad,
registerLanguage,
unregisterLanguage,
listLanguages,
getLanguage,
registerAliases,
requireLanguage,
autoDetection,
inherit: inherit$1,
addPlugin,
// plugins for frameworks
vuePlugin: BuildVuePlugin(hljs).VuePlugin
});
hljs.debugMode = function() { SAFE_MODE = false; };
hljs.safeMode = function() { SAFE_MODE = true; };
hljs.versionString = version;
for (const key in MODES) {
// @ts-ignore
if (typeof MODES[key] === "object") {
// @ts-ignore
deepFreezeEs6(MODES[key]);
}
}
// merge all the modes/regexs into our main object
Object.assign(hljs, MODES);
// built-in plugins, likely to be moved out of core in the future
hljs.addPlugin(brPlugin); // slated to be removed in v11
hljs.addPlugin(mergeHTMLPlugin);
hljs.addPlugin(tabReplacePlugin);
return hljs;
};
// export an "instance" of the highlighter
var highlight = HLJS({});
module.exports = highlight;