evilstreak / markdown-js

A Markdown parser for javascript
7.69k stars 863 forks source link

support parase "[TOC]"? #306

Open huanglw opened 5 years ago

huanglw commented 5 years ago

It seems not support "[TOC]" default?

CyanGlory commented 5 years ago

you may need this:

wrapper the provided html, and add some script:

<script>
    // add a basic TOC for markdown-js
    const headers = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
    headers.forEach((value, index, array) => {
      const tags = document.getElementsByTagName(value);
      for (let index = 0; index < tags.length; index++) {
        tags[index].setAttribute('id', tags[index].innerText);
      }
    });
  </script>