showdownjs / showdown

A bidirectional Markdown to HTML to Markdown converter written in Javascript
http://www.showdownjs.com/
MIT License
14.19k stars 1.56k forks source link

Is it possibile to get the Markdown title? #936

Open flaviotordini opened 2 years ago

flaviotordini commented 2 years ago

I'm using showdown to render Markdown but I also would like to access the standard Markdown title (#) to generate some custom table of contents I need.

I read about the "metadata" stuff but I don't want to force authors to use a redundant tag. The title is already there.

Is there a way? If not will you consider adding the feature?

MarketingPip commented 2 years ago

@flaviotordini - I am not a contributor etc on this project. But hoping I can maybe provide you some insight for your problem. Correct me if wrong are you just looking to place a TOC in your markdown content already? Or build it in another place...?

If you are looking to build it in another place - can I suggest using an existing Table Of Contents generator, modifying it for your own needs to produce the output file somewhere else with the added Table Of Contents?

This is not what you need - but you could use the NPM package it uses to do this, again the script might need modifications assuming you are trying to get H1 values from what I am reading (if you need any help etc - feel free to make a comment here & I will try my best to guide you through)

MarketingPipeline/TOC-Generator-Action: A Github Action to generate a table of contents in your README

Hope this helps.

flaviotordini commented 2 years ago

Thanks but I want to generate an external toc made of the titles (and paths) of many different Markdown documents.

MarketingPip commented 2 years ago

Thanks but I want to generate an external toc made of the titles (and paths) of many different Markdown documents.

@flaviotordini I figured this is what you are wanting to do. I will guide you through the steps to do this.

You will need this package or something similar (this is what I have setup in the action above)

markdown-toc-gen - npm

Insert in your Markdown Documents (the files you want to create a Table of Contents in) the following wherever you want the table of contents placed. There are options to place automatically at the start of each file keep in mind.

<-- toc --> <-- tocstop -->

then after installing markdown-toc-gen use the following command with [files] being your Markdown file paths.

markdown-toc-gen update [files..]

for more documentation on this again - read the following here. markdown-toc-gen - npm

Tho you mentioned you want to create something like the following example.

   [File Name](my_file_path/folder/File_Name.md)
   [File Name 2](my_file_path/folder/File_Name_2.md)

Assuming this is the case you will need to do some customization / scripting of your own to grab all file names & paths, place them inside a single markdown document with a separate header etc for each new line / file added ## [YOUR FILE NAME](YOUR FILE PATH), then add your <-- toc --> tags inside the document & generate the TOC, then remove all content after the generated TOCS / <-- toc --> tags.

Hope this helps & being this is kind of off topic from showdown I would suggest closing this issue. Tho I will be willing to help you if you have any questions - just prefer not spamming / using showdown issues section to discuss something somewhat off topic.

Again, hope this helps. :+1:

ps; in terms of getting the title inside the MD document, look for the first case of a H1 heading etc.

MarketingPip commented 2 years ago

@flaviotordini - I did creep your profile too & seen you do mostly work in C++.

If you are not experienced or confident with doing this, I can possibility make you a Python based script that does exactly what you need. Tho I can not guarantee an exact timeline on this.

MarketingPip commented 2 years ago

@flaviotordini - was this not what you were needing..?