UNPKG

svelte-rune-highlight

Version:
53 lines (52 loc) 1.93 kB
import { SvelteSet } from 'svelte/reactivity'; /** * Configuration constants for highlighting */ export const HIGHLIGHT_CONSTANTS = { DIGIT_WIDTH: 12, MIN_DIGITS: 2, HIGHLIGHTED_BACKGROUND: 'rgba(254, 241, 96, 0.2)' }; /** * Creates a set of all highlighted line numbers from individual lines and ranges */ export function createHighlightedLinesSet(highlightedLines = [], highlightedRanges = []) { const lines = new SvelteSet(highlightedLines); // Add ranges for (const [start, end] of highlightedRanges) { if (typeof start !== 'number' || typeof end !== 'number' || start < 1 || end < 1) { console.warn('Invalid range:', [start, end]); continue; } for (let i = start; i <= end; i++) { lines.add(i); } } return lines; } /** * Calculates the width needed for line numbers based on the number of lines */ export function calculateLineNumberWidth(lineCount, digitWidth = HIGHLIGHT_CONSTANTS.DIGIT_WIDTH, minDigits = HIGHLIGHT_CONSTANTS.MIN_DIGITS) { const lenDigits = lineCount.toString().length; const len = lenDigits < minDigits ? minDigits : lenDigits; return len * digitWidth; } /** * Escapes HTML special characters */ export function escapeHtml(str) { return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#39;'); } /** * Replaces $lib imports with the specified library name */ export function replaceLibImport(componentString, libraryName = 'svelte-rune-highlight') { if (typeof componentString !== 'string') { return ''; } // Handle $lib and optional subpaths, both quote styles return componentString .replace(/from '\$lib(\/[^']*)?'/g, (_m, subpath = '') => `from '${libraryName}${subpath}'`) .replace(/from "\$lib(\/[^"]*)?"/g, (_m, subpath = '') => `from "${libraryName}${subpath}"`); }