0.2.0 • Published 7 years ago

marked-token-tree v0.2.0

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

marked-token-tree

Build a tocken tree from marked tokens.


  1. Installation
  2. Usage
  3. Get the tree object
  4. Walk the token tree

Instalation

npm install --dev marked-token-tree

Usage

Get the tree object

const tree = require('marked-token-tre')

const source = `
    # Main heading
    Main block text

    ## Section 1
    Second level block text

    ### Section 1.1
    Third level block text

    ### Section 1.2
    Third level block text

    \```stylus
    .element
    color: black
    \```

    \```css
    .element {
    color: #000;
    }
    \```

    ## Section 2

    Second level block text
  `;
  
  const output = tree(source);
  
  console.log(output);

will output:

{  
  "heading":"Main heading",
    "depth":1,
    "content":[ {  
      "type":"paragraph",
      "text":"Main block text"
    }],
    "children":[{  
      "heading":"Section 1",
      "depth":2,
      "content":[{  
        "type":"paragraph",
        "text":"Second level block text"
      }],
      "children":[{  
        "heading":"Section 1.1",
        "depth":3,
        "content":[{  
            "type":"paragraph",
            "text":"Third level block text"
          }]
      }, {  
        "heading":"Section 1.2",
        "depth":3,
        "content": [{  
            "type":"paragraph",
            "text":"Third level block text"
          }, {  
            "type":"code",
            "text":".element\n    color: black",
            "lang":"stylus"
          }, {  
            "type":"code",
            "text":".element {\n    color: #000;\n}",
            "lang":"css"
          }]
      }]
    }, {  
      "heading":"Section 2",
      "depth":2,
      "content":[{  
          "type":"paragraph",
          "text":"Second level block text"
        }]
    }]
}

Walk the token tree

const fs = require('fs')
const tree = require('marked-token-tree')
const source = fs.readFileSync('./source.md')

source.walk((token, block) => {
  // process token and coresponding block
})
0.2.0

7 years ago

0.1.0

7 years ago

0.0.1

7 years ago