@tanstack/highlight
Version:
Tiny class-based syntax highlighting for documentation.
25 lines (24 loc) • 1.06 kB
JavaScript
// Scan each statement once, so selector lookahead cannot repeatedly scan a
// long unfinished stylesheet. Comments and strings are shielded first.
export const stylesheetWords = {
collect(code) {
const ranges = [];
for (const statement of code.matchAll(/[^{};]+/g)) {
const start = statement.index;
const value = statement[0];
if (code[start + value.length] === '{') {
for (const selector of value.matchAll(/[.#][A-Za-z_][\w-]*/g)) {
ranges.push({ start: start + selector.index, end: start + selector.index + selector[0].length, className: 'selector' });
}
}
else {
const property = /^\s*([\w-]+)\s*:(?!:)/.exec(value);
if (property) {
const offset = start + value.indexOf(property[1]);
ranges.push({ start: offset, end: offset + property[1].length, className: 'property' });
}
}
}
return ranges;
},
};