From 10227b959fea9db95e75169edaced82cc46ec847 Mon Sep 17 00:00:00 2001 From: GabMus Date: Fri, 22 Dec 2023 09:16:24 +0000 Subject: [PATCH] feat: test if local links exist in ci --- .gitlab-ci.yml | 11 ++++++++--- test_links.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 test_links.py diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 04772ba..fe610f5 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -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: diff --git a/test_links.py b/test_links.py new file mode 100644 index 0000000..07e0a9a --- /dev/null +++ b/test_links.py @@ -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)