Skip to content

Commit 2fafd0e

Browse files
(parser) Add hljs.registerAlias() public API (#2540)
* Add hljs.registerAlias(alias, languageName) public API * Add .registerAlias() test
1 parent 5c125b9 commit 2fafd0e

File tree

4 files changed

+51
-10
lines changed

4 files changed

+51
-10
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Parser Engine:
44

55
- (parser) Adds `keywords.$pattern` key to grammar definitions (#2519) [Josh Goebel][]
66
- (parser) Adds SHEBANG utility mode [Josh Goebel][]
7+
- (parser) Adds `registerAlias` method (#2540) [Taufik Nurrohman][]
78
- (enh) Added `on:begin` callback for modes (#2261) [Josh Goebel][]
89
- (enh) Added `on:end` callback for modes (#2261) [Josh Goebel][]
910
- (enh) Added ability to programatically ignore begin and end matches (#2261) [Josh Goebel][]

docs/api.rst

+17-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Highlight.js exports a few functions as methods of the ``hljs`` object.
55

66

77
``highlight(languageName, code, ignore_illegals, continuation)``
8-
---------------------------------------------------------
8+
----------------------------------------------------------------
99

1010
Core highlighting function.
1111
Accepts a language name, or an alias, and a string with the code to highlight.
@@ -32,7 +32,7 @@ Returns an object with the following properties:
3232

3333

3434
``highlightAuto(code, languageSubset)``
35-
----------------------------------------
35+
---------------------------------------
3636

3737
Highlighting with language detection.
3838
Accepts a string with the code to highlight and an optional array of language names and aliases restricting detection to only those languages. The subset can also be set with ``configure``, but the local parameter overrides the option if set.
@@ -76,7 +76,7 @@ Configures global options:
7676
* ``classPrefix``: a string prefix added before class names in the generated markup, used for backwards compatibility with stylesheets.
7777
* ``languages``: an array of language names and aliases restricting auto detection to only these languages.
7878
* ``languageDetectRe``: a regex to configure how CSS class names map to language (allows class names like say `color-as-php` vs the default of `language-php`, etc.)
79-
* ``noHighlightRe``: a regex to configure which CSS classes are to be skipped completely
79+
* ``noHighlightRe``: a regex to configure which CSS classes are to be skipped completely.
8080

8181
Accepts an object representing options with the values to updated. Other options don't change
8282
::
@@ -85,15 +85,14 @@ Accepts an object representing options with the values to updated. Other options
8585
tabReplace: ' ', // 4 spaces
8686
classPrefix: '' // don't append class prefix
8787
// … other options aren't changed
88-
})
88+
});
8989
hljs.initHighlighting();
9090

9191

9292
``initHighlighting()``
9393
----------------------
9494

95-
Applies highlighting to all ``<pre><code>..</code></pre>`` blocks on a page.
96-
95+
Applies highlighting to all ``<pre><code>...</code></pre>`` blocks on a page.
9796

9897

9998
``initHighlightingOnLoad()``
@@ -113,13 +112,21 @@ Adds new language to the library under the specified name. Used mostly internall
113112
to use common regular expressions defined within it.
114113

115114

115+
``registerAlias(alias|aliases, {languageName})``
116+
------------------------------------------------
117+
118+
Adds new language alias or aliases to the library for the specified language name defined under ``languageName`` key.
119+
120+
* ``alias|aliases``: a string or array with the name of alias being registered
121+
* ``languageName``: the language name as specified by ``registerLanguage``.
122+
123+
116124
``listLanguages()``
117-
----------------------------
125+
-------------------
118126

119127
Returns the languages names list.
120128

121129

122-
123130
.. _getLanguage:
124131

125132

@@ -132,7 +139,7 @@ Returns the language object if found, ``undefined`` otherwise.
132139

133140

134141
``requireLanguage(name)``
135-
---------------------
142+
-------------------------
136143

137144
Looks up a language by name or alias.
138145

@@ -150,5 +157,5 @@ Enables *debug/development* mode. **This mode purposely makes Highlight.js more
150157

151158
For example, if a new version suddenly had a serious bug (or breaking change) that affected only a single language:
152159

153-
* **In Safe Mode**: All other languages would continue to highlight just fine. The broken language would appear as a code block, but without any highlighting (as if it were plaintext).
160+
* **In Safe Mode**: All other languages would continue to highlight just fine. The broken language would appear as a code block, but without any highlighting (as if it were plaintext).
154161
* **In Debug Mode**: All highlighting would stop when an error was encountered and a JavaScript error would be thrown.

src/highlight.js

+9
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,14 @@ const HLJS = function(hljs) {
685685
return languages[name] || languages[aliases[name]];
686686
}
687687

688+
function registerAlias(alias, {languageName}) {
689+
let list = alias;
690+
if (typeof list === 'string') {
691+
list = [alias]
692+
}
693+
list.forEach(alias => aliases[alias] = languageName);
694+
}
695+
688696
function autoDetection(name) {
689697
var lang = getLanguage(name);
690698
return lang && !lang.disableAutodetect;
@@ -716,6 +724,7 @@ const HLJS = function(hljs) {
716724
registerLanguage,
717725
listLanguages,
718726
getLanguage,
727+
registerAlias,
719728
requireLanguage,
720729
autoDetection,
721730
inherit,

test/api/registerAlias.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
const hljs = require('../../build');
4+
const should = require('should');
5+
6+
describe('.registerAlias()', () => {
7+
it('should get an existing language by alias', () => {
8+
hljs.registerAlias('jquery', {
9+
languageName: 'javascript'
10+
});
11+
const result = hljs.getLanguage('jquery');
12+
13+
result.should.be.instanceOf(Object);
14+
});
15+
16+
it('should get an existing language by aliases', () => {
17+
hljs.registerAlias(['jquery', 'jqueryui'], {
18+
languageName: 'javascript'
19+
});
20+
const result = hljs.getLanguage('jquery');
21+
22+
result.should.be.instanceOf(Object);
23+
});
24+
});

0 commit comments

Comments
 (0)