@@ -3,23 +3,40 @@ import { sanitize_for_svelte } from './transformer';
3
3
import { escape } from 'html-escaper' ;
4
4
import { IMAGE_PREFIX , IMPORT_PREFIX , NODES_IMPORT } from './constants' ;
5
5
import { is_relative_path } from './utils' ;
6
+ import { Config } from './config' ;
6
7
7
- export function render_html (
8
+ export async function render_html (
8
9
node : RenderableTreeNodes ,
9
10
dependencies : Map < string , string > ,
10
- ) : string {
11
+ highlighter : Config [ 'highlighter' ] ,
12
+ escape_html = true ,
13
+ ) : Promise < string > {
11
14
/**
12
15
* if the node is a string or number, it's a text node.
13
16
*/
14
17
if ( typeof node === 'string' || typeof node === 'number' ) {
15
- return sanitize_for_svelte ( escape ( String ( node ) ) ) ;
18
+ if ( escape_html ) {
19
+ return sanitize_for_svelte ( escape ( String ( node ) ) ) ;
20
+ } else {
21
+ return sanitize_for_svelte ( String ( node ) ) ;
22
+ }
16
23
}
17
24
18
25
/**
19
26
* if the node is an array, render its items.
20
27
*/
21
28
if ( Array . isArray ( node ) ) {
22
- return node . map ( ( item ) => render_html ( item , dependencies ) ) . join ( '' ) ;
29
+ return Promise . all (
30
+ node . map (
31
+ async ( item ) =>
32
+ await render_html (
33
+ item ,
34
+ dependencies ,
35
+ highlighter ,
36
+ escape_html ,
37
+ ) ,
38
+ ) ,
39
+ ) . then ( ( items ) => items . join ( '' ) ) ;
23
40
}
24
41
25
42
/**
@@ -29,10 +46,10 @@ export function render_html(
29
46
return '' ;
30
47
}
31
48
32
- const { name, attributes, children = [ ] } = node ;
49
+ let { name, attributes, children = [ ] } = node ;
33
50
34
51
if ( ! name ) {
35
- return render_html ( children , dependencies ) ;
52
+ return await render_html ( children , dependencies , highlighter ) ;
36
53
}
37
54
38
55
const is_svelte = is_svelte_component ( node ) ;
@@ -41,9 +58,10 @@ export function render_html(
41
58
* add attributes to the tag.
42
59
*/
43
60
let output = `<${ name } ` ;
44
- for ( const [ key , value ] of Object . entries ( attributes ?? { } ) ) {
61
+ for ( let [ key , value ] of Object . entries ( attributes ?? { } ) ) {
45
62
const is_src_key = key === 'src' ;
46
63
const is_imported_image = is_src_key && is_relative_path ( value ) ;
64
+
47
65
if ( is_svelte ) {
48
66
switch ( name . toLowerCase ( ) ) {
49
67
case `${ NODES_IMPORT } .image` . toLowerCase ( ) :
@@ -97,11 +115,39 @@ export function render_html(
97
115
return output ;
98
116
}
99
117
118
+ let escape_next = true ;
119
+
120
+ if ( highlighter ) {
121
+ const run_highlighter =
122
+ name . toLowerCase ( ) === `${ NODES_IMPORT } .fence` . toLowerCase ( ) ||
123
+ name . toLowerCase ( ) === 'pre' . toLowerCase ( ) ;
124
+ if ( run_highlighter ) {
125
+ escape_next = false ;
126
+ children = await Promise . all (
127
+ children . map ( async ( child ) =>
128
+ typeof child === 'string'
129
+ ? await highlighter (
130
+ child ,
131
+ ( is_svelte
132
+ ? attributes ?. language
133
+ : attributes [ 'data-language' ] ) ?? '' ,
134
+ )
135
+ : child ,
136
+ ) ,
137
+ ) ;
138
+ }
139
+ }
140
+
100
141
/**
101
142
* render the children if present.
102
143
*/
103
144
if ( children . length ) {
104
- output += render_html ( children , dependencies ) ;
145
+ output += await render_html (
146
+ children ,
147
+ dependencies ,
148
+ highlighter ,
149
+ escape_next ,
150
+ ) ;
105
151
}
106
152
107
153
/**
0 commit comments