Skip to content

Commit

Permalink
Merge pull request #668 from Adamant-im/fix/broken-links-shown-undefined
Browse files Browse the repository at this point in the history
Fix: Broken links — are shown as undefined
  • Loading branch information
bludnic authored Oct 28, 2024
2 parents 7ea4e1d + 19725ca commit f986b0d
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/lib/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -27,10 +27,12 @@ renderer.link = function (href, title, text) {
return text
}

renderer.heading = function (text) {
renderer.heading = function ({ text }) {
return `<p>${text}</p>`
}

marked.use({ renderer })

/**
* Sanitizes text to show HTML
* @param {string} text text to sanitize
Expand All @@ -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))
}

/**
Expand All @@ -58,15 +60,15 @@ 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 || ''
}

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, '<span class="arrow-return">↵</span>')
Expand Down

0 comments on commit f986b0d

Please sign in to comment.