Do not syntax highlight for unknown languages

highlight.js supports ~180 languages, but alas, developers have come up
with even more. Since we do not control the languages developers use,
check that first instead of crashing when an unsupported language is
used.

Change-Id: Id8dbd11d63affe17eb009cd90a7d9e097b164ad4
This commit is contained in:
Ole Rehmsen
2019-01-22 15:12:30 +01:00
parent 27ad9180f3
commit c4902bf92e
2 changed files with 9 additions and 2 deletions

View File

@@ -349,7 +349,8 @@
// To store the result of the syntax highlighter.
let result;
if (this._baseLanguage && baseLine !== undefined) {
if (this._baseLanguage && baseLine !== undefined &&
this._hljs.getLanguage(this._baseLanguage)) {
baseLine = this._workaround(this._baseLanguage, baseLine);
result = this._hljs.highlight(this._baseLanguage, baseLine, true,
state.baseContext);
@@ -357,7 +358,8 @@
state.baseContext = result.top;
}
if (this._revisionLanguage && revisionLine !== undefined) {
if (this._revisionLanguage && revisionLine !== undefined &&
this._hljs.getLanguage(this._revisionLanguage)) {
revisionLine = this._workaround(this._revisionLanguage, revisionLine);
result = this._hljs.highlight(this._revisionLanguage, revisionLine,
true, state.revisionContext);

View File

@@ -50,6 +50,11 @@ limitations under the License.
top: state === undefined ? 1 : state + 1,
};
},
// Return something truthy because this method is used to check if the
// language is supported.
getLanguage(s) {
return {};
},
};
}