From 19725ca7cbadb8d186b22aecbac17b01139c2861 Mon Sep 17 00:00:00 2001 From: aineo <124525926+aineo@users.noreply.github.com> Date: Wed, 25 Sep 2024 18:41:35 +0300 Subject: [PATCH] fix: broken links are shown as undefined --- src/lib/markdown.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/lib/markdown.js b/src/lib/markdown.js index 92e2dda2f..1bcaeee3c 100644 --- a/src/lib/markdown.js +++ b/src/lib/markdown.js @@ -10,11 +10,11 @@ marked.setOptions({ const renderer = new marked.Renderer() -renderer.image = function (_href, _title, _text) { +renderer.image = function ({ _href, _title, _text }) { return '' } -renderer.link = function (href, title, text) { +renderer.link = function ({ href, _title, text }) { const linkPattern = /^(eth|bch|bitcoin|https?|s?ftp|magnet|tor|onion|tg):(.*)$/i const emailPattern = /^(mailto):[^@]+@[^@]+\.[^@]+$/i @@ -27,10 +27,12 @@ renderer.link = function (href, title, text) { return text } -renderer.heading = function (text) { +renderer.heading = function ({ text }) { return `

${text}

` } +marked.use({ renderer }) + /** * Sanitizes text to show HTML * @param {string} text text to sanitize @@ -46,7 +48,7 @@ export function sanitizeHTML(text = '') { * @returns {string} resulting sanitized HTML */ export function renderMarkdown(text = '') { - return marked(DOMPurify.sanitize(text), { renderer }) + return marked.parse(sanitizeHTML(text)) } /** @@ -58,7 +60,7 @@ export function renderMarkdown(text = '') { export function removeFormats(text = '') { const node = document.createElement('div') const textWithSymbol = text.replace(/\n/g, '↵ ') - node.innerHTML = marked(DOMPurify.sanitize(textWithSymbol), { renderer }) + node.innerHTML = marked.parse(sanitizeHTML(textWithSymbol)) return node.textContent || node.innerText || '' } @@ -66,7 +68,7 @@ export function removeFormats(text = '') { export function formatMessage(text = '') { const node = document.createElement('div') const textWithSymbol = text.replace(/\n/g, '↵ ') - node.innerHTML = marked(DOMPurify.sanitize(textWithSymbol), { renderer }) + node.innerHTML = marked.parse(sanitizeHTML(textWithSymbol)) const textWithoutHtml = node.textContent || node.innerText || '' const styledText = textWithoutHtml.replace(/↵/g, '')