| import { |
| AMPERSAND_REGEX, |
| DEFAULT_LANGUAGE, |
| FENCE_PATTERN, |
| GT_REGEX, |
| LANG_PATTERN, |
| LT_REGEX, |
| NEWLINE, |
| TRIM_LEADING_PADDING_REGEX, |
| TRIM_TRAILING_PADDING_REGEX |
| } from '$lib/constants'; |
| import hljs from 'highlight.js'; |
|
|
| export interface IncompleteCodeBlock { |
| language: string; |
| code: string; |
| openingIndex: number; |
| } |
|
|
| |
| |
| const FENCE_LINE_REGEX = /^ {0,3}(`{3,})(.*)$/; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function splitGluedClosingCodeFences(markdown: string): string { |
| if (!markdown.includes('```')) return markdown; |
|
|
| const lines = markdown.split(NEWLINE); |
|
|
| let inside = false; |
| let changed = false; |
|
|
| for (let i = 0; i < lines.length; i++) { |
| const match = FENCE_LINE_REGEX.exec(lines[i]); |
|
|
| if (!match) continue; |
|
|
| if (!inside) { |
| inside = true; |
|
|
| continue; |
| } |
|
|
| inside = false; |
|
|
| const trailing = match[2]; |
|
|
| if (trailing.includes('`') || !/\s/.test(trailing)) continue; |
|
|
| lines[i] = lines[i].slice(0, lines[i].length - trailing.length); |
| lines.splice(i + 1, 0, trailing.trim()); |
| i++; |
| changed = true; |
| } |
|
|
| return changed ? lines.join(NEWLINE) : markdown; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function trimCodePadding(code: string): string { |
| return code.replace(TRIM_LEADING_PADDING_REGEX, '').replace(TRIM_TRAILING_PADDING_REGEX, ''); |
| } |
|
|
| function escapeCode(code: string): string { |
| return code.replace(AMPERSAND_REGEX, '&').replace(LT_REGEX, '<').replace(GT_REGEX, '>'); |
| } |
|
|
| |
| const HIGHLIGHT_CACHE_MAX_SIZE = 64; |
| const highlightCache = new Map<string, string>(); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function highlightCode(code: string, language: string, autoDetect = true): string { |
| if (!code) return ''; |
|
|
| |
| |
| |
| const cacheKey = `${language}:${autoDetect}:${code}`; |
| const cached = highlightCache.get(cacheKey); |
|
|
| if (cached) return cached; |
|
|
| const trimmed = trimCodePadding(code); |
|
|
| let result: string; |
|
|
| try { |
| const lang = language.toLowerCase(); |
| const isSupported = hljs.getLanguage(lang); |
|
|
| if (isSupported) { |
| result = hljs.highlight(trimmed, { language: lang }).value; |
| } else if (autoDetect) { |
| result = hljs.highlightAuto(trimmed).value; |
| } else { |
| result = escapeCode(trimmed); |
| } |
| } catch { |
| result = escapeCode(trimmed); |
| } |
|
|
| if (highlightCache.size >= HIGHLIGHT_CACHE_MAX_SIZE) { |
| highlightCache.delete(highlightCache.keys().next().value!); |
| } |
|
|
| highlightCache.set(cacheKey, result); |
|
|
| return result; |
| } |
|
|
| export { trimCodePadding }; |
|
|
| |
| |
| |
| |
| |
| |
| export function detectIncompleteCodeBlock(markdown: string): IncompleteCodeBlock | null { |
| |
| |
| const fencePattern = new RegExp(FENCE_PATTERN.source, FENCE_PATTERN.flags); |
| const fences: number[] = []; |
|
|
| let fenceMatch; |
|
|
| while ((fenceMatch = fencePattern.exec(markdown)) !== null) { |
| |
| const pos = fenceMatch[0].startsWith(NEWLINE) ? fenceMatch.index + 1 : fenceMatch.index; |
|
|
| fences.push(pos); |
| } |
|
|
| |
| if (fences.length % 2 === 0) { |
| return null; |
| } |
|
|
| |
| |
| const openingIndex = fences[fences.length - 1]; |
| const afterOpening = markdown.slice(openingIndex + 3); |
| |
| const langMatch = afterOpening.match(LANG_PATTERN); |
| const language = langMatch?.[1] || DEFAULT_LANGUAGE; |
| const codeStartIndex = openingIndex + 3 + (langMatch?.[0]?.length ?? 0); |
| const code = markdown.slice(codeStartIndex); |
|
|
| return { |
| code, |
| language, |
| openingIndex |
| }; |
| } |
|
|