From 2f5590ea7846f0207430daa857a7dbb9cd74930b Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 15 Feb 2021 12:25:15 -0500 Subject: [PATCH] ehn(parser): add unregisterLanguage method (#3009) --- .eslintrc.js | 3 +++ CHANGES.md | 5 +++++ docs/api.rst | 8 +++++++ src/highlight.js | 15 +++++++++++++ test/api/index.js | 1 + test/api/unregisterLanguage.js | 39 ++++++++++++++++++++++++++++++++++ types/index.d.ts | 1 + 7 files changed, 72 insertions(+) create mode 100644 test/api/unregisterLanguage.js diff --git a/.eslintrc.js b/.eslintrc.js index a5470241fd..6027b3ec49 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -90,6 +90,9 @@ module.exports = { }, { files: ["test/**/*.js"], + globals: { + should: "readonly" + }, env: { mocha: true }, diff --git a/CHANGES.md b/CHANGES.md index 36ff62e2f3..c2946f1ecf 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 diff --git a/docs/api.rst b/docs/api.rst index 237c6eb1b9..3c2936b0d4 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -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})`` -------------------------------------------------- diff --git a/src/highlight.js b/src/highlight.js index 9caad5795e..ef03ab36c6 100644 --- a/src/highlight.js +++ b/src/highlight.js @@ -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 */ @@ -916,6 +930,7 @@ const HLJS = function(hljs) { initHighlighting, initHighlightingOnLoad, registerLanguage, + unregisterLanguage, listLanguages, getLanguage, registerAliases, diff --git a/test/api/index.js b/test/api/index.js index 223fbc0e30..b15ed29936 100644 --- a/test/api/index.js +++ b/test/api/index.js @@ -12,6 +12,7 @@ describe('hljs', function() { require('./keywords'); require('./number'); require('./registerAlias'); + require('./unregisterLanguage'); require('./starters'); require('./underscoreIdent'); }); diff --git a/test/api/unregisterLanguage.js b/test/api/unregisterLanguage.js new file mode 100644 index 0000000000..ce0ff00b99 --- /dev/null +++ b/test/api/unregisterLanguage.js @@ -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(); + } + }); +}); diff --git a/types/index.d.ts b/types/index.d.ts index 825ad72301..d5caf9054f 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -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