evilstreak / markdown-js

A Markdown parser for javascript
7.7k stars 860 forks source link

support parase "[TOC]"? #306

Open huanglw opened 6 years ago

huanglw commented 6 years ago

It seems not support "[TOC]" default?

CyanGlory commented 6 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>