rehype-highlight-code-lines
Advanced tools
Comparing version
@@ -10,2 +10,3 @@ import type { Plugin } from "unified"; | ||
dataHighlightLines?: string; | ||
metastring?: string | null | undefined; | ||
} | ||
@@ -12,0 +13,0 @@ } |
@@ -276,3 +276,3 @@ import { visit } from "unist-util-visit"; | ||
/** the part of correcting language and meta */ | ||
let meta = code.data?.meta?.toLowerCase().trim() ?? ""; | ||
let meta = (code.data?.meta || code.properties?.metastring || "").toLowerCase().trim(); | ||
const language = getLanguage(code.properties.className); | ||
@@ -284,18 +284,17 @@ if (language && | ||
language.startsWith("keepouterblankline"))) { | ||
// add specifiers to meta | ||
// add directives to meta | ||
meta = (language + " " + meta).trim(); | ||
// correct the code's meta | ||
code.data && (code.data.meta = (language + " " + code.data.meta).trim()); | ||
// remove all classnames like hljs, lang-{1,3}, language-showLineNumbers, because of false positive | ||
// remove classnames like hljs, lang-{1,3}, language-showLineNumbers, because of false positive | ||
code.properties.className = undefined; | ||
} | ||
/** the part of getting directives from code.properties.className */ | ||
const keywords = ["showlinenumbers", "nolinenumbers", "keepouterblankline"]; | ||
const classNames = code.properties.className | ||
const directives = code.properties.className | ||
?.map((cls) => cls.toLowerCase().replaceAll("-", "")) | ||
.filter((cls) => keywords.includes(cls)); | ||
/** the part of defining the directive for line numbering */ | ||
const directiveNoLineNumbers = meta.includes("nolinenumbers") || Boolean(classNames?.includes("nolinenumbers")); | ||
/** the part of defining the line numbering directive */ | ||
const directiveNoLineNumbers = meta.includes("nolinenumbers") || Boolean(directives?.includes("nolinenumbers")); | ||
const directiveShowLineNumbers = settings.showLineNumbers || | ||
meta.includes("showlinenumbers") || | ||
Boolean(classNames?.includes("showlinenumbers")); | ||
Boolean(directives?.includes("showlinenumbers")); | ||
let directiveLineNumbering = directiveNoLineNumbers | ||
@@ -312,5 +311,2 @@ ? false | ||
const { dataStartNumbering } = code.properties; | ||
if ("dataStartNumbering" in code.properties) { | ||
code.properties["dataStartNumbering"] = undefined; | ||
} | ||
if (!directiveNoLineNumbers && | ||
@@ -321,3 +317,3 @@ dataStartNumbering !== "" && | ||
} | ||
/** the part of defining the directive for line highlighting */ | ||
/** the part of defining the line highlighting directive */ | ||
// find number range string within curly braces and parse it | ||
@@ -333,5 +329,2 @@ const directiveLineHighlighting = []; | ||
const { dataHighlightLines } = code.properties; | ||
if ("dataHighlightLines" in code.properties) { | ||
code.properties["dataHighlightLines"] = undefined; | ||
} | ||
if (dataHighlightLines) { | ||
@@ -341,17 +334,17 @@ const range = rangeParser(dataHighlightLines.replace(/\s/g, "")); | ||
} | ||
/** the part of defining the directive for line trimming */ | ||
/** the part of defining the directive for outer blank lines */ | ||
// find the directive for trimming blank lines | ||
const directiveKeepOuterBlankLine = settings.keepOuterBlankLine || | ||
/keepouterblankline/.test(meta) || | ||
Boolean(classNames?.includes("keepouterblankline")); | ||
/** the part of cleaning of code properties */ | ||
code.properties.className = code.properties.className?.filter((cls) => !keywords.includes(cls.toLowerCase().replaceAll("-", ""))); | ||
if (isStringArray(code.properties.className) && code.properties.className.length === 0) { | ||
code.properties.className = undefined; | ||
} | ||
// if nothing to do for numbering, highlihting or trimming, just return; | ||
if (directiveLineNumbering === false && | ||
directiveLineHighlighting.length === 0 | ||
// directiveKeepOuterBlankLine === false | ||
) { | ||
Boolean(directives?.includes("keepouterblankline")); | ||
/** the part of cleaning the code properties */ | ||
const classnamesFiltered = code.properties.className?.filter((cls) => !keywords.includes(cls.toLowerCase().replaceAll("-", ""))); | ||
code.properties.className = classnamesFiltered?.length ? classnamesFiltered : undefined; | ||
["dataStartNumbering", "dataHighlightLines", "metastring"].forEach((property) => { | ||
if (property in code.properties) | ||
code.properties[property] = undefined; | ||
}); | ||
/** the part of main logic */ | ||
// if nothing to do for line numbering or line highlihting, just return; | ||
if (directiveLineNumbering === false && directiveLineHighlighting.length === 0) { | ||
return; | ||
@@ -358,0 +351,0 @@ } |
{ | ||
"name": "rehype-highlight-code-lines", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Rehype plugin to add line numbers to code blocks and allow highlighting of desired code lines", | ||
@@ -80,3 +80,3 @@ "type": "module", | ||
"typescript": "^5.7.3", | ||
"typescript-eslint": "^8.24.0", | ||
"typescript-eslint": "^8.24.1", | ||
"unified": "^11.0.5", | ||
@@ -83,0 +83,0 @@ "unist-util-remove-position": "^5.0.0", |
@@ -17,2 +17,3 @@ import type { Plugin } from "unified"; | ||
dataHighlightLines?: string; | ||
metastring?: string | null | undefined; | ||
} | ||
@@ -396,3 +397,3 @@ } | ||
let meta = code.data?.meta?.toLowerCase().trim() ?? ""; | ||
let meta = (code.data?.meta || code.properties?.metastring || "").toLowerCase().trim(); | ||
@@ -408,22 +409,21 @@ const language = getLanguage(code.properties.className); | ||
) { | ||
// add specifiers to meta | ||
// add directives to meta | ||
meta = (language + " " + meta).trim(); | ||
// correct the code's meta | ||
code.data && (code.data.meta = (language + " " + code.data.meta).trim()); | ||
// remove all classnames like hljs, lang-{1,3}, language-showLineNumbers, because of false positive | ||
// remove classnames like hljs, lang-{1,3}, language-showLineNumbers, because of false positive | ||
code.properties.className = undefined; | ||
} | ||
/** the part of getting directives from code.properties.className */ | ||
const keywords = ["showlinenumbers", "nolinenumbers", "keepouterblankline"]; | ||
const classNames = code.properties.className | ||
const directives = code.properties.className | ||
?.map((cls) => cls.toLowerCase().replaceAll("-", "")) | ||
.filter((cls) => keywords.includes(cls)); | ||
/** the part of defining the directive for line numbering */ | ||
/** the part of defining the line numbering directive */ | ||
const directiveNoLineNumbers = | ||
meta.includes("nolinenumbers") || Boolean(classNames?.includes("nolinenumbers")); | ||
meta.includes("nolinenumbers") || Boolean(directives?.includes("nolinenumbers")); | ||
@@ -433,3 +433,3 @@ const directiveShowLineNumbers = | ||
meta.includes("showlinenumbers") || | ||
Boolean(classNames?.includes("showlinenumbers")); | ||
Boolean(directives?.includes("showlinenumbers")); | ||
@@ -450,5 +450,2 @@ let directiveLineNumbering: boolean | number = directiveNoLineNumbers | ||
const { dataStartNumbering } = code.properties; | ||
if ("dataStartNumbering" in code.properties) { | ||
code.properties["dataStartNumbering"] = undefined; | ||
} | ||
@@ -463,3 +460,3 @@ if ( | ||
/** the part of defining the directive for line highlighting */ | ||
/** the part of defining the line highlighting directive */ | ||
@@ -478,5 +475,2 @@ // find number range string within curly braces and parse it | ||
const { dataHighlightLines } = code.properties; | ||
if ("dataHighlightLines" in code.properties) { | ||
code.properties["dataHighlightLines"] = undefined; | ||
} | ||
@@ -488,3 +482,3 @@ if (dataHighlightLines) { | ||
/** the part of defining the directive for line trimming */ | ||
/** the part of defining the directive for outer blank lines */ | ||
@@ -495,20 +489,20 @@ // find the directive for trimming blank lines | ||
/keepouterblankline/.test(meta) || | ||
Boolean(classNames?.includes("keepouterblankline")); | ||
Boolean(directives?.includes("keepouterblankline")); | ||
/** the part of cleaning of code properties */ | ||
/** the part of cleaning the code properties */ | ||
code.properties.className = code.properties.className?.filter( | ||
const classnamesFiltered = code.properties.className?.filter( | ||
(cls) => !keywords.includes(cls.toLowerCase().replaceAll("-", "")), | ||
); | ||
if (isStringArray(code.properties.className) && code.properties.className.length === 0) { | ||
code.properties.className = undefined; | ||
} | ||
code.properties.className = classnamesFiltered?.length ? classnamesFiltered : undefined; | ||
// if nothing to do for numbering, highlihting or trimming, just return; | ||
if ( | ||
directiveLineNumbering === false && | ||
directiveLineHighlighting.length === 0 | ||
// directiveKeepOuterBlankLine === false | ||
) { | ||
["dataStartNumbering", "dataHighlightLines", "metastring"].forEach((property) => { | ||
if (property in code.properties) code.properties[property] = undefined; | ||
}); | ||
/** the part of main logic */ | ||
// if nothing to do for line numbering or line highlihting, just return; | ||
if (directiveLineNumbering === false && directiveLineHighlighting.length === 0) { | ||
return; | ||
@@ -515,0 +509,0 @@ } |
Sorry, the diff of this file is not supported yet
65113
-0.76%805
-1.71%