Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ehn(api): add unregisterLanguage method #3009

Merged
merged 4 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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];
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
for(const alias of Object.keys(aliases)) {
if(aliases[alias] === languageName) {
delete aliases[alias];
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

/**
* @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');
});
38 changes: 38 additions & 0 deletions test/api/unregisterLanguage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"use strict";

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

let grammar = function () {
return {
contains: [{ beginKeywords: "class" }],
};
};

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

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

(result == null).should.be.true();
});

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

{
const result = hljs.getLanguage("jquery");
(result == null).should.be.false();
}
hljs.unregisterLanguage("test");
{
const result = hljs.getLanguage("jquery");
(result == null).should.be.true();
}
});
});
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