File tree Expand file tree Collapse file tree 1 file changed +20
-1
lines changed
packages/integrations/mdx/src Expand file tree Collapse file tree 1 file changed +20
-1
lines changed Original file line number Diff line number Diff line change 1
1
import type { VFile } from 'vfile' ;
2
2
import { jsToTreeNode } from './utils.js' ;
3
3
4
+ // Escape unsafe characters for safe code injection
5
+ const charMap : Record < string , string > = {
6
+ '<' : '\\u003C' ,
7
+ '>' : '\\u003E' ,
8
+ '/' : '\\u002F' ,
9
+ '\\' : '\\\\' ,
10
+ '\b' : '\\b' ,
11
+ '\f' : '\\f' ,
12
+ '\n' : '\\n' ,
13
+ '\r' : '\\r' ,
14
+ '\t' : '\\t' ,
15
+ '\0' : '\\0' ,
16
+ '\u2028' : '\\u2028' ,
17
+ '\u2029' : '\\u2029'
18
+ } ;
19
+ function escapeUnsafeChars ( str : string ) : string {
20
+ return str . replace ( / [ < > \b \f \n \r \t \0 \u2028 \u2029 / \\ ] / g, x => charMap [ x ] || x ) ;
21
+ }
22
+
4
23
export function rehypeInjectHeadingsExport ( ) {
5
24
return function ( tree : any , file : VFile ) {
6
25
const headings = file . data . astro ?. headings ?? [ ] ;
7
26
tree . children . unshift (
8
- jsToTreeNode ( `export function getHeadings() { return ${ JSON . stringify ( headings ) } }` ) ,
27
+ jsToTreeNode ( `export function getHeadings() { return ${ escapeUnsafeChars ( JSON . stringify ( headings ) ) } }` ) ,
9
28
) ;
10
29
} ;
11
30
}
You can’t perform that action at this time.
0 commit comments