Last active
          April 7, 2022 14:14 
        
      - 
      
 - 
        
Save albertodebortoli/9310424 to your computer and use it in GitHub Desktop.  
    Generate Markdown TOC
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/usr/bin/env ruby | |
| File.open("your_file.md", 'r') do |f| | |
| f.each_line do |line| | |
| forbidden_words = ['Table of contents', 'define', 'pragma'] | |
| next if !line.start_with?("#") || forbidden_words.any? { |w| line =~ /#{w}/ } | |
| title = line.gsub("#", "").strip | |
| href = title.gsub(" ", "-").downcase | |
| puts " " * (line.count("#")-1) + "* [#{title}](\##{href})" | |
| end | |
| end | 
I updated and included your script in this repo.
https://github.com/shitana/md_TOC_generator
(I also put the link to your script and profile)
The script is not stripping invalid characters used as Headers. For example,
#### The field `b` means ("bug")
    
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
Thanks, very nice. Here is a version that takes the file name as an argument: