Skip to content

Commit 05c792b

Browse files
committed
fix: add LINE_SEPARATOR const
1 parent cfb52ca commit 05c792b

File tree

1 file changed

+9
-20
lines changed

1 file changed

+9
-20
lines changed

src/lib/markdown.js

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { marked } from 'marked'
22
import DOMPurify from 'dompurify'
33

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+
49
marked.setOptions({
510
// marked sanitize is deprecated, using DOMPurify
611
// sanitize: true,
@@ -33,22 +38,6 @@ renderer.heading = function ({ text }) {
3338

3439
marked.use({ renderer })
3540

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-
}
5241
/**
5342
* Sanitizes text to show HTML
5443
* @param {string} text text to sanitize
@@ -64,7 +53,7 @@ export function sanitizeHTML(text = '') {
6453
* @returns {string} resulting sanitized HTML
6554
*/
6655
export function renderMarkdown(text = '') {
67-
return marked.parse(sanitizeHTML(normalizeLineSeparator(text)))
56+
return marked.parse(sanitizeHTML(text.replace(LINE_SEPARATOR, "\n")))
6857
}
6958

7059
/**
@@ -88,7 +77,7 @@ export function formatMessage(text = '') {
8877

8978
const textWithoutHtml = node.textContent || node.innerText || ''
9079

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>')
9483
}

0 commit comments

Comments
 (0)