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

Flat version of MDAST format

License

NotificationsYou must be signed in to change notification settings

streamich/mdast-flat

Repository files navigation

  • Flat version ofMDAST format.
  • Nodes are stored in a flatnodes array instead of a nested tree.
  • Table of contents is available incontents key.
  • All definitions are available indefinitions map.
  • All footnotes are available infootnotes map.

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 arraynodes indices, which areheading nodes.
  • definitions — a map of definition identifiers intonodes indices.
  • footnotes — a map of footnote identifiers intonodes indices.
  • footnoteOrder — ordered list of footnote node indices.

Nodes

MDAST-Flat nodes have the following attributes.

  • All the same attributes as MDAST tokens, except see below.
  • children attribute is an array of numbers that index intonodes list.
  • idx a number, which is the index of current node innodes list.
  • parent a number, which is the index of parent node in thenodes list.
  • Root nodes also havedepth attribute which tracksits depth if another document was merged in usingreplace function.

Usage

import{mdastToFlat,flatToMdast}from'mdast-flat';constflat=mdastToFlat(mdast1);constmdast2=flatToMdast(flat);

Reference

  • mdastToFlat(mdast) — converts MDAST to MDAST-Flat.
  • flatToMdast(flat) — converts MDAST-Flat to MDAST.
  • replace(flat1, idx, flat2) — replaces nodeidx inflat1 byflat2.

Example

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:[]

Replacing node with document

You can usereplace() function to insert a Markdown document inside another Markdowndocument instead of some specified node.

Consider you have a Markdown document.

1replace me

And another document.

2

Let'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:

12

License

Unlicense — public domain.

About

Flat version of MDAST format

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors5


[8]ページ先頭

©2009-2025 Movatter.jp