diff --git a/CHANGES.md b/CHANGES.md index e6348fdfe3..2b74d3d100 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,7 +4,7 @@ Parser Engine: - (parser) Adds `keywords.$pattern` key to grammar definitions (#2519) [Josh Goebel][] - (parser) Adds SHEBANG utility mode [Josh Goebel][] -- (parser) Adds `registerAlias` method (#2540) [Taufik Nurrohman][] +- (parser) Adds `registerAliases` method (#2540) [Taufik Nurrohman][] - (enh) Added `on:begin` callback for modes (#2261) [Josh Goebel][] - (enh) Added `on:end` callback for modes (#2261) [Josh Goebel][] - (enh) Added ability to programatically ignore begin and end matches (#2261) [Josh Goebel][] diff --git a/docs/api.rst b/docs/api.rst index a219bfa9ea..41f7014ad6 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -112,8 +112,8 @@ Adds new language to the library under the specified name. Used mostly internall to use common regular expressions defined within it. -``registerAlias(alias|aliases, {languageName})`` ------------------------------------------------- +``registerAliases(alias|aliases, {languageName})`` +-------------------------------------------------- Adds new language alias or aliases to the library for the specified language name defined under ``languageName`` key. diff --git a/src/highlight.js b/src/highlight.js index bc34fb4fc3..2b3f776429 100644 --- a/src/highlight.js +++ b/src/highlight.js @@ -658,7 +658,7 @@ const HLJS = function(hljs) { lang.rawDefinition = language.bind(null, hljs); if (lang.aliases) { - lang.aliases.forEach(function(alias) { aliases[alias] = name; }); + registerAliases(lang.aliases, { languageName: name }); } } @@ -685,12 +685,11 @@ const HLJS = function(hljs) { return languages[name] || languages[aliases[name]]; } - function registerAlias(alias, {languageName}) { - let list = alias; - if (typeof list === 'string') { - list = [alias] + function registerAliases(aliasList, {languageName}) { + if (typeof aliasList === 'string') { + aliasList = [aliasList] } - list.forEach(alias => aliases[alias] = languageName); + aliasList.forEach(alias => aliases[alias] = languageName); } function autoDetection(name) { @@ -724,7 +723,7 @@ const HLJS = function(hljs) { registerLanguage, listLanguages, getLanguage, - registerAlias, + registerAliases, requireLanguage, autoDetection, inherit, diff --git a/test/api/index.js b/test/api/index.js index 043bb65b4d..0e4b596dcb 100644 --- a/test/api/index.js +++ b/test/api/index.js @@ -12,4 +12,5 @@ describe('hljs', function() { require('./highlight'); require('./fixmarkup'); require('./keywords'); + require('./registerAlias'); }); diff --git a/test/api/registerAlias.js b/test/api/registerAlias.js index 3e03136fe4..aede3ebb7a 100644 --- a/test/api/registerAlias.js +++ b/test/api/registerAlias.js @@ -3,9 +3,9 @@ const hljs = require('../../build'); const should = require('should'); -describe('.registerAlias()', () => { +describe('.registerAliases()', () => { it('should get an existing language by alias', () => { - hljs.registerAlias('jquery', { + hljs.registerAliases('jquery', { languageName: 'javascript' }); const result = hljs.getLanguage('jquery'); @@ -14,7 +14,7 @@ describe('.registerAlias()', () => { }); it('should get an existing language by aliases', () => { - hljs.registerAlias(['jquery', 'jqueryui'], { + hljs.registerAliases(['jquery', 'jqueryui'], { languageName: 'javascript' }); const result = hljs.getLanguage('jquery');