1
1
import { marked } from 'marked'
2
2
import DOMPurify from 'dompurify'
3
3
4
+
5
+ // The U+2028 character (LINE SEPARATOR) is sometimes used as a line break, but it is treated as a space in some web environments,
6
+ // causing unexpected rendering issues. To avoid this, it's recommended to replace it with a standard line break character such as `\n`.
7
+ const LINE_SEPARATOR = / \u2028 / g;
8
+
4
9
marked . setOptions ( {
5
10
// marked sanitize is deprecated, using DOMPurify
6
11
// sanitize: true,
@@ -33,22 +38,6 @@ renderer.heading = function ({ text }) {
33
38
34
39
marked . use ( { renderer } )
35
40
36
-
37
- /**
38
- * Normalizes line separator characters in the given text by replacing U+2028 (LINE SEPARATOR) characters with the specified line separator character.
39
- *
40
- * The U+2028 character (LINE SEPARATOR) is sometimes used as a line break, but it is treated as a space in some web environments,
41
- * causing unexpected rendering issues. To avoid this, it's recommended to replace it with a standard line break character such as `\n`.
42
- *
43
- * @param {string } text - The text in which line separator characters need to be replaced.
44
- * @param {string } [replaceValue="\n"] - The value that will replace the U+2028 character. Defaults to the newline character (`\n`).
45
- *
46
- * @returns {string } - The text with U+2028 characters replaced by `replaceValue`.
47
- *
48
- */
49
- const normalizeLineSeparator = ( text , replaceValue = "\n" ) => {
50
- return text . replace ( / \u2028 / g, replaceValue ) ;
51
- }
52
41
/**
53
42
* Sanitizes text to show HTML
54
43
* @param {string } text text to sanitize
@@ -64,7 +53,7 @@ export function sanitizeHTML(text = '') {
64
53
* @returns {string } resulting sanitized HTML
65
54
*/
66
55
export function renderMarkdown ( text = '' ) {
67
- return marked . parse ( sanitizeHTML ( normalizeLineSeparator ( text ) ) )
56
+ return marked . parse ( sanitizeHTML ( text . replace ( LINE_SEPARATOR , "\n" ) ) )
68
57
}
69
58
70
59
/**
@@ -88,7 +77,7 @@ export function formatMessage(text = '') {
88
77
89
78
const textWithoutHtml = node . textContent || node . innerText || ''
90
79
91
- const replaceValue = '<span class="arrow- return">↵</span>' ;
92
-
93
- return normalizeLineSeparator ( textWithoutHtml . replace ( / ↵ / g, replaceValue ) , replaceValue )
80
+ return textWithoutHtml
81
+ . replace ( LINE_SEPARATOR , '↵' )
82
+ . replace ( / ↵ / g, '<span class="arrow-return">↵</span>' )
94
83
}
0 commit comments