Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix unexpected highlight words for custom meta properties #53

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export default function rehypePrettyCode(options = {}) {
}

// TODO: allow escape characters to break out of highlighting
const stippedValue = value.replace(/{:[a-zA-Z.-]+}/, '');
const strippedValue = value.replace(/{:[a-zA-Z.-]+}/, '');
const meta = value.match(/{:([a-zA-Z.-]+)}$/)?.[1];

if (!meta) {
Expand All @@ -136,11 +136,11 @@ export default function rehypePrettyCode(options = {}) {
)?.settings.foreground ?? 'inherit';

trees[mode] = hastParser.parse(
`<pre><code><span style="color:${color}">${stippedValue}</span></code></pre>`
`<pre><code><span style="color:${color}">${strippedValue}</span></code></pre>`
);
} else {
trees[mode] = hastParser.parse(
highlighter.codeToHtml(stippedValue, meta)
highlighter.codeToHtml(strippedValue, meta)
);
}
}
Expand Down Expand Up @@ -169,16 +169,15 @@ export default function rehypePrettyCode(options = {}) {
node.children[0].data?.meta ?? node.children[0].properties.metastring;
const titleMatch = meta?.match(/title="(.+)"/);
const title = titleMatch?.[1] ?? null;
const titleString = titleMatch?.[0] ?? '';
const metaWithoutTitle = meta?.replace(titleString, '');
const cleanedMeta = meta?.replace(/\w+="[^"]*"/g, '');

const lineNumbers = meta
? rangeParser(metaWithoutTitle.match(/{(.*)}/)?.[1] ?? '')
? rangeParser(cleanedMeta.match(/{(.*)}/)?.[1] ?? '')
: [];

let word = [];
const wordMatch = metaWithoutTitle
? [...metaWithoutTitle.matchAll(/\/(.*?)\//g)]
const wordMatch = cleanedMeta
? [...cleanedMeta.matchAll(/\/(.*?)\//g)]
: undefined;
if (Array.isArray(wordMatch)) {
wordMatch.forEach((name, index) => {
Expand All @@ -188,8 +187,8 @@ export default function rehypePrettyCode(options = {}) {

let wordNumbers = [];
if (meta) {
const wordNumbersMatch = metaWithoutTitle
? [...metaWithoutTitle.matchAll(/\/.*?\/(\S*)/g)]
const wordNumbersMatch = cleanedMeta
? [...cleanedMeta.matchAll(/\/.*?\/(\S*)/g)]
: undefined;
if (Array.isArray(wordNumbersMatch)) {
wordNumbersMatch.forEach((name, index) => {
Expand Down