Skip to content

Instantly share code, notes, and snippets.

@xiejiangzhi
Last active April 23, 2018 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiejiangzhi/9e08f7744179bb1ac569c441771faf6a to your computer and use it in GitHub Desktop.
Save xiejiangzhi/9e08f7744179bb1ac569c441771faf6a to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
def generate_dir_links(dir)
level_names = []
last_level = 0
Dir['*.md'].sort.each_with_object([]) do |file, md|
next if file == 'Home.md'
path = file.split('--')
name = path.last
path[0..-2].each_with_index do |lname, index|
if level_names[index] != lname
l_str = " " * index
md << l_str + "- #{lname.gsub(/-+/, ' ')}"
last_level = index
end
end
level_names = path[0..-2]
level_str = " " * (path.length - 1)
link = level_str + "- [#{name.gsub('.md', '').gsub(/-+/, ' ')}](#{file.gsub('.md', '')})"
md << link
end
end
File.write('Home.md', generate_dir_links('.').join("\n"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment