Skip to content

Commit

Permalink
fix(parser) fixes sublanguage with no rule matches
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgoebel committed Apr 24, 2020
1 parent 2d90a5c commit d5ebc92
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
9 changes: 9 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## Version 10.0.1

Parser Engine Changes:

- (bug) Fix sublanguage with no relevance score (#) [Josh Goebel][]

[Josh Goebel]: https://github.com/yyyc514


## Version 10.0.0

New languages:
Expand Down
24 changes: 18 additions & 6 deletions src/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,22 @@ const HLJS = function(hljs) {
}
}

// returns a valid highlight result, without actually
// doing any actual work, auto highlight starts with
// this and it's possible for small snippets that
// auto-detection may not find a better match
function justTextHighlightResult(code) {
const result = {
relevance: 0,
emitter: new options.__emitter(options),
value: escape(code),
illegal: false,
top: PLAINTEXT_LANGUAGE
};
result.emitter.addText(code)
return result;
}

/*
Highlighting with language detection. Accepts a string with the code to
highlight. Returns an object with the following properties:
Expand All @@ -456,11 +472,7 @@ const HLJS = function(hljs) {
*/
function highlightAuto(code, languageSubset) {
languageSubset = languageSubset || options.languages || Object.keys(languages);
var result = {
relevance: 0,
emitter: new options.__emitter(options),
value: escape(code)
};
var result = justTextHighlightResult(code)
var second_best = result;
languageSubset.filter(getLanguage).filter(autoDetection).forEach(function(name) {
var current = _highlight(name, code, false);
Expand Down Expand Up @@ -589,7 +601,7 @@ const HLJS = function(hljs) {
window.addEventListener('DOMContentLoaded', initHighlighting, false);
}

var PLAINTEXT_LANGUAGE = { disableAutodetect: true };
const PLAINTEXT_LANGUAGE = { disableAutodetect: true, name: 'Plain text' };

function registerLanguage(name, language) {
var lang;
Expand Down

0 comments on commit d5ebc92

Please sign in to comment.