1
1
import type { HmmBlock , HmmOptions } from "./types.ts" ;
2
+ import { TextLineStream } from "@std/streams/text-line-stream" ;
3
+ import { escape , unescape } from "@dbushell/hyperless" ;
2
4
import { blockParser } from "./block-parser.ts" ;
3
5
import { BlockStream } from "./block-stream.ts" ;
4
- import { TextLineStream } from "@std/streams/text-line-stream" ;
5
6
6
7
// Block Plugins
7
8
import blockBlockquote from "./block-plugins/blockquote.ts" ;
@@ -25,27 +26,27 @@ import inlineTypography from "./inline-plugins/typography.ts";
25
26
26
27
// Default options
27
28
export const defaultOptions : HmmOptions = {
28
- blockPlugins : [
29
- blockBlockquote ,
30
- blockPreformatted ,
31
- blockHeading ,
32
- blockHorizontalRule ,
33
- blockImage ,
34
- blockOrderedList ,
35
- blockUnorderedList ,
36
- blockParagraph ,
37
- ] ,
29
+ blockPlugins : new Map ( [
30
+ [ blockBlockquote . type , blockBlockquote ] ,
31
+ [ blockPreformatted . type , blockPreformatted ] ,
32
+ [ blockHeading . type , blockHeading ] ,
33
+ [ blockHorizontalRule . type , blockHorizontalRule ] ,
34
+ [ blockImage . type , blockImage ] ,
35
+ [ blockOrderedList . type , blockOrderedList ] ,
36
+ [ blockUnorderedList . type , blockUnorderedList ] ,
37
+ [ blockParagraph . type , blockParagraph ] ,
38
+ ] ) ,
38
39
// Order is significant
39
- inlinePlugins : [
40
- inlineTypography ,
41
- inlineEscape ,
42
- inlineImage ,
43
- inlineAnchor ,
44
- inlineCode ,
45
- inlineStrong ,
46
- inlineEmphasis ,
47
- inlineDeleted ,
48
- ] ,
40
+ inlinePlugins : new Map ( [
41
+ [ inlineTypography . type , inlineTypography ] ,
42
+ [ inlineEscape . type , inlineEscape ] ,
43
+ [ inlineImage . type , inlineImage ] ,
44
+ [ inlineAnchor . type , inlineAnchor ] ,
45
+ [ inlineCode . type , inlineCode ] ,
46
+ [ inlineStrong . type , inlineStrong ] ,
47
+ [ inlineEmphasis . type , inlineEmphasis ] ,
48
+ [ inlineDeleted . type , inlineDeleted ] ,
49
+ ] ) ,
49
50
} ;
50
51
51
52
export const parse = async (
@@ -66,26 +67,23 @@ export const parse = async (
66
67
. pipeThrough ( new BlockStream ( options ) ) ;
67
68
}
68
69
69
- // Get pre-render plugins
70
- const prerender = options . inlinePlugins . filter ( ( plugin ) =>
71
- Object . hasOwn ( plugin , "prerender" )
72
- ) ;
73
-
74
70
// Final block renders
75
71
const blocks : Array < Promise < string > > = [ ] ;
76
72
77
73
for await ( const block of stream ) {
78
- // Apply pre-render plugins to all lines
79
74
if ( block . type !== "preformatted" ) {
75
+ // Inline code is escaped early to avoid `<div>` being parsed as HTML node
80
76
for ( let i = 0 ; i < block . lines . length ; i ++ ) {
81
- prerender . forEach ( ( plugin ) => {
82
- block . lines [ i ] = plugin . prerender ! ( block . lines [ i ] , options ) ;
83
- } ) ;
77
+ if ( block . lines [ i ] . includes ( "`" ) === false ) continue ;
78
+ block . lines [ i ] = block . lines [ i ] . replace (
79
+ / ` ( [ ^ ` ] + ) ` / g,
80
+ ( ...match ) => escape ( unescape ( match [ 0 ] ) ) ,
81
+ ) ;
84
82
}
85
83
}
86
84
// Start block render
87
- const plugin = options . blockPlugins . find ( ( p ) => p . type === block . type ) ! ;
88
- blocks . push ( Promise . resolve ( plugin . render ( block , options ) ) ) ;
85
+ const plugin = options . blockPlugins . get ( block . type ) ! ;
86
+ blocks . push ( plugin . render ( block , options ) ) ;
89
87
}
90
88
return blocks ;
91
89
} ;
0 commit comments