Skip to content

Add fallback option #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,13 @@
npm i highlightjs-lang.js
```

### Bower

```bash
bower install highlightjs-lang
```

#### Getting the library from CDN

```html
<script src="//cdn.jsdelivr.net/npm/highlightjs-lang.js@1.1.0/dist/highlightjs-lang.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/highlightjs-lang.js@latest/dist/highlightjs-lang.min.js"></script>
```

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

Expand All @@ -33,6 +27,7 @@ Download plugin and include file after highlight.js:
```html
<script src="path/to/highlight.min.js"></script>

<!-- Load plugin: -->
<script src="path/to/highlightjs-lang.min.js"></script>
```

Expand All @@ -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:
Expand All @@ -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

Expand All @@ -98,9 +94,11 @@ If you want to override the default language name, you can specify a _overridden

```js
var myOptions = {
// ...
overrideNames: {
cs: 'C#',
}
},
// ...
};
```

Expand All @@ -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.)
Expand Down
2 changes: 1 addition & 1 deletion dist/highlightjs-lang.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@
<script src="dist/highlightjs-lang.min.js"></script>
<script>
hljs.highlightAll();

// init plugin:
hljs.initLangOnLoad(
{
overrideNames: {
cpp: 'C++',
}
dart: 'Dart',
},
fallback: function (lang) {
if (lang === 'php') {
return '$ PHP $';
}
return lang;
},
}
);
</script>
Expand Down
19 changes: 15 additions & 4 deletions src/highlightjs-lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -70,6 +70,7 @@
options = options || {};
return {
overrideNames: getOverrideNamesOption(element, langKey, options),
fallback: getFallbackOption(options),
};
}

Expand All @@ -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);
Expand All @@ -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;
}
Expand All @@ -117,7 +128,7 @@
return langName;
}

return langKey;
return options.fallback(langKey);
}

/**
Expand Down