1
- import { marked } from 'marked'
1
+ import { Marked } from 'marked'
2
2
import DOMPurify from 'dompurify'
3
3
4
4
// The U+2028 character (LINE SEPARATOR) is sometimes used as a line break, but it is treated as a space in some web environments,
@@ -9,6 +9,8 @@ const LINE_SEPARATOR = /\u2028/g
9
9
// It replaces line breaks with a visual symbol (↵) to indicate where new lines exist in the original text.
10
10
const LINE_BREAK_VISUAL = '↵ '
11
11
12
+ const marked = new Marked ( )
13
+
12
14
marked . setOptions ( {
13
15
// marked sanitize is deprecated, using DOMPurify
14
16
// sanitize: true,
@@ -18,11 +20,11 @@ marked.setOptions({
18
20
19
21
const renderer = new marked . Renderer ( )
20
22
21
- renderer . image = function ( { _href , _title , _text } ) {
23
+ renderer . image = function ( ) {
22
24
return ''
23
25
}
24
26
25
- renderer . link = function ( { href, _title , text } ) {
27
+ renderer . link = function ( { href, text } ) {
26
28
const linkPattern = / ^ ( e t h | b c h | b i t c o i n | h t t p s ? | s ? f t p | m a g n e t | t o r | o n i o n | t g ) : ( .* ) $ / i
27
29
const emailPattern = / ^ ( m a i l t o ) : [ ^ @ ] + @ [ ^ @ ] + \. [ ^ @ ] + $ / i
28
30
@@ -56,7 +58,7 @@ export function sanitizeHTML(text = '') {
56
58
* @returns {string } resulting sanitized HTML
57
59
*/
58
60
export function renderMarkdown ( text = '' ) {
59
- return marked . parse ( sanitizeHTML ( text . replace ( LINE_SEPARATOR , '\n' ) ) )
61
+ return marked . parse ( sanitizeHTML ( text . replace ( LINE_SEPARATOR , '\n' ) ) , { async : false } )
60
62
}
61
63
62
64
/**
@@ -68,7 +70,7 @@ export function renderMarkdown(text = '') {
68
70
export function removeFormats ( text = '' ) {
69
71
const node = document . createElement ( 'div' )
70
72
const textWithSymbol = text . replace ( / \n / g, '↵ ' )
71
- node . innerHTML = marked . parse ( sanitizeHTML ( textWithSymbol ) )
73
+ node . innerHTML = marked . parse ( sanitizeHTML ( textWithSymbol ) , { async : false } )
72
74
73
75
return node . textContent || node . innerText || ''
74
76
}
@@ -77,7 +79,7 @@ export function formatMessage(text = '') {
77
79
const node = document . createElement ( 'div' )
78
80
79
81
const textWithSymbol = text . replace ( / \n / g, LINE_BREAK_VISUAL )
80
- node . innerHTML = marked . parse ( sanitizeHTML ( textWithSymbol ) )
82
+ node . innerHTML = marked . parse ( sanitizeHTML ( textWithSymbol ) , { async : false } )
81
83
82
84
const textWithoutHtml = node . textContent || node . innerText || ''
83
85
0 commit comments