Skip to content

Commit ef84159

Browse files
authored
Merge pull request #2 from wcoder/feature/fallback-option
2 parents 8b0b91a + c8bc04a commit ef84159

File tree

4 files changed

+54
-17
lines changed

4 files changed

+54
-17
lines changed

README.md

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,13 @@
1212
npm i highlightjs-lang.js
1313
```
1414

15-
### Bower
16-
17-
```bash
18-
bower install highlightjs-lang
19-
```
20-
2115
#### Getting the library from CDN
2216

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

27-
highlightjs-lang.js 1.1.0 is known to work with highlight.js 11.3.1.
21+
highlightjs-lang.js 1.1.0 is known to work with highlight.js 11.3.1+
2822

2923
## Usage
3024

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

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

@@ -57,7 +52,7 @@ Initialize plugin after highlight.js:
5752
```js
5853
hljs.highlightAll();
5954

60-
hljs.initLangOnLoad();
55+
hljs.initLangOnLoad(); // <-- init plugin
6156
```
6257

6358
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:
7772
version | name | type | default value | description
7873
--------|---------------|---------|---------------|-----------------------
7974
v1.1.0 | overrideNames | object | {} | [Override the default language names](#overrideNames)
75+
v1.1.1 | fallback | func(str): str | (lang) => lang | [Fallback to convert unknown names](#fallback)
8076

8177
### Examples of using
8278

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

9995
```js
10096
var myOptions = {
97+
// ...
10198
overrideNames: {
10299
cs: 'C#',
103-
}
100+
},
101+
// ...
104102
};
105103
```
106104

@@ -118,6 +116,26 @@ In both cases language name will be `C#`.
118116

119117
> [List of default language names](https://github.com/wcoder/highlightjs-lang.js/blob/master/src/highlightjs-lang.js#L4-L10)
120118
119+
### fallback
120+
121+
Specifying the desired format for undeclared language names:
122+
123+
```js
124+
var myOptions = {
125+
// ...
126+
fallback: function (lang) {
127+
return '~~' + lang;
128+
},
129+
// ...
130+
};
131+
```
132+
133+
Convert all undeclared language names to names with `~~` prefix:
134+
135+
```
136+
xyz -> ~~xyz
137+
```
138+
121139
## Skipping some blocks
122140

123141
(Applies to `hljs.initLangOnLoad()` initialization only.)

dist/highlightjs-lang.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,19 @@
1717
<script src="dist/highlightjs-lang.min.js"></script>
1818
<script>
1919
hljs.highlightAll();
20+
21+
// init plugin:
2022
hljs.initLangOnLoad(
2123
{
2224
overrideNames: {
23-
cpp: 'C++',
24-
}
25+
dart: 'Dart',
26+
},
27+
fallback: function (lang) {
28+
if (lang === 'php') {
29+
return '$ PHP $';
30+
}
31+
return lang;
32+
},
2533
}
2634
);
2735
</script>

src/highlightjs-lang.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
if (lang !== '') {
5757
var langPanel = document.createElement('div');
5858
langPanel.className = 'hljs-lang';
59-
langPanel.textContent = convertLangName(lang, internalOptions.overrideNames);
59+
langPanel.textContent = convertLangName(lang, internalOptions);
6060
element.parentNode.insertBefore(langPanel, element);
6161
}
6262
}
@@ -70,6 +70,7 @@
7070
options = options || {};
7171
return {
7272
overrideNames: getOverrideNamesOption(element, langKey, options),
73+
fallback: getFallbackOption(options),
7374
};
7475
}
7576

@@ -91,6 +92,16 @@
9192
return overrideNames;
9293
}
9394

95+
function getFallbackOption (options) {
96+
return !!options.fallback
97+
? options.fallback
98+
: defaultFallbackOption;
99+
}
100+
101+
function defaultFallbackOption (langKey) {
102+
return langKey;
103+
}
104+
94105
function getLangNameFromElement (element) {
95106
var classes = element.className.split(' ');
96107
var lang = getLangNameFromClasses(classes);
@@ -105,9 +116,9 @@
105116
return '';
106117
}
107118

108-
function convertLangName(langKey, overrideNamesMap)
119+
function convertLangName(langKey, options)
109120
{
110-
var overriddenLangName = overrideNamesMap[langKey];
121+
var overriddenLangName = options.overrideNames[langKey];
111122
if (!!overriddenLangName) {
112123
return overriddenLangName;
113124
}
@@ -117,7 +128,7 @@
117128
return langName;
118129
}
119130

120-
return langKey;
131+
return options.fallback(langKey);
121132
}
122133

123134
/**

0 commit comments

Comments
 (0)