{"id":30180954,"url":"https://github.com/streamich/mdast-flat","last_synced_at":"2025-08-12T08:06:32.391Z","repository":{"id":39338412,"uuid":"172551871","full_name":"streamich/mdast-flat","owner":"streamich","description":"Flat version of MDAST format","archived":false,"fork":false,"pushed_at":"2025-08-08T17:24:20.000Z","size":1067,"stargazers_count":5,"open_issues_count":28,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-08T17:27:26.776Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/streamich.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"streamich"}},"created_at":"2019-02-25T17:26:42.000Z","updated_at":"2025-08-08T17:23:46.000Z","dependencies_parsed_at":"2023-02-02T20:31:30.647Z","dependency_job_id":"5df03896-434a-44f5-8a82-677700994817","html_url":"https://github.com/streamich/mdast-flat","commit_stats":{"total_commits":349,"total_committers":6,"mean_commits":"58.166666666666664","dds":"0.12320916905444124","last_synced_commit":"db7b77410229db7c77268a30deb1fba6e934da4b"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/streamich/mdast-flat","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/streamich%2Fmdast-flat","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/streamich%2Fmdast-flat/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/streamich%2Fmdast-flat/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/streamich%2Fmdast-flat/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/streamich","download_url":"https://codeload.github.com/streamich/mdast-flat/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/streamich%2Fmdast-flat/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269465056,"owners_count":24421727,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-08T02:00:09.200Z","response_time":72,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-08-12T08:06:15.988Z","updated_at":"2025-08-12T08:06:32.383Z","avatar_url":"https://github.com/streamich.png","language":"TypeScript","funding_links":["https://github.com/sponsors/streamich"],"categories":[],"sub_categories":[],"readme":"# mdast-flat\n\n- Flat version of [MDAST format](https://github.com/syntax-tree/mdast).\n- Nodes are stored in a flat `nodes` array instead of a nested tree.\n- Table of contents is available in `contents` key.\n- All definitions are available in `definitions` map.\n- All footnotes are available in `footnotes` map.\n\nMain difference from MDAST is that `Parent` node `children` property is an\narray of numbers (instead of array of nodes). Numbers are indices into `nodes`\narray, which contains all nodes.\n\n```idl\ninterface FlatParent \u003c: Parent {\n  idx: number\n  children: [number]\n}\n```\n\n`idx` is index of the current node, `children` contains array of children indices.\n\nFull document schema:\n\n```idl\ninterface MdastFlat {\n  nodes: [Root | FlatParent | Literal]\n  contents: [number]\n  definitions: {[identifier]: number}\n  footnotes: {[identifier]: number}\n  footnoteOrder: [number]\n}\n```\n\n- `node` \u0026mdash; a flat array of document nodes.\n- `contents` \u0026mdash; an array `nodes` indices, which are `heading` nodes.\n- `definitions` \u0026mdash; a map of definition identifiers into `nodes` indices.\n- `footnotes` \u0026mdash; a map of footnote identifiers into `nodes` indices.\n- `footnoteOrder` \u0026mdash; ordered list of footnote node indices.\n\n\n### Nodes\n\nMDAST-Flat nodes have the following attributes.\n\n- All the same attributes as MDAST tokens, except see below.\n- `children` attribute is an array of numbers that index into `nodes` list.\n- `idx` a number, which is the index of current node in `nodes` list.\n- `parent` a number, which is the index of parent node in the `nodes` list.\n- Root nodes also have `depth` attribute which tracks\n  its depth if another document was merged in using `replace` function.\n\n## Usage\n\n```js\nimport {mdastToFlat, flatToMdast} from 'mdast-flat';\n\nconst flat = mdastToFlat(mdast1);\nconst mdast2 = flatToMdast(flat);\n```\n\n\n## Reference\n\n- `mdastToFlat(mdast)` \u0026mdash; converts MDAST to MDAST-Flat.\n- `flatToMdast(flat)` \u0026mdash; converts MDAST-Flat to MDAST.\n- `replace(flat1, idx, flat2)` \u0026mdash; replaces node `idx` in `flat1` by `flat2`.\n\n\n## Example\n\nLet's say you have the following Markdown.\n\n    [Click me][click]\n\n    [click]: https://github.com/\n\nYou could convert it to MDAST using [`very-small-parser`](https://github.com/streamich/very-small-parser).\n\n```yml\ntype: root\nchildren:\n- type: paragraph\n  children:\n  - type: linkReference\n    children:\n    - type: text\n      value: Click me\n    identifier: click\n    referenceType: full\n- type: definition\n  identifier: click\n  title:\n  url: https://github.com/\n```\n\nUsing `mdastToFlat()` function you can covert it to MDAST-Flat.\n\n```yml\nnodes:\n- type: root\n  children:\n  - 1\n- type: paragraph\n  children:\n  - 2\n- type: linkReference\n  children:\n  - 3\n  identifier: click\n  referenceType: full\n- type: text\n  value: Click me\n- type: definition\n  identifier: click\n  title:\n  url: https://github.com/\ncontents: []\ndefinitions:\n  click: 4\nfootnotes: {}\nfootnoteOrder: []\n```\n\n### Replacing node with document\n\nYou can use `replace()` function to insert a Markdown document inside another Markdown\ndocument instead of some specified node.\n\nConsider you have a Markdown document.\n\n    1\n\n    replace me\n\nAnd another document.\n\n    2\n\nLet's say you have parsed both documents into `flat1` and `flat2` MDAST-Flat objects, respectively.\nNow you want to insert the second document inside the first document in place of `replace me`\nparagraph (which has `idx` of `3` in `flat1`);\n\n```js\nconst merged = replace(flat1, 3, flat2);\n```\n\nThe result is MDAST-Flat `merged` object, which merges all nodes using new `portal` node. It also\nmerges `contents`, `definitions` and `footnotes`, if there are any.\n\n```yml\nnodes:\n- type: root\n  children:\n  - 1\n  - 3\n  idx: 0\n- type: paragraph\n  children:\n  - 2\n  idx: 1\n- type: text\n  value: '1'\n  idx: 2\n- type: portal\n  idx: 3\n  original:\n    type: paragraph\n    children:\n    - 4\n    idx: 3\n  children:\n  - 5\n- type: text\n  value: replace me\n  idx: 4\n- type: root\n  children:\n  - 6\n  idx: 5\n- type: paragraph\n  children:\n  - 7\n  idx: 6\n- type: text\n  value: '2'\n  idx: 7\ncontents: []\ndefinitions: {}\nfootnotes: {}\nfootnoteOrder: []\n```\n\nResulting Markdown equivalent is:\n\n    1\n\n    2\n\n\n## License\n\n[Unlicense](LICENSE) \u0026mdash; public domain.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstreamich%2Fmdast-flat","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstreamich%2Fmdast-flat","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstreamich%2Fmdast-flat/lists"}