Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Broken links — are shown as undefined #668

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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