Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork0
Flat version of MDAST format
License
streamich/mdast-flat
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
- Flat version ofMDAST format.
- Nodes are stored in a flat
nodesarray instead of a nested tree. - Table of contents is available in
contentskey. - All definitions are available in
definitionsmap. - All footnotes are available in
footnotesmap.
Main difference from MDAST is thatParent nodechildren property is anarray of numbers (instead of array of nodes). Numbers are indices intonodesarray, which contains all nodes.
interface FlatParent<: Parent { idx: number children: [number]}
idx is index of the current node,children contains array of children indices.
Full document schema:
interface MdastFlat { nodes: [Root| FlatParent| Literal] contents: [number] definitions: {[identifier]: number} footnotes: {[identifier]: number} footnoteOrder: [number]}node— a flat array of document nodes.contents— an arraynodesindices, which areheadingnodes.definitions— a map of definition identifiers intonodesindices.footnotes— a map of footnote identifiers intonodesindices.footnoteOrder— ordered list of footnote node indices.
MDAST-Flat nodes have the following attributes.
- All the same attributes as MDAST tokens, except see below.
childrenattribute is an array of numbers that index intonodeslist.idxa number, which is the index of current node innodeslist.parenta number, which is the index of parent node in thenodeslist.- Root nodes also have
depthattribute which tracksits depth if another document was merged in usingreplacefunction.
import{mdastToFlat,flatToMdast}from'mdast-flat';constflat=mdastToFlat(mdast1);constmdast2=flatToMdast(flat);
mdastToFlat(mdast)— converts MDAST to MDAST-Flat.flatToMdast(flat)— converts MDAST-Flat to MDAST.replace(flat1, idx, flat2)— replaces nodeidxinflat1byflat2.
Let's say you have the following Markdown.
[Click me][click][click]: https://github.com/You could convert it to MDAST usingvery-small-parser.
type:rootchildren:-type:paragraphchildren: -type:linkReferencechildren: -type:textvalue:Click meidentifier:clickreferenceType:full-type:definitionidentifier:clicktitle:url:https://github.com/
UsingmdastToFlat() function you can covert it to MDAST-Flat.
nodes:-type:rootchildren: -1-type:paragraphchildren: -2-type:linkReferencechildren: -3identifier:clickreferenceType:full-type:textvalue:Click me-type:definitionidentifier:clicktitle:url:https://github.com/contents:[]definitions:click:4footnotes:{}footnoteOrder:[]
You can usereplace() function to insert a Markdown document inside another Markdowndocument instead of some specified node.
Consider you have a Markdown document.
1replace meAnd another document.
2Let's say you have parsed both documents intoflat1 andflat2 MDAST-Flat objects, respectively.Now you want to insert the second document inside the first document in place ofreplace meparagraph (which hasidx of3 inflat1);
constmerged=replace(flat1,3,flat2);
The result is MDAST-Flatmerged object, which merges all nodes using newportal node. It alsomergescontents,definitions andfootnotes, if there are any.
nodes:-type:rootchildren: -1 -3idx:0-type:paragraphchildren: -2idx:1-type:textvalue:'1'idx:2-type:portalidx:3original:type:paragraphchildren: -4idx:3children: -5-type:textvalue:replace meidx:4-type:rootchildren: -6idx:5-type:paragraphchildren: -7idx:6-type:textvalue:'2'idx:7contents:[]definitions:{}footnotes:{}footnoteOrder:[]
Resulting Markdown equivalent is:
12Unlicense — public domain.
About
Flat version of MDAST format
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors5
Uh oh!
There was an error while loading.Please reload this page.