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

Commitc120131

Browse files
committed
feat: 🎸 add mdastToFlat function
1 parent0bf4231 commitc120131

File tree

8 files changed

+325
-8
lines changed

8 files changed

+325
-8
lines changed

‎README.md‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
- All footnotes are available in`footnotes` map.
88

99
Main difference from MDAST is that`Parent` node`children` property is an
10-
array of numbers (instead of array of nodes). Numbers are indicesto`node`
10+
array of numbers (instead of array of nodes). Numbers are indicesinto`nodes`
1111
array, which contain all nodes.
1212

1313
```idl
@@ -19,10 +19,8 @@ interface FlatParent <: Parent {
1919
Full document schema:
2020

2121
```idl
22-
type Node = Root | FlatParent | Literal
23-
2422
interface MdastFlat {
25-
nodes: [Node]
23+
nodes: [Root | FlatParent | Literal]
2624
contents: [Heading]
2725
definitions: {
2826
[identifier]: Definition

‎src/__tests__/index.spec.ts‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎src/__tests__/mdastToFlat.spec.ts‎

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
import{create}from'md-mdast';
2+
import{mdastToFlat}from'../mdastToFlat';
3+
4+
describe('structure',()=>{
5+
it('exists',()=>{
6+
expect(typeofmdastToFlat).toBe('function');
7+
});
8+
9+
it('returns correct document shape',()=>{
10+
constparser=create();
11+
constmdast=parser.tokenizeBlock('foo');
12+
constdoc=mdastToFlat(mdast!);
13+
14+
expect(mdast).toMatchObject({
15+
type:'root',
16+
children:[
17+
{
18+
type:'paragraph',
19+
children:[
20+
{
21+
type:'text',
22+
value:'foo',
23+
},
24+
],
25+
},
26+
],
27+
});
28+
expect(doc).toMatchObject({
29+
nodes:[
30+
{
31+
type:'root',
32+
children:[1],
33+
},
34+
{
35+
type:'paragraph',
36+
children:[2],
37+
},
38+
{
39+
type:'text',
40+
value:'foo',
41+
},
42+
],
43+
contents:[],
44+
definitions:{},
45+
footnotes:{},
46+
});
47+
});
48+
49+
it('adds titles to contents list',()=>{
50+
constparser=create();
51+
constmdast=parser.tokenizeBlock('# Title\n'+'\n'+'## Subtitle\n');
52+
constdoc=mdastToFlat(mdast!);
53+
54+
expect(mdast).toMatchObject({
55+
type:'root',
56+
children:[
57+
{
58+
type:'heading',
59+
depth:1,
60+
children:[
61+
{
62+
type:'text',
63+
value:'Title',
64+
},
65+
],
66+
},
67+
{
68+
type:'heading',
69+
depth:2,
70+
children:[
71+
{
72+
type:'text',
73+
value:'Subtitle',
74+
},
75+
],
76+
},
77+
],
78+
});
79+
expect(doc).toMatchObject({
80+
nodes:[
81+
{
82+
type:'root',
83+
children:[1,3],
84+
},
85+
{
86+
type:'heading',
87+
children:[2],
88+
},
89+
{
90+
type:'text',
91+
value:'Title',
92+
},
93+
{
94+
type:'heading',
95+
children:[4],
96+
},
97+
{
98+
type:'text',
99+
value:'Subtitle',
100+
},
101+
],
102+
contents:[1,3],
103+
definitions:{},
104+
footnotes:{},
105+
});
106+
});
107+
108+
it('structure link definitions',()=>{
109+
constparser=create();
110+
constmdast=parser.tokenizeBlock('[Click me][click]\n'+'\n'+'[click]: https://github.com/');
111+
constdoc=mdastToFlat(mdast!);
112+
113+
expect(mdast).toMatchObject({
114+
type:'root',
115+
children:[
116+
{
117+
type:'paragraph',
118+
children:[
119+
{
120+
type:'linkReference',
121+
children:[
122+
{
123+
type:'text',
124+
value:'Click me',
125+
},
126+
],
127+
identifier:'click',
128+
referenceType:'full',
129+
},
130+
],
131+
},
132+
{
133+
type:'definition',
134+
identifier:'click',
135+
title:null,
136+
url:'https://github.com/',
137+
},
138+
],
139+
});
140+
expect(doc).toMatchObject({
141+
nodes:[
142+
{
143+
type:'root',
144+
children:[1],
145+
},
146+
{
147+
type:'paragraph',
148+
children:[2],
149+
},
150+
{
151+
type:'linkReference',
152+
children:[3],
153+
identifier:'click',
154+
referenceType:'full',
155+
},
156+
{
157+
type:'text',
158+
value:'Click me',
159+
},
160+
{
161+
type:'definition',
162+
identifier:'click',
163+
title:null,
164+
url:'https://github.com/',
165+
},
166+
],
167+
contents:[],
168+
definitions:{
169+
click:4,
170+
},
171+
footnotes:{},
172+
});
173+
});
174+
175+
it('a footnote',()=>{
176+
constparser=create();
177+
constmdast=parser.tokenizeBlock('Hello[^gg]\n'+'\n'+'[^gg]: world!');
178+
constdoc=mdastToFlat(mdast!);
179+
180+
expect(mdast).toMatchObject({
181+
type:'root',
182+
children:[
183+
{
184+
type:'paragraph',
185+
children:[
186+
{
187+
type:'text',
188+
value:'Hello',
189+
},
190+
{
191+
type:'footnoteReference',
192+
value:'gg',
193+
},
194+
],
195+
},
196+
{
197+
type:'footnoteDefinition',
198+
identifier:'gg',
199+
children:[
200+
{
201+
type:'paragraph',
202+
children:[
203+
{
204+
type:'text',
205+
},
206+
],
207+
},
208+
],
209+
},
210+
],
211+
});
212+
expect(doc).toMatchObject({
213+
nodes:[
214+
{
215+
type:'root',
216+
children:[1],
217+
},
218+
{
219+
type:'paragraph',
220+
children:[2,3],
221+
},
222+
{
223+
type:'text',
224+
value:'Hello',
225+
},
226+
{
227+
type:'footnoteReference',
228+
},
229+
{
230+
type:'footnoteDefinition',
231+
children:[5],
232+
},
233+
{
234+
type:'paragraph',
235+
children:[6],
236+
},
237+
{
238+
type:'text',
239+
value:'world!',
240+
},
241+
],
242+
contents:[],
243+
definitions:{},
244+
footnotes:{
245+
gg:4,
246+
},
247+
});
248+
});
249+
});

‎src/index.ts‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎src/index.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export*from'./mdastToFlat';

‎src/mdastToFlat.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import{MdastToFlat,TNode,FlatDefinitions,FlatFootnotes}from'./types';
2+
import{IRoot,TAnyToken}from'md-mdast/lib/types';
3+
4+
exportconstmdastToFlat:MdastToFlat=(mdast)=>{
5+
constnodes:TNode[]=[];
6+
constcontents:number[]=[];
7+
constdefinitions:FlatDefinitions={};
8+
constfootnotes:FlatFootnotes={};
9+
constdoc={
10+
nodes,
11+
contents,
12+
definitions,
13+
footnotes,
14+
};
15+
16+
consttraverse=(token:IRoot|TAnyToken):number=>{
17+
constindex=nodes.length;
18+
constnode={...token}asTNode;
19+
nodes.push(node);
20+
21+
if(token.children){
22+
if(token.childreninstanceofArray){
23+
node.children=(token.childrenasany).map(traverse).filter((i:number)=>i>-1);
24+
}else{
25+
constchildIndex=traverse(token.children);
26+
if(childIndex>-1){
27+
node.children=[childIndex]asany;
28+
}
29+
}
30+
}
31+
32+
switch(node.type){
33+
case'heading':
34+
contents.push(index);
35+
returnindex;
36+
case'definition':
37+
definitions[node.identifier]=index;
38+
return-1;
39+
case'footnoteDefinition':
40+
footnotes[node.identifier]=index;
41+
return-1;
42+
default:
43+
returnindex;
44+
}
45+
};
46+
47+
if(mdast){
48+
traverse(mdast);
49+
}
50+
51+
returndoc;
52+
};

‎src/types.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import{IRoot,TAnyToken}from'md-mdast/lib/types';
2+
3+
exportinterfaceFlatDefinitions{
4+
[id:string]:number;
5+
}
6+
7+
exportinterfaceFlatFootnotes{
8+
[id:string]:number;
9+
}
10+
11+
exporttypeTNode=(IRoot|TAnyToken)&{children?:number[]};
12+
13+
exportinterfaceFlat{
14+
nodes:TNode[];
15+
contents:number[];
16+
definitions:FlatDefinitions;
17+
footnotes:FlatFootnotes;
18+
}
19+
20+
exporttypeMdastToFlat=(mdast:IRoot|TAnyToken)=>Flat;

‎tsconfig.json‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target":"es2018",
3+
"target":"es5",
44
"module":"commonjs",
55
"moduleResolution":"Node",
66
"removeComments":true,
@@ -14,7 +14,6 @@
1414
"pretty":true,
1515
"sourceMap":false,
1616
"strict":true,
17-
"jsx":"react",
1817
"esModuleInterop":true,
1918
"forceConsistentCasingInFileNames":true,
2019
"noEmitHelpers":true,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp