forked from w3f/polkadot-wiki
-
Notifications
You must be signed in to change notification settings - Fork 1
/
converter.rb
executable file
·63 lines (49 loc) · 1.31 KB
/
converter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Converts docs from a hierarchical mkdocs format
# to an ID-marked format for docusaurus
# Also tags any internal links for manual updating
# by prefacing line with "TODO:"
def get_title(f)
s = f.index("# ")
e = f.index("\n", s)
f[(s+2)..(e-1)]
end
def tag_links(fs, f)
output = []
lines = fs.split("\n")
lines.each do |line|
if line.match(/\]\([^h#m].*\)/)
output << ("TODO:" + line)
else
output << line
end
end
output.join("\n")
end
# EXECUTION STARTS HERE
raise "Enter src and dest directories as args" if ARGV.count < 2
src_dir = ARGV[0]
dest_dir = ARGV[1]
files = `find #{src_dir} -name "*.md"`.split("\n")
# Modify and move all files
files.each do |f|
begin
# Get id - remove intro and .md, replace / with -
id = f[38..-4].gsub('/', '-')
fs = `cat #{f}`
title = get_title(fs)
# Generate header
header = "---\nid: #{id}\ntitle: #{title}\nsidebar_label: #{title}\n---\n\n"
# Replace all links in `fs` with new link format
fs = tag_links(fs, f)
# Concatenate header to file string
output = header + fs
# Write modified file to new location
output_file = "#{dest_dir}/#{id}.md"
File.new(output_file)
File.open(output_file, "w") do |file|
file.write(output)
end
rescue => e
puts "id: #{id}\nError: #{e}"
end
end