Markdown to create pages and table of contents?

后端 未结 30 2756
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-12 08:49

I started to use markdown to take notes.

I use marked to view my markdown notes and its beautiful.

But as my notes get longer I find it diff

30条回答
  •  忘掉有多难
    2020-12-12 09:40

    You could try this ruby script to generate the TOC from a markdown file.

     #!/usr/bin/env ruby
    
    require 'uri'
    
    fileName = ARGV[0]
    fileName = "README.md" if !fileName
    
    File.open(fileName, 'r') do |f|
      inside_code_snippet = false
      f.each_line do |line|
        forbidden_words = ['Table of contents', 'define', 'pragma']
        inside_code_snippet = !inside_code_snippet if line.start_with?('```')
        next if !line.start_with?("#") || forbidden_words.any? { |w| line =~ /#{w}/ } || inside_code_snippet
    
        title = line.gsub("#", "").strip
        href = URI::encode title.gsub(" ", "-").downcase
        puts "  " * (line.count("#")-1) + "* [#{title}](\##{href})"
      end
    end
    

提交回复
热议问题