-
Notifications
You must be signed in to change notification settings - Fork 302
/
Copy pathcreate-api-docs.sh
executable file
·63 lines (45 loc) · 1.98 KB
/
create-api-docs.sh
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
#!/bin/bash
# ---------------------
# This script grabs the Lotus JSON-RPC API methods from github.com/filecoin-project/lotus and splits it into individual files. It also runs Prettier against each file, and reduces the header levels.
# To run this script, install Prettier with `npm install -g prettier` and execute the file with ./create-api-docs.sh
# ---------------------
# Download the Markdown file.
curl -o api.md https://raw.githubusercontent.com/filecoin-project/lotus/master/documentation/en/api-v1-unstable-methods.md
# Define the output directory.
output_dir="./"
# Create the output directory if it doesn't exist.
mkdir -p "$output_dir"
# Read the Markdown file line by line.
while IFS= read -r line; do
# Check if the line starts with "## ".
if [[ $line == "## "* ]]; then
# Extract the header text.
header=$(echo "$line" | sed 's/^## //')
# Remove any special characters from the header text to create a lowercase filename.
filename=$(echo "$header" | tr -cd '[:alnum:]\n' | tr '[:upper:]' '[:lower:]')
# Create a new file for each header.
if [[ $filename != "tableofcontents" ]]; then
output_file="$output_dir/$filename.md"
echo "# $header" > "$output_file"
fi
elif [[ $line == "### "* ]]; then
# Modify the line to start with "## ".
modified_line=$(echo "$line" | sed 's/^### /## /')
# Append the modified line to the current output file.
echo "$modified_line" >> "$output_file"
elif [[ $line == "#### "* ]]; then
# Modify the line to start with "### ".
modified_line=$(echo "$line" | sed 's/^#### /### /')
# Append the modified line to the current output file.
echo "$modified_line" >> "$output_file"
elif [[ -n "$output_file" ]]; then
# Append the line to the current output file.
echo "$line" >> "$output_file"
fi
done < api.md
# Remove the temporary api.md and .md files.
rm api.md .md
# Run prettier on each generated file.
for file in "$output_dir"/*.md; do
npx prettier --write "$file"
done