Skip to content

Commit

Permalink
Flag improper use
Browse files Browse the repository at this point in the history
  • Loading branch information
OnkarRuikar committed Jun 3, 2022
1 parent 91e096b commit fa5135a
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions build/syntax-highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,24 @@ function syntaxHighlight($, doc) {
}

const addLineNumbers = className.match(/linenumbers/);
const highlightsTag = className.match(/highlight[-\d]+/g);
let highlights;
if (highlightsTag) {
highlights = highlightsTag[0].match(/(?<=-)\d+/g);
highlights = highlights ? highlights : [];
}

const code = $pre.text();
let html = "";

if (!addLineNumbers) {
if (highlightsTag) {
console.warn(
`Tag 'linenumbers' must be present to use '${highlightsTag}'.`
);
}
html = Prism.highlight(code, grammar, language);
} else {
let highlights = className.match(/(?<=-)\d+/g);
highlights = highlights ? highlights : [];

const env = {
code: code,
grammar: grammar,
Expand Down Expand Up @@ -163,8 +172,14 @@ Prism.plugins.enhance = {

if (children.length > 1) {
newList.push(this.createLineToken(children, env, lineNo));
lineNo++;
}

const outOfRangeNos = env.highlights.filter((h) => h >= lineNo);
if (outOfRangeNos.length > 0) {
outOfRangeNos.sort((a, b) => a - b);
console.warn(`Can not highlight lines: ${outOfRangeNos.join(", ")}`);
}
env.tokens = newList;
}
},
Expand Down

0 comments on commit fa5135a

Please sign in to comment.