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

Extract a Table of Contents from a Markdown document #18

Closed
RobertDober opened this issue Sep 24, 2021 · 0 comments
Closed

Extract a Table of Contents from a Markdown document #18

RobertDober opened this issue Sep 24, 2021 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@RobertDober
Copy link
Owner

RobertDober commented Sep 24, 2021

Synopsis

As Extractly is used by Earmark and potentially by EarmarkParser parsing the markdown is not an option (and probably total overkill anyway).

Therefore scanning for lines matching ~r{\A \s{0,3} (#{1,7}) \s+ (.*)}x shall deliver the necessary information.

Sketch

With that in mind such a list shall be obtained by the following pseudo code

     lines
     |> Enum.map(&Regex.run(~r{\A \s{0,3} (#{1,7}) \s+ (.*)}x, &1)
     |> Enum.filter(& &1)
     |> Enum.map(fn [_, header, title] -> {String.length(header), title} end) 

yielding, e.g. ...

   [{1, "Main"}, {2, "Sub 1"}, {2, "Sub 2"}, {3, "SubSub"}, {1, "Epilogue"}]

which than, depending on some options shall yield markdown, e.g.

1. Main
   1. Sub 1
   1. Sub 2
      1. SubSub
1. Epilogue

Options

  • numbered: 1 value for starting position, nil for unnumbered lists
  • levels: 7 maximum level of headers taken into account
  • output: :markdown alternatives like :ast, :html or :json might be a nice enhancement

Limitations

Setext headers are not taken into account, as I do not have a use case for now

@RobertDober RobertDober added the enhancement New feature or request label Sep 24, 2021
@RobertDober RobertDober self-assigned this Sep 24, 2021
@RobertDober RobertDober added this to the 0.5.2 milestone Sep 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant