Skip to content

Commit 517d2da

Browse files
committed
Implement skip: true
The new property allows crude parsing within sub-languages to parse out the whole buffer before actually running processBuffer().
1 parent ef69ef3 commit 517d2da

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

docs/reference.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,32 @@ The value of the attribute controls which language or languages will be used for
296296
* language name: explicit highlighting with the specified language
297297
* empty array: auto detection with all the languages available
298298
* array of language names: auto detection constrained to the specified set
299+
300+
skip
301+
^^^^
302+
303+
**type**: boolean
304+
305+
Skips any markup processing for the mode ensuring that it remains a part of its
306+
parent buffer along with the starting and the ending lexemes. This works in
307+
conjunction with the parent's :ref:`subLanguage` when it requires complex
308+
parsing.
309+
310+
Consider parsing JSX inside JavaScript::
311+
312+
var x = <node> text <child/> text </node>;
313+
314+
You have to correctly balance opening and ending angle brackets to make sure
315+
that the entire XML node is parsed out before it can be highlighted with a
316+
sub-language::
317+
318+
{
319+
begin: /</, end: />/,
320+
subLanguague: 'xml',
321+
contains: [
322+
{begin: /</, end: />/, skip: true, contains: ['self']}
323+
]
324+
}
325+
326+
Without ``skip: true`` every angle bracket within the root node would cause the
327+
parser to drop out back into JavaScript.

src/highlight.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -393,12 +393,16 @@ https://highlightjs.org/
393393

394394
var new_mode = subMode(lexeme, top);
395395
if (new_mode) {
396-
if (new_mode.excludeBegin) {
396+
if (new_mode.skip) {
397397
mode_buffer += lexeme;
398-
}
399-
processBuffer();
400-
if (!new_mode.returnBegin && !new_mode.excludeBegin) {
401-
mode_buffer = lexeme;
398+
} else {
399+
if (new_mode.excludeBegin) {
400+
mode_buffer += lexeme;
401+
}
402+
processBuffer();
403+
if (!new_mode.returnBegin && !new_mode.excludeBegin) {
404+
mode_buffer = lexeme;
405+
}
402406
}
403407
startNewMode(new_mode, lexeme);
404408
return new_mode.returnBegin ? 0 : lexeme.length;
@@ -407,18 +411,24 @@ https://highlightjs.org/
407411
var end_mode = endOfMode(top, lexeme);
408412
if (end_mode) {
409413
var origin = top;
410-
if (!(origin.returnEnd || origin.excludeEnd)) {
414+
if (origin.skip) {
411415
mode_buffer += lexeme;
412-
}
413-
processBuffer();
414-
if (origin.excludeEnd) {
415-
mode_buffer = lexeme;
416+
} else {
417+
if (!(origin.returnEnd || origin.excludeEnd)) {
418+
mode_buffer += lexeme;
419+
}
420+
processBuffer();
421+
if (origin.excludeEnd) {
422+
mode_buffer = lexeme;
423+
}
416424
}
417425
do {
418426
if (top.className) {
419427
result += '</span>';
420428
}
421-
relevance += top.relevance;
429+
if (!top.skip) {
430+
relevance += top.relevance;
431+
}
422432
top = top.parent;
423433
} while (top != end_mode.parent);
424434
if (end_mode.starts) {

0 commit comments

Comments
 (0)