Skip to content

Commit 3802e4a

Browse files
committed
Add script to fix relative links
1 parent 556be83 commit 3802e4a

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

astro.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default defineConfig({
3535
baseUrl:
3636
"https://github.com/splashkit/the-programmers-field-guide/edit/main/",
3737
},
38-
plugins: [starlightLinksValidator({ errorOnRelativeLinks: false })],
38+
plugins: [starlightLinksValidator({ errorOnRelativeLinks: true })],
3939
sidebar: [
4040
{
4141
label: "Part 0: First Steps",

fix-relative.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import os
2+
rootdir = '/workspaces/the-programmers-field-guide/book'
3+
4+
for subdir, dirs, files in os.walk(rootdir):
5+
for file in files:
6+
if file.endswith(".md") or file.endswith(".mdx"):
7+
fname = os.path.join(subdir, file)
8+
print(f"Processing {fname}")
9+
bookPath = subdir.removeprefix("/workspaces/the-programmers-field-guide/book").split('/')
10+
depth = len(bookPath)
11+
maxDepth = depth
12+
# Opening the file in read and write mode
13+
with open(fname,'r+') as f:
14+
15+
# Reading the file into content
16+
content = f.read()
17+
18+
# Determine replacements
19+
while depth > 0:
20+
search = f"]({'../' * depth}"
21+
depth -= 1
22+
replacement = f"](/book{'/'.join(bookPath[:(maxDepth - depth)])}/"
23+
print(f" - searching for {search} -> {replacement}")
24+
25+
# Replacing search text
26+
content = content.replace(search, replacement)
27+
28+
# Move to the start of the file
29+
f.seek(0)
30+
31+
# Replace file content
32+
f.write(content)
33+
34+
# Truncating the file size
35+
f.truncate()

0 commit comments

Comments
 (0)