Skip to content

Commit

Permalink
Use a placeholder map. Push Vars
Browse files Browse the repository at this point in the history
  • Loading branch information
ptgott committed Nov 18, 2024
1 parent ec5a11c commit 262b46a
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions server/rehype-hljs-var.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,24 @@ export const rehypeVarInHLJS = (
// children. Otherwise add a text node or a span, depending on the
// value's parent.
let newChildren: Array<MdxJsxFlowElement | Element> = [];

// Assemble a map of indexes to their corresponding placeholders so we
// can tell whether a given index falls within a placeholder.
const placeholderIndices = new Map();
placeholders.forEach((p) => {
placeholderIndices.set(p.index, p);
});

let valueIdx = 0;
while (valueIdx < hljsSpanValue.length) {
if (placeholderIndices.has(valueIdx)) {
const placeholder = valueIdx + placeholderIndices.get(valueIdx);
valueIdx += placeholder.length;
newChildren.push(placeholdersToVars[placeholderValue]);
continue;
}
}

// If there is content before the first Var, separate it into a new hljs
// span.
if (placeholders[0].index > 0) {
Expand Down

0 comments on commit 262b46a

Please sign in to comment.