Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0b7f288

Browse files
authored
Merge pull request#2 from streamich/idx
Idx
2 parents3c4db04 +1be40b2 commit0b7f288

File tree

9 files changed

+300
-15
lines changed

9 files changed

+300
-15
lines changed

‎README.md‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ array, which contains all nodes.
1212

1313
```idl
1414
interface FlatParent <: Parent {
15+
idx: number
1516
children: [number]
1617
}
1718
```
1819

20+
`idx` is index of the current node,`children` contains array of children indices.
21+
1922
Full document schema:
2023

2124
```idl
@@ -51,6 +54,7 @@ const mdast2 = flatToMdast(flat);
5154

5255
-`mdastToFlat(mdast)`&mdash; converts MDAST to MDAST-Flat.
5356
-`flatToMdast(flat)`&mdash; converts MDAST-Flat to MDAST.
57+
-`replace(flat1, idx, flat2)`&mdash; replaces node`idx` in`flat1` by`flat2`.
5458

5559

5660
##Example
@@ -107,6 +111,80 @@ definitions:
107111
footnotes: {}
108112
```
109113

114+
### Replacing node with document
115+
116+
You can use `replace()` function to insert a Markdown document inside another Markdown
117+
document instead of some specified node.
118+
119+
Consider you have a Markdown document.
120+
121+
1
122+
123+
replace me
124+
125+
And another document.
126+
127+
2
128+
129+
Let's say you have parsed both documents into `flat1` and `flat2` MDAST-Flat objects, respectively.
130+
Now you want to insert the second document inside the first document in place of `replace me`
131+
paragraph (which has `idx` of `3` in `flat1`);
132+
133+
```js
134+
const merged = replace(flat1, 3, flat2);
135+
```
136+
137+
The result is MDAST-Flat `merged` object, which merges all nodes using new `portal` node. It also
138+
merges `contents`, `definitions` and `footnotes`, if there are any.
139+
140+
```yml
141+
nodes:
142+
- type: root
143+
children:
144+
- 1
145+
- 3
146+
idx: 0
147+
- type: paragraph
148+
children:
149+
- 2
150+
idx: 1
151+
- type: text
152+
value: '1'
153+
idx: 2
154+
- type: portal
155+
idx: 3
156+
original:
157+
type: paragraph
158+
children:
159+
- 4
160+
idx: 3
161+
children:
162+
- 5
163+
- type: text
164+
value: replace me
165+
idx: 4
166+
- type: root
167+
children:
168+
- 6
169+
idx: 5
170+
- type: paragraph
171+
children:
172+
- 7
173+
idx: 6
174+
- type: text
175+
value: '2'
176+
idx: 7
177+
contents: []
178+
definitions: {}
179+
footnotes: {}
180+
```
181+
182+
Resulting Markdown equivalent is:
183+
184+
1
185+
186+
2
187+
110188

111189
## License
112190

‎src/__tests__/flatToMdast.spec.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ describe('structure', () => {
1010
nodes:[
1111
{
1212
type:'root',
13-
}
14-
]
13+
},
14+
],
1515
};
1616
constmdast=flatToMdast(flatasany);
1717

@@ -249,7 +249,7 @@ describe('structure', () => {
249249
children:[
250250
{
251251
type:'text',
252-
value:'world!'
252+
value:'world!',
253253
},
254254
],
255255
},

‎src/__tests__/mdastToFlat.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ describe('structure', () => {
248248
});
249249
});
250250

