@@ -6,7 +6,6 @@ import autoLinkLiteralMarkdownSyntax from 'micromark-extension-gfm-autolink-lite
6
6
// @ts -ignore
7
7
import gfmStrikethroughFromMarkdownExtension from 'mdast-util-gfm-strikethrough/from-markdown' ;
8
8
import gfmStrikethroughMarkdownSyntax from 'micromark-extension-gfm-strikethrough' ;
9
- import definitions from 'mdast-util-definitions' ;
10
9
import { Descendant } from 'slate' ;
11
10
import { getTextNodeForCurrentlyActiveMarks , addMarkToChildren } from './utils' ;
12
11
@@ -17,22 +16,19 @@ const markdownConfig = {
17
16
18
17
export function deserializeMarkdown ( markdown : string ) {
19
18
const root = mdASTUtilFromMarkdown ( markdown , markdownConfig ) ;
20
- const getDefinition = definitions ( root ) ;
21
19
let nodes = root . children ;
22
20
if ( nodes . length === 1 && nodes [ 0 ] . type === 'paragraph' ) {
23
21
nodes = nodes [ 0 ] . children ;
24
22
}
25
- return deserializeChildren ( nodes , getDefinition ) ;
23
+ return deserializeChildren ( nodes , markdown ) ;
26
24
}
27
25
28
- type GetDefinition = ReturnType < typeof definitions > ;
29
-
30
26
type MDNode = ReturnType < typeof mdASTUtilFromMarkdown > [ 'children' ] [ number ] ;
31
27
32
- function deserializeChildren ( nodes : MDNode [ ] , getDefinition : GetDefinition ) {
28
+ function deserializeChildren ( nodes : MDNode [ ] , input : string ) {
33
29
const outputNodes : Descendant [ ] = [ ] ;
34
30
for ( const node of nodes ) {
35
- const result = deserializeMarkdownNode ( node , getDefinition ) ;
31
+ const result = deserializeMarkdownNode ( node , input ) ;
36
32
if ( result . length ) {
37
33
outputNodes . push ( ...result ) ;
38
34
}
@@ -43,64 +39,45 @@ function deserializeChildren(nodes: MDNode[], getDefinition: GetDefinition) {
43
39
return outputNodes ;
44
40
}
45
41
46
- function deserializeMarkdownNode ( node : MDNode , getDefinition : GetDefinition ) : Descendant [ ] {
42
+ function deserializeMarkdownNode ( node : MDNode , input : string ) : Descendant [ ] {
47
43
switch ( node . type ) {
48
44
case 'blockquote' : {
49
- return [ { type : 'blockquote' , children : deserializeChildren ( node . children , getDefinition ) } ] ;
50
- }
51
- case 'linkReference' : {
52
- return [
53
- {
54
- type : 'link' ,
55
- href : getDefinition ( node . identifier ) ?. url || '' ,
56
- children : deserializeChildren ( node . children , getDefinition ) ,
57
- } ,
58
- ] ;
45
+ return [ { type : 'blockquote' , children : deserializeChildren ( node . children , input ) } ] ;
59
46
}
60
47
case 'link' : {
61
48
return [
62
49
{
63
50
type : 'link' ,
64
51
href : node . url ,
65
- children : deserializeChildren ( node . children , getDefinition ) ,
52
+ children : deserializeChildren ( node . children , input ) ,
66
53
} ,
67
54
] ;
68
55
}
69
56
case 'code' : {
70
57
return [ { type : 'code' , children : [ { text : node . value } ] } ] ;
71
58
}
72
59
case 'paragraph' : {
73
- return [ { type : 'paragraph' , children : deserializeChildren ( node . children , getDefinition ) } ] ;
60
+ return [ { type : 'paragraph' , children : deserializeChildren ( node . children , input ) } ] ;
74
61
}
75
62
case 'heading' : {
76
63
return [
77
64
{
78
65
type : 'heading' ,
79
66
level : node . depth ,
80
- children : deserializeChildren ( node . children , getDefinition ) ,
67
+ children : deserializeChildren ( node . children , input ) ,
81
68
} ,
82
69
] ;
83
70
}
84
71
case 'list' : {
85
72
return [
86
73
{
87
74
type : node . ordered ? 'ordered-list' : 'unordered-list' ,
88
- children : deserializeChildren ( node . children , getDefinition ) ,
75
+ children : deserializeChildren ( node . children , input ) ,
89
76
} ,
90
77
] ;
91
78
}
92
- case 'imageReference' : {
93
- return [
94
- getTextNodeForCurrentlyActiveMarks (
95
- ` ?. url || '' } )`
96
- ) ,
97
- ] ;
98
- }
99
- case 'image' : {
100
- return [ getTextNodeForCurrentlyActiveMarks ( `` ) ] ;
101
- }
102
79
case 'listItem' : {
103
- return [ { type : 'list-item' , children : deserializeChildren ( node . children , getDefinition ) } ] ;
80
+ return [ { type : 'list-item' , children : deserializeChildren ( node . children , input ) } ] ;
104
81
}
105
82
case 'thematicBreak' : {
106
83
return [ { type : 'divider' , children : [ { text : '' } ] } ] ;
@@ -109,28 +86,29 @@ function deserializeMarkdownNode(node: MDNode, getDefinition: GetDefinition): De
109
86
return [ getTextNodeForCurrentlyActiveMarks ( '\n' ) ] ;
110
87
}
111
88
case 'delete' : {
112
- return addMarkToChildren ( 'strikethrough' , ( ) =>
113
- deserializeChildren ( node . children , getDefinition )
114
- ) ;
89
+ return addMarkToChildren ( 'strikethrough' , ( ) => deserializeChildren ( node . children , input ) ) ;
115
90
}
116
91
case 'strong' : {
117
- return addMarkToChildren ( 'bold' , ( ) => deserializeChildren ( node . children , getDefinition ) ) ;
92
+ return addMarkToChildren ( 'bold' , ( ) => deserializeChildren ( node . children , input ) ) ;
118
93
}
119
94
case 'emphasis' : {
120
- return addMarkToChildren ( 'italic' , ( ) => deserializeChildren ( node . children , getDefinition ) ) ;
95
+ return addMarkToChildren ( 'italic' , ( ) => deserializeChildren ( node . children , input ) ) ;
121
96
}
122
97
case 'inlineCode' : {
123
98
return addMarkToChildren ( 'code' , ( ) => [ getTextNodeForCurrentlyActiveMarks ( node . value ) ] ) ;
124
99
}
125
- // while it would be nice if we parsed the html here
100
+ // while it might be nice if we parsed the html here
126
101
// it's a bit more complicated than just parsing the html
127
102
// because an html node might just be an opening/closing node
128
103
// but we just have an opening/closing node here
129
104
// not the opening and closing and children
130
- case 'html' :
131
105
case 'text' : {
132
106
return [ getTextNodeForCurrentlyActiveMarks ( node . value ) ] ;
133
107
}
134
108
}
135
- return [ ] ;
109
+ return [
110
+ getTextNodeForCurrentlyActiveMarks (
111
+ input . slice ( node . position ! . start . offset , node . position ! . end . offset )
112
+ ) ,
113
+ ] ;
136
114
}
0 commit comments