1
+ import { matcher } from "./matcher" ;
1
2
import { blockElementParserList , inlineElementParserList } from "./parser" ;
2
3
3
- export const marked = ( markdownStr : string , blockParsers = blockElementParserList , inlineParsers = inlineElementParserList ) : string => {
4
+ export const marked = (
5
+ markdownStr : string ,
6
+ blockParsers = blockElementParserList ,
7
+ inlineParsers = inlineElementParserList
8
+ ) : string | JSX . Element => {
4
9
for ( const parser of blockParsers ) {
5
- const matchResult = parser . matcher ( markdownStr ) ;
10
+ const matchResult = matcher ( markdownStr , parser . regexp ) ;
6
11
if ( ! matchResult ) {
7
12
continue ;
8
13
}
9
14
const matchedStr = matchResult [ 0 ] ;
10
15
const retainContent = markdownStr . slice ( matchedStr . length ) ;
11
16
12
17
if ( parser . name === "br" ) {
13
- return parser . renderer ( matchedStr ) + marked ( retainContent , blockParsers , inlineParsers ) ;
18
+ return (
19
+ < >
20
+ { parser . renderer ( matchedStr ) }
21
+ { marked ( retainContent , blockParsers , inlineParsers ) }
22
+ </ >
23
+ ) ;
14
24
} else {
15
25
if ( retainContent === "" ) {
16
26
return parser . renderer ( matchedStr ) ;
17
27
} else if ( retainContent . startsWith ( "\n" ) ) {
18
- return parser . renderer ( matchedStr ) + marked ( retainContent . slice ( 1 ) , blockParsers , inlineParsers ) ;
28
+ return (
29
+ < >
30
+ { parser . renderer ( matchedStr ) }
31
+ { marked ( retainContent . slice ( 1 ) , blockParsers , inlineParsers ) }
32
+ </ >
33
+ ) ;
19
34
}
20
35
}
21
36
}
@@ -24,7 +39,7 @@ export const marked = (markdownStr: string, blockParsers = blockElementParserLis
24
39
let matchedIndex = - 1 ;
25
40
26
41
for ( const parser of inlineParsers ) {
27
- const matchResult = parser . matcher ( markdownStr ) ;
42
+ const matchResult = matcher ( markdownStr , parser . regexp ) ;
28
43
if ( ! matchResult ) {
29
44
continue ;
30
45
}
@@ -41,17 +56,23 @@ export const marked = (markdownStr: string, blockParsers = blockElementParserLis
41
56
}
42
57
43
58
if ( matchedInlineParser ) {
44
- const matchResult = matchedInlineParser . matcher ( markdownStr ) ;
59
+ const matchResult = matcher ( markdownStr , matchedInlineParser . regexp ) ;
45
60
if ( matchResult ) {
46
61
const matchedStr = matchResult [ 0 ] ;
47
62
const matchedLength = matchedStr . length ;
48
63
const prefixStr = markdownStr . slice ( 0 , matchedIndex ) ;
49
64
const suffixStr = markdownStr . slice ( matchedIndex + matchedLength ) ;
50
- return marked ( prefixStr , [ ] , inlineParsers ) + matchedInlineParser . renderer ( matchedStr ) + marked ( suffixStr , [ ] , inlineParsers ) ;
65
+ return (
66
+ < >
67
+ { marked ( prefixStr , [ ] , inlineParsers ) }
68
+ { matchedInlineParser . renderer ( matchedStr ) }
69
+ { marked ( suffixStr , [ ] , inlineParsers ) }
70
+ </ >
71
+ ) ;
51
72
}
52
73
}
53
74
54
- return markdownStr ;
75
+ return < > { markdownStr } </ > ;
55
76
} ;
56
77
57
78
interface MatchedNode {
@@ -64,7 +85,7 @@ export const getMatchedNodes = (markdownStr: string): MatchedNode[] => {
64
85
65
86
const walkthough = ( markdownStr : string , blockParsers = blockElementParserList , inlineParsers = inlineElementParserList ) : string => {
66
87
for ( const parser of blockParsers ) {
67
- const matchResult = parser . matcher ( markdownStr ) ;
88
+ const matchResult = matcher ( markdownStr , parser . regexp ) ;
68
89
if ( ! matchResult ) {
69
90
continue ;
70
91
}
@@ -79,6 +100,7 @@ export const getMatchedNodes = (markdownStr: string): MatchedNode[] => {
79
100
return walkthough ( retainContent , blockParsers , inlineParsers ) ;
80
101
} else {
81
102
if ( retainContent . startsWith ( "\n" ) ) {
103
+ walkthough ( matchedStr , [ ] , inlineParsers ) ;
82
104
return walkthough ( retainContent . slice ( 1 ) , blockParsers , inlineParsers ) ;
83
105
}
84
106
}
@@ -88,7 +110,7 @@ export const getMatchedNodes = (markdownStr: string): MatchedNode[] => {
88
110
let matchedIndex = - 1 ;
89
111
90
112
for ( const parser of inlineParsers ) {
91
- const matchResult = parser . matcher ( markdownStr ) ;
113
+ const matchResult = matcher ( markdownStr , parser . regexp ) ;
92
114
if ( ! matchResult ) {
93
115
continue ;
94
116
}
@@ -105,7 +127,7 @@ export const getMatchedNodes = (markdownStr: string): MatchedNode[] => {
105
127
}
106
128
107
129
if ( matchedInlineParser ) {
108
- const matchResult = matchedInlineParser . matcher ( markdownStr ) ;
130
+ const matchResult = matcher ( markdownStr , matchedInlineParser . regexp ) ;
109
131
if ( matchResult ) {
110
132
const matchedStr = matchResult [ 0 ] ;
111
133
const matchedLength = matchedStr . length ;
0 commit comments