Skip to content

Commit

Permalink
Merge pull request #187 from papermoonio/eshaben/add-internal-link-sc…
Browse files Browse the repository at this point in the history
…ript

add script to update internal links
  • Loading branch information
eshaben committed May 10, 2024
2 parents 69523e9 + d1f41e1 commit 7de3595
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions scripts/internal-links.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# ------------- 👋 Welcome to the script for calculating redirect mappings ---------------#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# The purpose of this script is to add backslashes to the end of all internal links. This #
# is required because if there isn't a backslash, a redirect occurs and adds one. #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# To use the script, ensure that the `moonbeam-docs` repo is nestled inside of the #
# `moonbeam-mkdocs` repo and on your branch with the latest changes. Then simply run #
# `python scripts/internal-links.py` in your terminal. There will be logs printed to the #
# terminal. When the script is complete, you can review all of the changes in the #
# `moonbeam-docs` repo. #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

import os
import re

# Function to recursively traverse directories
def traverse_dir(dir_path):
print("👀 Scanning and updating links...")
for root, dirs, files in os.walk(dir_path):
for file in files:
if file.endswith('.md'):
file_path = os.path.join(root, file)
modify_links(file_path)
print("✅ All links have been updated")

# Function to modify links in Markdown content
def modify_links(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()

# Regular expression to match links that start with "](/"
regex = r'\]\((?!/images/)(/[^#\s)]+)(?=[\s)]|$)'

# Replace links according to the specified format
modified_content = re.sub(regex, lambda match: match.group(0) + '/' if not match.group(0).endswith('/') else match.group(0), content)

# Write modified content back to the file
with open(file_path, 'w', encoding='utf-8') as f:
f.write(modified_content)

# Start traversing from the current directory
traverse_dir('moonbeam-docs')

0 comments on commit 7de3595

Please sign in to comment.