From bdd932bbbbeaf9edb93ed2a0b02eedb2d1f03677 Mon Sep 17 00:00:00 2001 From: Fovway Date: Tue, 4 Nov 2025 12:33:51 +0700 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F=20?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=B4=D0=B5=D1=80=D0=B8=D0=BD=D0=B3=D0=B0=20?= =?UTF-8?q?=D0=B2=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=BD=D1=8B=D1=85=20=D1=82?= =?UTF-8?q?=D0=BE=D0=BA=D0=B5=D0=BD=D0=BE=D0=B2=20=D0=B2=20markdown,=20?= =?UTF-8?q?=D1=83=D0=BB=D1=83=D1=87=D1=88=D0=B5=D0=BD=D0=B0=20=D0=BE=D0=B1?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BA=D0=B0=20=D1=82=D0=B5=D0=BA?= =?UTF-8?q?=D1=81=D1=82=D0=BE=D0=B2=D1=8B=D1=85=20=D1=8D=D0=BB=D0=B5=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D1=82=D0=BE=D0=B2=20=D0=B8=20=D1=81=D1=81=D1=8B?= =?UTF-8?q?=D0=BB=D0=BE=D0=BA=20=D1=81=20=D0=BF=D0=BE=D0=B4=D0=B4=D0=B5?= =?UTF-8?q?=D1=80=D0=B6=D0=BA=D0=BE=D0=B9=20=D0=BA=D0=B0=D1=81=D1=82=D0=BE?= =?UTF-8?q?=D0=BC=D0=BD=D1=8B=D1=85=20=D1=80=D0=B5=D0=BD=D0=B4=D0=B5=D1=80?= =?UTF-8?q?=D0=B5=D1=80=D0=BE=D0=B2.=20=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D1=8B=20=D0=BA=D0=BE=D0=BC=D0=BF=D0=BE=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=D1=82=D1=8B=20=D0=B4=D0=BB=D1=8F=20=D0=BA=D0=BE?= =?UTF-8?q?=D1=80=D1=80=D0=B5=D0=BA=D1=82=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=BE?= =?UTF-8?q?=D1=82=D0=BE=D0=B1=D1=80=D0=B0=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=B2=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=BD=D1=8B=D1=85=20=D1=81?= =?UTF-8?q?=D1=82=D1=80=D1=83=D0=BA=D1=82=D1=83=D1=80=20=D0=B2=20=D1=81?= =?UTF-8?q?=D0=BF=D0=B8=D1=81=D0=BA=D0=B0=D1=85=20=D0=B8=20=D1=81=D1=81?= =?UTF-8?q?=D1=8B=D0=BB=D0=BA=D0=B0=D1=85.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/markdown.ts | 71 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 4 deletions(-) diff --git a/src/utils/markdown.ts b/src/utils/markdown.ts index 83b2075..6e89f04 100644 --- a/src/utils/markdown.ts +++ b/src/utils/markdown.ts @@ -23,12 +23,66 @@ const spoilerExtension = { }, }; +// Функция для рендеринга вложенных токенов +function renderTokens(tokens: any[], renderer: any): string { + return tokens.map((token) => { + // Используем кастомный renderer если он есть + if (renderer[token.type]) { + return renderer[token.type](token); + } + // Fallback для встроенных типов токенов + if (token.type === 'text') { + return token.text || ''; + } + if (token.type === 'strong') { + return `${renderTokens(token.tokens || [], renderer)}`; + } + if (token.type === 'em') { + return `${renderTokens(token.tokens || [], renderer)}`; + } + if (token.type === 'codespan') { + return `${token.text || ''}`; + } + if (token.type === 'del') { + return `${renderTokens(token.tokens || [], renderer)}`; + } + if (token.type === 'link') { + // Для ссылок используем кастомный renderer если доступен + if (renderer.link) { + return renderer.link(token); + } + // Fallback для встроенных ссылок + const href = token.href || ''; + const title = token.title ? ` title="${token.title}"` : ''; + const text = token.tokens && token.tokens.length > 0 + ? renderTokens(token.tokens, renderer) + : (token.text || ''); + return `${text}`; + } + if (token.type === 'spoiler') { + // Для спойлеров используем кастомный renderer если доступен + if (renderer.spoiler) { + return renderer.spoiler(token); + } + return `${token.text || ''}`; + } + return token.text || ''; + }).join(''); +} + // Кастомный renderer для внешних ссылок и чекбоксов const renderer: any = { link(token: any) { const href = token.href; const title = token.title; - const text = token.text; + + // Правильно обрабатываем вложенные токены для форматирования внутри ссылок + let text = ''; + if (token.tokens && token.tokens.length > 0) { + text = renderTokens(token.tokens, this); + } else if (token.text) { + text = token.text; + } try { const url = new URL(href, window.location.href); @@ -45,15 +99,24 @@ const renderer: any = { }, // Кастомный renderer для элементов списка с чекбоксами listitem(token: any) { - const text = token.text; const task = token.task; const checked = token.checked; + + // Правильно обрабатываем вложенные токены для форматирования + let content = ''; + if (token.tokens && token.tokens.length > 0) { + // Рендерим вложенные токены используя наш renderer + content = renderTokens(token.tokens, this); + } else if (token.text) { + // Если токенов нет, используем текст (для обратной совместимости) + content = token.text; + } if (task) { const checkbox = ``; - return `
  • ${checkbox} ${text}
  • \n`; + return `
  • ${checkbox} ${content}
  • \n`; } - return `
  • ${text}
  • \n`; + return `
  • ${content}
  • \n`; }, };