Coder Social home page Coder Social logo

mdast-flat's Introduction

mdast-flat

  • Flat version of MDAST format.
  • Nodes are stored in a flat nodes array instead of a nested tree.
  • Table of contents is available in contents key.
  • All definitions are available in definitions map.
  • All footnotes are available in footnotes map.

Main difference from MDAST is that Parent node children property is an array of numbers (instead of array of nodes). Numbers are indices into nodes array, 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 array nodes indices, which are heading nodes.
  • definitions — a map of definition identifiers into nodes indices.
  • footnotes — a map of footnote identifiers into nodes 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 into nodes list.
  • idx a number, which is the index of current node in nodes list.
  • parent a number, which is the index of parent node in the nodes list.
  • Root nodes also have depth attribute which tracks its depth if another document was merged in using replace function.

Usage

import {mdastToFlat, flatToMdast} from 'mdast-flat';

const flat = mdastToFlat(mdast1);
const mdast2 = flatToMdast(flat);

Reference

  • mdastToFlat(mdast) — converts MDAST to MDAST-Flat.
  • flatToMdast(flat) — converts MDAST-Flat to MDAST.
  • replace(flat1, idx, flat2) — replaces node idx in flat1 by flat2.

Example

Let's say you have the following Markdown.

[Click me][click]

[click]: https://github.com/

You could convert it to MDAST using md-mdast.

type: root
children:
- type: paragraph
  children:
  - type: linkReference
    children:
    - type: text
      value: Click me
    identifier: click
    referenceType: full
- type: definition
  identifier: click
  title:
  url: https://github.com/

Using mdastToFlat() function you can covert it to MDAST-Flat.

nodes:
- type: root
  children:
  - 1
- type: paragraph
  children:
  - 2
- type: linkReference
  children:
  - 3
  identifier: click
  referenceType: full
- type: text
  value: Click me
- type: definition
  identifier: click
  title:
  url: https://github.com/
contents: []
definitions:
  click: 4
footnotes: {}
footnoteOrder: []

Replacing node with document

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

Consider you have a Markdown document.

1

replace me

And another document.

2

Let's say you have parsed both documents into flat1 and flat2 MDAST-Flat objects, respectively. Now you want to insert the second document inside the first document in place of replace me paragraph (which has idx of 3 in flat1);

const merged = replace(flat1, 3, flat2);

The result is MDAST-Flat merged object, which merges all nodes using new portal node. It also merges contents, definitions and footnotes, if there are any.

nodes:
- type: root
  children:
  - 1
  - 3
  idx: 0
- type: paragraph
  children:
  - 2
  idx: 1
- type: text
  value: '1'
  idx: 2
- type: portal
  idx: 3
  original:
    type: paragraph
    children:
    - 4
    idx: 3
  children:
  - 5
- type: text
  value: replace me
  idx: 4
- type: root
  children:
  - 6
  idx: 5
- type: paragraph
  children:
  - 7
  idx: 6
- type: text
  value: '2'
  idx: 7
contents: []
definitions: {}
footnotes: {}
footnoteOrder: []

Resulting Markdown equivalent is:

1

2

License

Unlicense — public domain.

mdast-flat's People

Contributors

dependabot[bot] avatar renovate-bot avatar renovate[bot] avatar semantic-release-bot avatar streamich avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mdast-flat's Issues

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (github>whitesource/merge-confidence:beta)

The automated release is failing 🚨

🚨 The automated release from the master branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you could benefit from your bug fixes and new features.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can resolve this 💪.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here is some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


Invalid npm token.

The npm token configured in the NPM_TOKEN environment variable must be a valid token allowing to publish to the registry https://registry.npmjs.org/.

If you are using Two-Factor Authentication, make configure the auth-only level is supported. semantic-release cannot publish with the default auth-and-writes level.

Please make sure to set the NPM_TOKEN environment variable in your CI with the exact value of the npm token.


Good luck with your project ✨

Your semantic-release bot 📦🚀

Remove empty paragraphs

There are empty paragraph nodes generated, possibly when definitions are removed, don't generate empty paragraphs.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update dependency prettier to v3
  • chore(deps): update dependency rimraf to v5
  • chore(deps): update dependency typescript to v5
  • 🔐 Create all rate-limited PRs at once 🔐

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

circleci
.circleci/config.yml
  • node 10.24
npm
package.json
  • md-mdast ^3.1.0
  • @semantic-release/changelog 5.0.1
  • @semantic-release/git 9.0.0
  • @semantic-release/npm 7.1.1
  • @types/jest 24.9.1
  • @types/node 14.17.11
  • husky 6.0.0
  • jest 27.5.1
  • lint-staged 11.1.2
  • prettier 2.8.8
  • pretty-quick 3.1.3
  • rimraf 3.0.2
  • semantic-release 17.4.2
  • ts-jest 27.1.5
  • ts-node 9.1.1
  • tslint 5.20.1
  • tslint-config-common 1.6.2
  • typescript 3.5.3
  • node >=6.9

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.