@@ -3,23 +3,40 @@ import { sanitize_for_svelte } from './transformer';
33import { escape } from 'html-escaper' ;
44import { IMAGE_PREFIX , IMPORT_PREFIX , NODES_IMPORT } from './constants' ;
55import { is_relative_path } from './utils' ;
6+ import { Config } from './config' ;
67
7- export function render_html (
8+ export async function render_html (
89 node : RenderableTreeNodes ,
910 dependencies : Map < string , string > ,
10- ) : string {
11+ highlighter : Config [ 'highlighter' ] ,
12+ escape_html = true ,
13+ ) : Promise < string > {
1114 /**
1215 * if the node is a string or number, it's a text node.
1316 */
1417 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+ }
1623 }
1724
1825 /**
1926 * if the node is an array, render its items.
2027 */
2128 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 ( '' ) ) ;
2340 }
2441
2542 /**
@@ -29,10 +46,10 @@ export function render_html(
2946 return '' ;
3047 }
3148
32- const { name, attributes, children = [ ] } = node ;
49+ let { name, attributes, children = [ ] } = node ;
3350
3451 if ( ! name ) {
35- return render_html ( children , dependencies ) ;
52+ return await render_html ( children , dependencies , highlighter ) ;
3653 }
3754
3855 const is_svelte = is_svelte_component ( node ) ;
@@ -41,9 +58,10 @@ export function render_html(
4158 * add attributes to the tag.
4259 */
4360 let output = `<${ name } ` ;
44- for ( const [ key , value ] of Object . entries ( attributes ?? { } ) ) {
61+ for ( let [ key , value ] of Object . entries ( attributes ?? { } ) ) {
4562 const is_src_key = key === 'src' ;
4663 const is_imported_image = is_src_key && is_relative_path ( value ) ;
64+
4765 if ( is_svelte ) {
4866 switch ( name . toLowerCase ( ) ) {
4967 case `${ NODES_IMPORT } .image` . toLowerCase ( ) :
@@ -97,11 +115,39 @@ export function render_html(
97115 return output ;
98116 }
99117
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+
100141 /**
101142 * render the children if present.
102143 */
103144 if ( children . length ) {
104- output += render_html ( children , dependencies ) ;
145+ output += await render_html (
146+ children ,
147+ dependencies ,
148+ highlighter ,
149+ escape_next ,
150+ ) ;
105151 }
106152
107153 /**
0 commit comments