Skip to content

Commit f6813cc

Browse files
authored
fix(parser) fixes sublanguage with no rule matches (#2506)
* fix(parser) fixes sublanguage with no rule matches Resolves #2504.
1 parent 3875088 commit f6813cc

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

CHANGES.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## Version 10.0.1
2+
3+
Parser Engine Changes:
4+
5+
- (bug) Fix sublanguage with no relevance score (#2506) [Josh Goebel][]
6+
7+
[Josh Goebel]: https://github.com/yyyc514
8+
9+
110
## Version 10.0.0
211

312
New languages:

src/highlight.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,22 @@ const HLJS = function(hljs) {
443443
}
444444
}
445445

446+
// returns a valid highlight result, without actually
447+
// doing any actual work, auto highlight starts with
448+
// this and it's possible for small snippets that
449+
// auto-detection may not find a better match
450+
function justTextHighlightResult(code) {
451+
const result = {
452+
relevance: 0,
453+
emitter: new options.__emitter(options),
454+
value: escape(code),
455+
illegal: false,
456+
top: PLAINTEXT_LANGUAGE
457+
};
458+
result.emitter.addText(code)
459+
return result;
460+
}
461+
446462
/*
447463
Highlighting with language detection. Accepts a string with the code to
448464
highlight. Returns an object with the following properties:
@@ -456,11 +472,7 @@ const HLJS = function(hljs) {
456472
*/
457473
function highlightAuto(code, languageSubset) {
458474
languageSubset = languageSubset || options.languages || Object.keys(languages);
459-
var result = {
460-
relevance: 0,
461-
emitter: new options.__emitter(options),
462-
value: escape(code)
463-
};
475+
var result = justTextHighlightResult(code)
464476
var second_best = result;
465477
languageSubset.filter(getLanguage).filter(autoDetection).forEach(function(name) {
466478
var current = _highlight(name, code, false);
@@ -589,7 +601,7 @@ const HLJS = function(hljs) {
589601
window.addEventListener('DOMContentLoaded', initHighlighting, false);
590602
}
591603

592-
var PLAINTEXT_LANGUAGE = { disableAutodetect: true };
604+
const PLAINTEXT_LANGUAGE = { disableAutodetect: true, name: 'Plain text' };
593605

594606
function registerLanguage(name, language) {
595607
var lang;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span>foo();<span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
2+
<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span>booger<span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
3+
<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span>hjk<span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script>foo();</script>
2+
<script>booger</script>
3+
<script>hjk</script>

0 commit comments

Comments
 (0)