Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better solution for TOC #1489

Closed
ibreathebsb opened this issue May 16, 2019 · 1 comment
Closed

Better solution for TOC #1489

ibreathebsb opened this issue May 16, 2019 · 1 comment

Comments

@ibreathebsb
Copy link

Hi guys!

I have been using marked for an doc site recently, marked is a good library but when I realize that I need a TOC, it does not work very well. There are existing solutions like #545 ,while reading marked docs, I found a better(👀maybe?) solution, that is:

marked.lexer

for example, suppose we have the following markdown text

# A
> please ignore me 
## B
# C
## D

then we use marked.lexer(markdownText) to get the tokens:

[
  {
    "type": "heading",
    "depth": 1,
    "text": "A"
  },
  {
    "type": "blockquote_start"
  },
  {
    "type": "paragraph",
    "text": "please ignore me"
  },
  {
    "type": "blockquote_end"
  },
  {
    "type": "heading",
    "depth": 2,
    "text": "B"
  },
  {
    "type": "heading",
    "depth": 1,
    "text": "C"
  },
  {
    "type": "heading",
    "depth": 2,
    "text": "D"
  }
]

wow! that's all we need, right? now we can filter these headings and do whatever we want!

//  tokens is the result returned by marked.lexer()
const headings = tokens.filter(token => token.type === 'heading')
//
DO_WHATEVER_U_WANT(headings)

👏 That's all, wish this could help, thanks!

@UziTech
Copy link
Member

UziTech commented May 16, 2019

This would probably be better as a comment on #545

We are trying to consolidate the issues so I am going to close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants