File tree Expand file tree Collapse file tree 1 file changed +20
-4
lines changed Expand file tree Collapse file tree 1 file changed +20
-4
lines changed Original file line number Diff line number Diff line change @@ -95,12 +95,28 @@ const TEXT_REPLACEMENTS = {
95
95
* @param {string } text - The text to escape.
96
96
* @returns {string } - The escaped text.
97
97
*/
98
+ const escInsideCode = ( s ) =>
99
+ s
100
+ . replaceAll ( / \\ / g, '\\\\' )
101
+ . replaceAll ( / ` / g, '\\`' )
102
+ . replaceAll ( / \$ \{ / g, '\\${' )
103
+ . replaceAll ( / [ { } ] / g, ( m ) => '\\' + m )
104
+
98
105
const escapeMarkdown = ( text ) => {
99
106
if ( typeof text !== 'string' ) return text
100
- return text
101
- . replaceAll ( / ( < ) / g, String . raw `\$1` )
102
- . replaceAll ( '\n' , '<br/>' )
103
- . replaceAll ( / ` ( [ ^ ` ] + ) ` / g, '<code>{`$1`}</code>' )
107
+
108
+ let out = text
109
+
110
+ // 1) przypadek template literal: `... `...``
111
+ out = out . replace ( / ` ( [ \s \S ] * ?) ` ` / g, ( _ , raw ) => {
112
+ const safe = escInsideCode ( raw )
113
+ return `<code>{\`${ safe } \`}</code>`
114
+ } )
115
+
116
+ // 2) przypadek listowych wartości: `...` przed przecinkiem albo końcem
117
+ out = out . replace ( / ` ( [ ^ ` ] + ?) ` (? = \s * , | \s * $ ) / g, '<code>{`$1`}</code>' )
118
+
119
+ return out
104
120
}
105
121
106
122
/**
You can’t perform that action at this time.
0 commit comments