ui: Linting & Formatting scripts (#26819)
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
import hljs from 'highlight.js';
|
||||
import {
|
||||
NEWLINE,
|
||||
DEFAULT_LANGUAGE,
|
||||
LANG_PATTERN,
|
||||
AMPERSAND_REGEX,
|
||||
LT_REGEX,
|
||||
GT_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;
|
||||
@@ -41,21 +41,25 @@ 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);
|
||||
@@ -106,9 +110,11 @@ export function highlightCode(code: string, language: string, autoDetect = true)
|
||||
// (e.g., when text after a code block changes but the code itself doesn't).
|
||||
const cacheKey = `${language}:${autoDetect}:${code}`;
|
||||
const cached = highlightCache.get(cacheKey);
|
||||
|
||||
if (cached) return cached;
|
||||
|
||||
const trimmed = trimCodePadding(code);
|
||||
|
||||
let result: string;
|
||||
|
||||
try {
|
||||
@@ -129,6 +135,7 @@ export function highlightCode(code: string, language: string, autoDetect = true)
|
||||
if (highlightCache.size >= HIGHLIGHT_CACHE_MAX_SIZE) {
|
||||
highlightCache.delete(highlightCache.keys().next().value!);
|
||||
}
|
||||
|
||||
highlightCache.set(cacheKey, result);
|
||||
|
||||
return result;
|
||||
@@ -147,11 +154,13 @@ export function detectIncompleteCodeBlock(markdown: string): IncompleteCodeBlock
|
||||
// A code block is incomplete if there's an odd number of ``` fences
|
||||
const fencePattern = new RegExp(FENCE_PATTERN.source, FENCE_PATTERN.flags);
|
||||
const fences: number[] = [];
|
||||
|
||||
let fenceMatch;
|
||||
|
||||
while ((fenceMatch = fencePattern.exec(markdown)) !== null) {
|
||||
// Store the position after the ```
|
||||
const pos = fenceMatch[0].startsWith(NEWLINE) ? fenceMatch.index + 1 : fenceMatch.index;
|
||||
|
||||
fences.push(pos);
|
||||
}
|
||||
|
||||
@@ -164,7 +173,6 @@ export function detectIncompleteCodeBlock(markdown: string): IncompleteCodeBlock
|
||||
// The last fence is the opening of the incomplete block
|
||||
const openingIndex = fences[fences.length - 1];
|
||||
const afterOpening = markdown.slice(openingIndex + 3);
|
||||
|
||||
// Extract language and code content
|
||||
const langMatch = afterOpening.match(LANG_PATTERN);
|
||||
const language = langMatch?.[1] || DEFAULT_LANGUAGE;
|
||||
@@ -172,8 +180,8 @@ export function detectIncompleteCodeBlock(markdown: string): IncompleteCodeBlock
|
||||
const code = markdown.slice(codeStartIndex);
|
||||
|
||||
return {
|
||||
language,
|
||||
code,
|
||||
language,
|
||||
openingIndex
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user