11import React from 'react' ;
22import { View } from 'react-native' ;
33import MarkdownIt from 'markdown-it' ;
4+ import tokensToAST from './util/tokensToAST' ;
5+ import removeInlineTokens from "./util/removeInlineTokens" ;
6+ import groupTextTokens from "./util/groupTextTokens" ;
47
58let md = MarkdownIt ( ) ;
69
7- /**
8- *
9- * @param {{type: string, tag:string, content: string, children: *, attrs: Array} } token
10- * @param {number } tokenIndex
11- * @return {{type: string, content, tokenIndex: *, index: number, attributes: {}, children: *} }
12- */
13- function createNode ( token , tokenIndex ) {
14- let type = 'root' ;
15-
16- if ( token ) {
17- if ( ! token . tag ) {
18- type = token . type ;
19- } else {
20- type = token . tag ;
21- }
22- }
23-
24- const content = token . content ;
25- let attributes = { } ;
26-
27- if ( token . attrs ) {
28- attributes = token . attrs . reduce ( ( prev , curr ) => {
29- const [ name , value ] = curr ;
30- return { ...prev , [ name ] : value } ;
31- } , { } ) ;
32- }
33-
34- return {
35- type,
36- content,
37- tokenIndex,
38- index : 0 ,
39- attributes,
40- children : tokensToAST ( token . children ) ,
41- } ;
42- }
43-
44- /**
45- *
46- * @param {Array<{type: string, tag:string, content: string, children: *, attrs: Array}> }tokens
47- * @return {Array }
48- */
49- function tokensToAST ( tokens ) {
50- const stack = [ ] ;
51- const stackText = [ ] ;
52- let children = [ ] ;
53-
54- if ( ! tokens || tokens . length === 0 ) {
55- return [ ] ;
56- }
57-
58- for ( let i = 0 ; i < tokens . length ; i ++ ) {
59- const token = tokens [ i ] ;
60- const astNode = createNode ( token , i ) ;
61-
62- if ( ! ( astNode . type === 'text' && astNode . children . length === 0 && astNode . content === '' ) ) {
63- astNode . index = children . length ;
64-
65- if ( token . nesting === 1 ) {
66- children . push ( astNode ) ;
67- stack . push ( children ) ;
68- children = astNode . children ;
69- } else if ( token . nesting === - 1 ) {
70- children = stack . pop ( ) ;
71- } else if ( token . nesting === 0 ) {
72- children . push ( astNode ) ;
73- }
74- }
75- }
76-
77- return children ;
78- }
79-
80- function stringToTokens ( source , markdownIt = md ) {
10+ export function stringToTokens ( source , markdownIt = md ) {
8111 let result = [ ] ;
8212 try {
8313 result = md . parse ( source , { } ) ;
@@ -92,11 +22,9 @@ function stringToTokens(source, markdownIt = md) {
9222 * @param {AstRenderer } [markdownIt]
9323 * @return {View }
9424 */
95- function parser ( source , renderer , markdownIt = md ) {
96- const tokens = stringToTokens ( source , markdownIt ) ;
25+ export function parser ( source , renderer , markdownIt = md ) {
26+ const tokens = groupTextTokens ( removeInlineTokens ( stringToTokens ( source , markdownIt ) ) ) ;
9727 const asttree = tokensToAST ( tokens ) ;
9828
9929 return < View > { renderer . render ( asttree ) } </ View > ;
10030}
101-
102- export { tokensToAST , stringToTokens , parser } ;
0 commit comments