-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix-markdown.mjs
113 lines (93 loc) · 3.1 KB
/
fix-markdown.mjs
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import fs from 'node:fs'
import path from 'node:path'
const documentationDirectory = path.resolve('./docs/docs')
// Rename README.md to index.md and fix links containing 'functions/'
function renameREADMEtoIndex(filePath) {
const newPath = path.join(path.dirname(filePath), 'index.md')
// Read file content and fix 'functions/' links
let content = fs.readFileSync(filePath, 'utf8')
content = content.replaceAll('functions/', './')
// Write the fixed content back
fs.writeFileSync(filePath, content, 'utf8')
// Rename the file
fs.renameSync(filePath, newPath)
console.log(`Renamed ${filePath} to ${newPath} and fixed links.`)
}
// Fix Markdown content
function fixMarkdown(filePath) {
const content = fs.readFileSync(filePath, 'utf8')
const lines = content.split('\n')
const newContent = lines.slice(6)
const newLines = []
let skipProcessing = false
for (let line of newContent) {
if (skipProcessing) {
newLines.push(line)
continue
}
if (line.startsWith('## Index') || line.startsWith('### Functions')) {
continue
}
else if (line.startsWith('## Modules')) {
line = line.replace('## Modules', '# Documentation')
}
else if (line.startsWith('# Function:')) {
line = line.replace('# Function:', '#')
}
else if (line.startsWith('## Example')) {
skipProcessing = true
newLines.push(line)
continue
}
line = line
.replaceAll(/README.md/g, '')
.replaceAll(/<([A-Z]+)>/gi, '<$1>')
newLines.push(line)
}
const fixedContent = newLines.join('\n')
fs.writeFileSync(filePath, fixedContent, 'utf8')
}
// Move files out of 'functions' folder and delete the folder
function moveOutFunctions(directory) {
const functionsDirectory = path.join(directory, 'functions')
if (
fs.existsSync(functionsDirectory)
&& fs.statSync(functionsDirectory).isDirectory()
) {
const files = fs.readdirSync(functionsDirectory)
// Move all files to the parent directory
for (const file of files) {
const oldPath = path.join(functionsDirectory, file)
const newPath = path.join(directory, file)
fs.renameSync(oldPath, newPath)
console.log(`Moved ${file} to ${directory}`)
}
// Delete the 'functions' folder if it's empty
fs.rmdirSync(functionsDirectory)
console.log(`Deleted empty folder: ${functionsDirectory}`)
}
}
// Traverse and process documents
function traverseDocuments(directory) {
const files = fs.readdirSync(directory)
for (const file of files) {
const fullPath = path.join(directory, file)
if (fs.statSync(fullPath).isDirectory()) {
traverseDocuments(fullPath)
// Handle 'functions' directory specifically
if (path.basename(fullPath) === 'functions') {
moveOutFunctions(path.dirname(fullPath))
}
}
else if (file.endsWith('.md')) {
console.log(`Processing file: ${file}`)
fixMarkdown(fullPath)
if (file === 'README.md') {
console.log(`Renaming file: ${file} to index.md`)
renameREADMEtoIndex(fullPath)
}
}
}
}
// Run the script
traverseDocuments(documentationDirectory)