251-
it.only('all elements twice or more',()=>{
251+
it('all elements twice or more',()=>{
252252
constparser=create();
253253
constmd=fs.readFileSync(__dirname+'/md/all-elements-twice.md','utf8');
254254
constmdast=parser.tokenizeBlock(md)!;

‎src/__tests__/replace.spec.ts‎

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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+
});

‎src/flatToMdast.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {IRoot, TAnyToken, TBlockToken} from 'md-mdast/lib/types';
33

44
exportconstflatToMdast:FlatToMdast=(flat:Flat)=>{
55
consttraverse:(index:number)=>IRoot|TAnyToken=(index)=>{
6-
constnode:any={...flat.nodes[index]};
6+
const{idx:omit,...node}=flat.nodes[index]asany;
77
if(node.children)node.children=node.children.map(traverse);
88
returnnode;
99
};
@@ -12,12 +12,12 @@ export const flatToMdast: FlatToMdast = (flat: Flat) => {
1212
if(!mdast.children)mdast.children=[];
1313

1414
if(flat.definitionsinstanceofObject){
15-
Object.values(flat.definitions).forEach(index=>{
15+
Object.values(flat.definitions).forEach((index)=>{
1616
mdast.children.push(traverse(index)asTBlockToken);
1717
});
1818
}
1919
if(flat.footnotesinstanceofObject){
20-
Object.values(flat.footnotes).forEach(index=>{
20+
Object.values(flat.footnotes).forEach((index)=>{
2121
mdast.children.push(traverse(index)asTBlockToken);
2222
});
2323
}

‎src/index.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
export*from'./mdastToFlat';
2+
export*from'./flatToMdast';
3+
export*from'./replace';

‎src/mdastToFlat.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export const mdastToFlat: MdastToFlat = (mdast) => {
1414
};
1515

1616
consttraverse=(token:IRoot|TAnyToken):number=>{
17-
constindex=nodes.length;
18-
constnode={...token}asTNode;
17+
constidx=nodes.length;
18+
constnode={...token, idx}asTNode;
1919
nodes.push(node);
2020

2121
if(token.children){
@@ -31,16 +31,16 @@ export const mdastToFlat: MdastToFlat = (mdast) => {
3131

3232
switch(node.type){
3333
case'heading':
34-
contents.push(index);
35-
returnindex;
34+
contents.push(idx);
35+
returnidx;
3636
case'definition':
37-
definitions[node.identifier]=index;
37+
definitions[node.identifier]=idx;
3838
return-1;
3939
case'footnoteDefinition':
40-
footnotes[node.identifier]=index;
40+
footnotes[node.identifier]=idx;
4141
return-1;
4242
default:
43-
returnindex;
43+
returnidx;
4444
}
4545
};
4646

‎src/replace.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import{Flat}from'./types';
2+
3+
exportconstreplace=(into:Flat,at:number,what:Flat):Flat=>{
4+
constmergeIdx=into.nodes.length;
5+
constmerged:Flat={
6+
nodes:[...into.nodes],
7+
contents:[...into.contents],
8+
definitions:{...into.definitions},
9+
footnotes:{...into.footnotes},
10+
};
11+
12+
constreplacedNode=merged.nodes[at];
13+
merged.nodes[at]={
14+
type:'portal',
15+
idx:at,
16+
original:replacedNode,
17+
children:[mergeIdx],
18+
}asany;
19+
20+
// APPEND NODES.
21+
for(constnodeofwhat.nodes){
22+
constnewNode:any={
23+
...node,
24+
idx:node.idx+mergeIdx,
25+
};
26+
27+
if(newNode.children){
28+
newNode.children=newNode.children.map((idx)=>idx+mergeIdx);
29+
}
30+
merged.nodes.push(newNode);
31+
}
32+
33+
// MERGE METADATA.
34+
for(constidxofwhat.contents){
35+
merged.contents.push(idx+mergeIdx);
36+
}
37+
Object.keys(what.definitions).forEach(
38+
(identifier)=>(merged.definitions[identifier]=what.definitions[identifier]+mergeIdx),
39+
);
40+
Object.keys(what.footnotes).forEach(
41+
(identifier)=>(merged.footnotes[identifier]=what.footnotes[identifier]+mergeIdx),
42+
);
43+
44+
returnmerged;
45+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp