1
0
Fork 0

Highlight headers

This commit is contained in:
Pabloader 2026-03-25 09:49:30 +00:00
parent 4b24ce85e0
commit bbe5716e40
2 changed files with 27 additions and 1 deletions

View File

@ -25,3 +25,10 @@
padding: 0.1em 0.3em; padding: 0.1em 0.3em;
border-radius: 0.2em; border-radius: 0.2em;
} }
.header {
font-size: 1.4em;
font-weight: bold;
color: var(--yellow, #e6db74);
display: block;
}

View File

@ -2,9 +2,10 @@ import styles from './assets/highlight.module.css';
export const highlight = (message: string, keepMarkup = true): string => { export const highlight = (message: string, keepMarkup = true): string => {
let resultHTML = ''; let resultHTML = '';
const tokenRegex = /(\*\*?|"|```|`)/g; const tokenRegex = /(\*\*?|"|```|`|(?:^|\n)# |\n)/g;
const stack: string[] = []; const stack: string[] = [];
let inCodeBlock = false; let inCodeBlock = false;
let inHeader = false;
let lastIndex = 0; let lastIndex = 0;
let match: RegExpExecArray | null; let match: RegExpExecArray | null;
@ -27,6 +28,23 @@ export const highlight = (message: string, keepMarkup = true): string => {
continue; continue;
} }
if (token.endsWith('# ')) {
if (inHeader) resultHTML += '</span>';
inHeader = true;
resultHTML += `${token.slice(0, -2)}<span class="${styles.header}">${keepMarkup ? '# ' : ''}`;
continue;
}
if (token === '\n') {
if (inHeader) {
resultHTML += `${keepMarkup ? '\n' : ''}</span>`;
inHeader = false;
} else {
resultHTML += '\n';
}
continue;
}
if (isClose) { if (isClose) {
stack.pop(); stack.pop();
resultHTML += `${keepToken ? token : ''}</span>`; resultHTML += `${keepToken ? token : ''}</span>`;
@ -51,6 +69,7 @@ export const highlight = (message: string, keepMarkup = true): string => {
resultHTML += message.slice(lastIndex); resultHTML += message.slice(lastIndex);
if (inHeader) resultHTML += '</span>';
resultHTML += '</span>'.repeat(stack.length); resultHTML += '</span>'.repeat(stack.length);
return resultHTML; return resultHTML;