From bbe5716e40945512f99fd2c73c661cb495a070d0 Mon Sep 17 00:00:00 2001 From: Pabloader Date: Wed, 25 Mar 2026 09:49:30 +0000 Subject: [PATCH] Highlight headers --- src/common/assets/highlight.module.css | 7 +++++++ src/common/highlight.ts | 21 ++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/common/assets/highlight.module.css b/src/common/assets/highlight.module.css index b64ff73..e892450 100644 --- a/src/common/assets/highlight.module.css +++ b/src/common/assets/highlight.module.css @@ -25,3 +25,10 @@ padding: 0.1em 0.3em; border-radius: 0.2em; } + +.header { + font-size: 1.4em; + font-weight: bold; + color: var(--yellow, #e6db74); + display: block; +} diff --git a/src/common/highlight.ts b/src/common/highlight.ts index 03c0d0e..2ceffa4 100644 --- a/src/common/highlight.ts +++ b/src/common/highlight.ts @@ -2,9 +2,10 @@ import styles from './assets/highlight.module.css'; export const highlight = (message: string, keepMarkup = true): string => { let resultHTML = ''; - const tokenRegex = /(\*\*?|"|```|`)/g; + const tokenRegex = /(\*\*?|"|```|`|(?:^|\n)# |\n)/g; const stack: string[] = []; let inCodeBlock = false; + let inHeader = false; let lastIndex = 0; let match: RegExpExecArray | null; @@ -27,6 +28,23 @@ export const highlight = (message: string, keepMarkup = true): string => { continue; } + if (token.endsWith('# ')) { + if (inHeader) resultHTML += ''; + inHeader = true; + resultHTML += `${token.slice(0, -2)}${keepMarkup ? '# ' : ''}`; + continue; + } + + if (token === '\n') { + if (inHeader) { + resultHTML += `${keepMarkup ? '\n' : ''}`; + inHeader = false; + } else { + resultHTML += '\n'; + } + continue; + } + if (isClose) { stack.pop(); resultHTML += `${keepToken ? token : ''}`; @@ -51,6 +69,7 @@ export const highlight = (message: string, keepMarkup = true): string => { resultHTML += message.slice(lastIndex); + if (inHeader) resultHTML += ''; resultHTML += ''.repeat(stack.length); return resultHTML;