diff --git a/README.md b/README.md index f41c1bc..44b0266 100644 --- a/README.md +++ b/README.md @@ -12,19 +12,13 @@ npm i highlightjs-lang.js ``` -### Bower - -```bash -bower install highlightjs-lang -``` - #### Getting the library from CDN ```html - + ``` -highlightjs-lang.js 1.1.0 is known to work with highlight.js 11.3.1. +highlightjs-lang.js 1.1.0 is known to work with highlight.js 11.3.1+ ## Usage @@ -33,6 +27,7 @@ Download plugin and include file after highlight.js: ```html + ``` @@ -57,7 +52,7 @@ Initialize plugin after highlight.js: ```js hljs.highlightAll(); -hljs.initLangOnLoad(); +hljs.initLangOnLoad(); // <-- init plugin ``` Here’s an equivalent way to calling `initLangBlock` using jQuery: @@ -77,6 +72,7 @@ After version 1.1.0 plugin has optional parameter `options` - for custom setup: version | name | type | default value | description --------|---------------|---------|---------------|----------------------- v1.1.0 | overrideNames | object | {} | [Override the default language names](#overrideNames) +v1.1.1 | fallback | func(str): str | (lang) => lang | [Fallback to convert unknown names](#fallback) ### Examples of using @@ -98,9 +94,11 @@ If you want to override the default language name, you can specify a _overridden ```js var myOptions = { + // ... overrideNames: { cs: 'C#', - } + }, + // ... }; ``` @@ -118,6 +116,26 @@ In both cases language name will be `C#`. > [List of default language names](https://github.com/wcoder/highlightjs-lang.js/blob/master/src/highlightjs-lang.js#L4-L10) +### fallback + +Specifying the desired format for undeclared language names: + +```js +var myOptions = { + // ... + fallback: function (lang) { + return '~~' + lang; + }, + // ... +}; +``` + +Convert all undeclared language names to names with `~~` prefix: + +``` +xyz -> ~~xyz +``` + ## Skipping some blocks (Applies to `hljs.initLangOnLoad()` initialization only.) diff --git a/dist/highlightjs-lang.min.js b/dist/highlightjs-lang.min.js index 2686279..1ca48fd 100644 --- a/dist/highlightjs-lang.min.js +++ b/dist/highlightjs-lang.min.js @@ -1 +1 @@ -!function(r,o){"use strict";var a={cpp:"C++",cs:"C#",csharp:"C#",fsharp:"F#",objectivec:"Objective-C"};function t(e){try{var t,n=o.querySelectorAll("code.hljs,code.nohighlight");for(t in n)!n.hasOwnProperty(t)||n[t].classList.contains("nohljslang")||i(n[t],e)}catch(e){r.console.error("hljs-lang error: ",e)}}function i(e,t){var n,r;"object"==typeof e&&(n=function(e){if(e&&1 diff --git a/src/highlightjs-lang.js b/src/highlightjs-lang.js index 3c679af..1c9e18d 100644 --- a/src/highlightjs-lang.js +++ b/src/highlightjs-lang.js @@ -56,7 +56,7 @@ if (lang !== '') { var langPanel = document.createElement('div'); langPanel.className = 'hljs-lang'; - langPanel.textContent = convertLangName(lang, internalOptions.overrideNames); + langPanel.textContent = convertLangName(lang, internalOptions); element.parentNode.insertBefore(langPanel, element); } } @@ -70,6 +70,7 @@ options = options || {}; return { overrideNames: getOverrideNamesOption(element, langKey, options), + fallback: getFallbackOption(options), }; } @@ -91,6 +92,16 @@ return overrideNames; } + function getFallbackOption (options) { + return !!options.fallback + ? options.fallback + : defaultFallbackOption; + } + + function defaultFallbackOption (langKey) { + return langKey; + } + function getLangNameFromElement (element) { var classes = element.className.split(' '); var lang = getLangNameFromClasses(classes); @@ -105,9 +116,9 @@ return ''; } - function convertLangName(langKey, overrideNamesMap) + function convertLangName(langKey, options) { - var overriddenLangName = overrideNamesMap[langKey]; + var overriddenLangName = options.overrideNames[langKey]; if (!!overriddenLangName) { return overriddenLangName; } @@ -117,7 +128,7 @@ return langName; } - return langKey; + return options.fallback(langKey); } /**