From 534d1b6c2a2abb396ce115bedec392517f5dac4c Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 30 Jul 2026 14:21:00 +0000 Subject: [PATCH] refactor: extract link matching and injection logic to improve readability Moves the link matching logic out of the main loop into a `find_matches` function, and the file injection logic into an `inject_links` function. This makes the `auto_link.py` script significantly easier to read and maintain. Co-authored-by: lsb11 <269203137+lsb11@users.noreply.github.com> --- scripts/auto_link.py | 48 ++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/scripts/auto_link.py b/scripts/auto_link.py index ced07ea..0b611a1 100644 --- a/scripts/auto_link.py +++ b/scripts/auto_link.py @@ -7,23 +7,7 @@ with open('src/data/apps.json', 'r') as f: apps = json.load(f) -# Build a mapping of app names to their programmatic hub links -app_links = {} -for app in apps: - app_links[app['name'].lower()] = { - 'name': app['name'], - 'link': f"/apps/{app['id']}/" - } - -blog_files = glob.glob('src/content/blog/*.md') -standalone_files = glob.glob('src/pages/*.astro') - -files_to_check = blog_files + [f for f in standalone_files if os.path.basename(f) not in ['index.astro', '404.astro', 'sitemap.astro', 'sitemap-page.astro']] - -for file_path in files_to_check: - with open(file_path, 'r') as f: - content = f.read() - +def find_matches(content, app_links): content_lower = content.lower() matches = [] @@ -33,9 +17,9 @@ if app_data['link'] not in content: matches.append(app_data) - if not matches: - continue - + return matches + +def inject_links(file_path, content, matches): # Inject links safely at the bottom of the file if file_path.endswith('.md'): # Just append to MD @@ -65,3 +49,27 @@ with open(file_path, 'w') as f: f.write(content) print(f"Injected links into {file_path}") + +# Build a mapping of app names to their programmatic hub links +app_links = {} +for app in apps: + app_links[app['name'].lower()] = { + 'name': app['name'], + 'link': f"/apps/{app['id']}/" + } + +blog_files = glob.glob('src/content/blog/*.md') +standalone_files = glob.glob('src/pages/*.astro') + +files_to_check = blog_files + [f for f in standalone_files if os.path.basename(f) not in ['index.astro', '404.astro', 'sitemap.astro', 'sitemap-page.astro']] + +for file_path in files_to_check: + with open(file_path, 'r') as f: + content = f.read() + + matches = find_matches(content, app_links) + + if not matches: + continue + + inject_links(file_path, content, matches)