Skip to content

Commit dc8b23f

Browse files
committed
Finalize fallback option
1 parent 4e5e10d commit dc8b23f

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ After version 1.1.0 plugin has optional parameter `options` - for custom setup:
7777
version | name | type | default value | description
7878
--------|---------------|---------|---------------|-----------------------
7979
v1.1.0 | overrideNames | object | {} | [Override the default language names](#overrideNames)
80+
v1.1.1 | fallback | func(str): str | (lang) => lang | [Fallback to convert unknown names](#fallback)
8081

8182
### Examples of using
8283

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

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

123144
(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: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,8 @@
9898
: defaultFallbackOption;
9999
}
100100

101-
function defaultFallbackOption (codeLang) {
102-
if (!!codeLang) {
103-
var lang = codeLang.trim();
104-
if (lang.length > 0) {
105-
lang[0] = lang[0].toUpperCase();
106-
return lang;
107-
}
108-
}
109-
return codeLang;
101+
function defaultFallbackOption (langKey) {
102+
return langKey;
110103
}
111104

112105
function getLangNameFromElement (element) {

0 commit comments

Comments
 (0)