Skip to content

Commit

Permalink
ehn(parser): add unregisterLanguage method (#3009)
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Feb 15, 2021
1 parent 742e87b commit 2f5590e
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ module.exports = {
},
{
files: ["test/**/*.js"],
globals: {
should: "readonly"
},
env: {
mocha: true
},
Expand Down
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ Language grammar improvements:
- enh(php) Add `mixed` to list of keywords (#2997) [Ayesh][]
- enh(php) Add support binary, octal, hex and scientific numerals with underscore separator support (#2997) [Ayesh][]

API:

- enh(api) add `unregisterLanguage` method (#3009) [Antoine du Hamel][]

[Stef Levesque]: https://github.com/stef-levesque
[Josh Goebel]: https://github.com/joshgoebel
[John Cheung]: https://github.com/Real-John-Cheung
[xDGameStudios]: https://github.com/xDGameStudios
[Ayesh]: https://github.com/Ayesh
[Vyron Vasileiadis]: https://github.com/fedonman
[Antoine du Hamel]: https://github.com/aduh95

## Version 10.6.0

Expand Down
8 changes: 8 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ Adds new language to the library under the specified name. Used mostly internall
to use common regular expressions defined within it.


``unregisterLanguage(languageName)``
------------------------------------

Removes a language and its aliases from the library. Used mostly internally.

* ``languageName``: a string with the name of the language being removed.


``registerAliases(alias|aliases, {languageName})``
--------------------------------------------------

Expand Down
15 changes: 15 additions & 0 deletions src/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,20 @@ const HLJS = function(hljs) {
}
}

/**
* Remove a language grammar module
*
* @param {string} languageName
*/
function unregisterLanguage(languageName) {
delete languages[languageName];
for (const alias of Object.keys(aliases)) {
if (aliases[alias] === languageName) {
delete aliases[alias];
}
}
}

/**
* @returns {string[]} List of language internal names
*/
Expand Down Expand Up @@ -916,6 +930,7 @@ const HLJS = function(hljs) {
initHighlighting,
initHighlightingOnLoad,
registerLanguage,
unregisterLanguage,
listLanguages,
getLanguage,
registerAliases,
Expand Down
1 change: 1 addition & 0 deletions test/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('hljs', function() {
require('./keywords');
require('./number');
require('./registerAlias');
require('./unregisterLanguage');
require('./starters');
require('./underscoreIdent');
});
39 changes: 39 additions & 0 deletions test/api/unregisterLanguage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict";

const hljs = require("../../build");

const jQuery = function() {
return {
name: "jQuery",
contains: [{ beginKeywords: "class" }]
};
};

describe(".unregisterLanguage()", () => {
beforeEach(() => {
hljs.registerLanguage("test", jQuery);
});

it("should remove an existing language", () => {
hljs.unregisterLanguage("test");
const result = hljs.getLanguage("test");

should(result).be.undefined();
});

it("should remove an existing language and its aliases", () => {
hljs.registerAliases(["jquery", "jqueryui"], {
languageName: "test"
});

{
const result = hljs.getLanguage("jquery");
should(result.name).equal("jQuery");
}
hljs.unregisterLanguage("test");
{
const result = hljs.getLanguage("jquery");
should(result).be.undefined();
}
});
});
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface PublicApi {
initHighlightingOnLoad: () => void
highlightAll: () => void
registerLanguage: (languageName: string, language: LanguageFn) => void
unregisterLanguage: (languageName: string) => void
listLanguages: () => string[]
registerAliases: (aliasList: string | string[], { languageName } : {languageName: string}) => void
getLanguage: (languageName: string) => Language | undefined
Expand Down

0 comments on commit 2f5590e

Please sign in to comment.