mirror of
https://gitlab.com/lvra/lvra.gitlab.io.git
synced 2024-12-25 15:20:26 +01:00
Merge branch 'feat/testLinksScript' into 'main'
feat: test if local links exist in ci See merge request LVRA/lvra.gitlab.io!5
This commit is contained in:
commit
0da381c40b
2 changed files with 41 additions and 3 deletions
|
@ -1,15 +1,20 @@
|
|||
image: registry.gitlab.com/pages/hugo/hugo_extended
|
||||
|
||||
variables:
|
||||
GIT_SUBMODULE_STRATEGY: recursive
|
||||
|
||||
test:
|
||||
test-links:
|
||||
image: python:latest
|
||||
script:
|
||||
- python3 ./test_links.py
|
||||
|
||||
test-build:
|
||||
image: registry.gitlab.com/pages/hugo/hugo_extended
|
||||
script:
|
||||
- hugo
|
||||
except:
|
||||
- main
|
||||
|
||||
pages:
|
||||
image: registry.gitlab.com/pages/hugo/hugo_extended
|
||||
script:
|
||||
- hugo
|
||||
artifacts:
|
||||
|
|
33
test_links.py
Normal file
33
test_links.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
#!/usr/bin/env python3
|
||||
from pathlib import Path
|
||||
import re
|
||||
from typing import List
|
||||
from os.path import exists
|
||||
|
||||
|
||||
def get_all_markdown_files():
|
||||
return Path("content").rglob("*.md")
|
||||
|
||||
|
||||
def find_links(f: str) -> List[str]:
|
||||
with open(f, "r") as fd:
|
||||
content = fd.read()
|
||||
return [
|
||||
link.lstrip("]").strip("()") for link in re.findall(r"\]\(\/[\w\/]+\)", content)
|
||||
]
|
||||
|
||||
|
||||
def verify_link_exists(link: str) -> bool:
|
||||
return exists(f"content{link}/_index.md") or exists(f'content{link.rstrip("/")}.md')
|
||||
|
||||
|
||||
md_files = get_all_markdown_files()
|
||||
|
||||
res = 0
|
||||
for f in md_files:
|
||||
for link in find_links(f):
|
||||
if not verify_link_exists(link):
|
||||
print(f"E: {f}: {link} does not exist")
|
||||
res = 1
|
||||
|
||||
exit(res)
|
Loading…
Reference in a new issue