|
| 1 | +import{create}from'md-mdast'; |
| 2 | +import{mdastToFlat}from'../mdastToFlat'; |
| 3 | +import{replace}from'../replace'; |
| 4 | + |
| 5 | +describe('structure',()=>{ |
| 6 | +it('exists',()=>{ |
| 7 | +expect(typeofreplace).toBe('function'); |
| 8 | +}); |
| 9 | + |
| 10 | +it('simple example',()=>{ |
| 11 | +constparser=create(); |
| 12 | +constmdast1=parser.tokenizeBlock('1\n'+'\n'+'replace me\n'); |
| 13 | +constmdast2=parser.tokenizeBlock('2\n'); |
| 14 | +constflat1=mdastToFlat(mdast1!); |
| 15 | +constflat2=mdastToFlat(mdast2!); |
| 16 | +constmerged=replace(flat1,3,flat2); |
| 17 | + |
| 18 | +expect(merged).toMatchObject({ |
| 19 | +nodes:[ |
| 20 | +{type:'root',children:[1,3],idx:0}, |
| 21 | +{type:'paragraph',children:[2],idx:1}, |
| 22 | +{type:'text',value:'1',idx:2}, |
| 23 | +{type:'portal',idx:3,original:{type:'paragraph',children:[4],idx:3},children:[5]}, |
| 24 | +{type:'text',value:'replace me',idx:4}, |
| 25 | +{type:'root',children:[6],idx:5}, |
| 26 | +{type:'paragraph',children:[7],idx:6}, |
| 27 | +{type:'text',value:'2',idx:7}, |
| 28 | +], |
| 29 | +contents:[], |
| 30 | +definitions:{}, |
| 31 | +footnotes:{}, |
| 32 | +}); |
| 33 | +}); |
| 34 | + |
| 35 | +it('merges nodes',()=>{ |
| 36 | +constparser=create(); |
| 37 | +constmdast1=parser.tokenizeBlock('1\n'+'\n'+'here\n'+'\n'+'4\n'); |
| 38 | +constmdast2=parser.tokenizeBlock('3\n'+'\n'+'4\n'); |
| 39 | +constflat1=mdastToFlat(mdast1!); |
| 40 | +constflat2=mdastToFlat(mdast2!); |
| 41 | +constmerged=replace(flat1,3,flat2); |
| 42 | + |
| 43 | +expect(merged).toMatchObject({ |
| 44 | +nodes:[ |
| 45 | +{type:'root',children:[1,3,5],idx:0}, |
| 46 | +{type:'paragraph',children:[2],idx:1}, |
| 47 | +{type:'text',value:'1',idx:2}, |
| 48 | +{type:'portal',original:{type:'paragraph',children:[4],idx:3},children:[7]}, |
| 49 | +{type:'text',value:'here',idx:4}, |
| 50 | +{type:'paragraph',children:[6],idx:5}, |
| 51 | +{type:'text',value:'4',idx:6}, |
| 52 | +{type:'root',children:[8,10],idx:7}, |
| 53 | +{type:'paragraph',children:[9],idx:8}, |
| 54 | +{type:'text',value:'3',idx:9}, |
| 55 | +{type:'paragraph',children:[11],idx:10}, |
| 56 | +{type:'text',value:'4',idx:11}, |
| 57 | +], |
| 58 | +contents:[], |
| 59 | +definitions:{}, |
| 60 | +footnotes:{}, |
| 61 | +}); |
| 62 | +}); |
| 63 | + |
| 64 | +it('merges metadata',()=>{ |
| 65 | +constparser=create(); |
| 66 | +constmdast1=parser.tokenizeBlock(` |
| 67 | +# Click [here][link1] world![^foot] |
| 68 | +
|
| 69 | +merge here |
| 70 | +
|
| 71 | +[^foot]: This is footnote 1 |
| 72 | +[link1]: http://google.com |
| 73 | +`); |
| 74 | +constmdast2=parser.tokenizeBlock(` |
| 75 | +## [what][gg]?[^note] |
| 76 | +
|
| 77 | +[gg]: mailto:gg@bets.com |
| 78 | +[^note]: is dis... |
| 79 | +`); |
| 80 | +constflat1=mdastToFlat(mdast1!); |
| 81 | +constflat2=mdastToFlat(mdast2!); |
| 82 | +constmerged=replace(flat1,7,flat2); |
| 83 | + |
| 84 | +expect(merged).toMatchObject({ |
| 85 | +nodes:[ |
| 86 | +{type:'root',children:[1,7],idx:0}, |
| 87 | +{ |
| 88 | +type:'heading', |
| 89 | +children:[2,3,5,6], |
| 90 | +depth:1, |
| 91 | +idx:1, |
| 92 | +}, |
| 93 | +{type:'text',value:'Click ',idx:2}, |
| 94 | +{ |
| 95 | +type:'linkReference', |
| 96 | +children:[4], |
| 97 | +identifier:'link1', |
| 98 | +referenceType:'full', |
| 99 | +idx:3, |
| 100 | +}, |
| 101 | +{type:'text',value:'here',idx:4}, |
| 102 | +{type:'text',value:' world',idx:5}, |
| 103 | +{ |
| 104 | +type:'imageReference', |
| 105 | +identifier:'^foot', |
| 106 | +referenceType:'shortcut', |
| 107 | +alt:'^foot', |
| 108 | +idx:6, |
| 109 | +}, |
| 110 | +{ |
| 111 | +type:'portal', |
| 112 | +idx:7, |
| 113 | +original:{type:'paragraph',children:[8],idx:7}, |
| 114 | +children:[13], |
| 115 | +}, |
| 116 | +{type:'text',value:'merge here',idx:8}, |
| 117 | +{ |
| 118 | +type:'footnoteDefinition', |
| 119 | +children:[10], |
| 120 | +identifier:'foot', |
| 121 | +idx:9, |
| 122 | +}, |
| 123 | +{type:'paragraph',children:[11],idx:10}, |
| 124 | +{type:'text',value:'This is footnote 1',idx:11}, |
| 125 | +{ |
| 126 | +type:'definition', |
| 127 | +identifier:'link1', |
| 128 | +title:null, |
| 129 | +url:'http://google.com', |
| 130 | +idx:12, |
| 131 | +}, |
| 132 | +{type:'root',children:[14],idx:13}, |
| 133 | +{type:'heading',children:[15,17,18],depth:2,idx:14}, |
| 134 | +{ |
| 135 | +type:'linkReference', |
| 136 | +children:[16], |
| 137 | +identifier:'gg', |
| 138 | +referenceType:'full', |
| 139 | +idx:15, |
| 140 | +}, |
| 141 | +{type:'text',value:'what',idx:16}, |
| 142 | +{type:'text',value:'?',idx:17}, |
| 143 | +{type:'footnoteReference',value:'note',idx:18}, |
| 144 | +{ |
| 145 | +type:'definition', |
| 146 | +identifier:'gg', |
| 147 | +title:null, |
| 148 | +url:'mailto:gg@bets.com', |
| 149 | +idx:19, |
| 150 | +}, |
| 151 | +{type:'footnoteDefinition',children:[21],identifier:'note',idx:20}, |
| 152 | +{type:'paragraph',children:[22],idx:21}, |
| 153 | +{type:'text',value:'is dis…',idx:22}, |
| 154 | +], |
| 155 | +contents:[1,14], |
| 156 | +definitions:{link1:12,gg:19}, |
| 157 | +footnotes:{foot:9,note:20}, |
| 158 | +}); |
| 159 | +}); |
| 160 | +}); |