ui: Constants refactor (#26908)
* refactor: Constants * refactor: Constants/Enums cleanup * refactor: Constant objects instead of multiple single value constants * refactor: Cleanup constants
This commit is contained in:
@@ -1,14 +1,4 @@
|
||||
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 { CODE_BLOCK, NEWLINE } from '$lib/constants';
|
||||
import hljs from 'highlight.js';
|
||||
|
||||
export interface IncompleteCodeBlock {
|
||||
@@ -81,11 +71,16 @@ export function splitGluedClosingCodeFences(markdown: string): string {
|
||||
* so internal blank lines are still rendered as such.
|
||||
*/
|
||||
function trimCodePadding(code: string): string {
|
||||
return code.replace(TRIM_LEADING_PADDING_REGEX, '').replace(TRIM_TRAILING_PADDING_REGEX, '');
|
||||
return code
|
||||
.replace(CODE_BLOCK.TRIM_LEADING_PADDING_REGEX, '')
|
||||
.replace(CODE_BLOCK.TRIM_TRAILING_PADDING_REGEX, '');
|
||||
}
|
||||
|
||||
function escapeCode(code: string): string {
|
||||
return code.replace(AMPERSAND_REGEX, '&').replace(LT_REGEX, '<').replace(GT_REGEX, '>');
|
||||
return code
|
||||
.replace(CODE_BLOCK.AMPERSAND_REGEX, '&')
|
||||
.replace(CODE_BLOCK.LT_REGEX, '<')
|
||||
.replace(CODE_BLOCK.GT_REGEX, '>');
|
||||
}
|
||||
|
||||
/** Bounded cache for highlightCode results. */
|
||||
@@ -152,7 +147,7 @@ export { trimCodePadding };
|
||||
export function detectIncompleteCodeBlock(markdown: string): IncompleteCodeBlock | null {
|
||||
// Count all code fences in the markdown
|
||||
// A code block is incomplete if there's an odd number of ``` fences
|
||||
const fencePattern = new RegExp(FENCE_PATTERN.source, FENCE_PATTERN.flags);
|
||||
const fencePattern = new RegExp(CODE_BLOCK.FENCE_PATTERN.source, CODE_BLOCK.FENCE_PATTERN.flags);
|
||||
const fences: number[] = [];
|
||||
|
||||
let fenceMatch;
|
||||
@@ -174,8 +169,8 @@ export function detectIncompleteCodeBlock(markdown: string): IncompleteCodeBlock
|
||||
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;
|
||||
const langMatch = afterOpening.match(CODE_BLOCK.LANG_PATTERN);
|
||||
const language = langMatch?.[1] || CODE_BLOCK.DEFAULT_LANGUAGE;
|
||||
const codeStartIndex = openingIndex + 3 + (langMatch?.[0]?.length ?? 0);
|
||||
const code = markdown.slice(codeStartIndex);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user