From f08b9c1523e60c6954ed96ae8280ce152c402823 Mon Sep 17 00:00:00 2001 From: Niklas van Schrick Date: Tue, 26 Dec 2023 02:38:42 +0100 Subject: [PATCH] Add ability for review apps on documentation --- .gitlab-ci.yml | 12 ++++++++ build.sh | 3 +- scripts/local_link_suffixer.js | 52 ++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 scripts/local_link_suffixer.js diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b958a14..413355d 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -9,6 +9,18 @@ build: extends: - .build stage: build + variables: + C0_GLOBAL_BASE: https://code0-tech.gitlab.io/-/telescopium/-/jobs/$CI_JOB_ID/artifacts/dist + after_script: + - node scripts/local_link_suffixer.js + - | + echo -e "\e[0Ksection_start:`date +%s`:glpa_summary\r\e[0KHeader of the summary" + echo "Documentation preview available at ${C0_GLOBAL_BASE}/index.html" + echo -e "\e[0Ksection_end:`date +%s`:glpa_summary\r\e[0K" + artifacts: + paths: + - dist + expire_in: 7 days rules: - if: ($CI_COMMIT_BRANCH != "build-branch" || $C0_TRIGGER_REF != "refs/heads/main") && $CI_COMMIT_BRANCH != "glpa/main" diff --git a/build.sh b/build.sh index d42c52a..3d5ab60 100755 --- a/build.sh +++ b/build.sh @@ -13,7 +13,7 @@ build_project() { ln -s $(pwd)/projects/$project/docs/content projects/$project/content/docs ln $(pwd)/src/content/config.ts projects/$project/content/config.ts if [[ "$project" != "docs-landing-page" ]]; then - export BASE_URL=/$project/ + export BASE_URL=$C0_GLOBAL_BASE/$project/ export OUT_DIR=./dist/$project fi SRC_DIR=$(pwd)/projects/$project npm run build @@ -23,6 +23,7 @@ build_project() { rm -rf projects/$project/env.d.ts } +export BASE_URL=$C0_GLOBAL_BASE/ build_project "docs-landing-page" for project in $(ls projects | grep -v docs-landing-page); do diff --git a/scripts/local_link_suffixer.js b/scripts/local_link_suffixer.js new file mode 100644 index 0000000..5989805 --- /dev/null +++ b/scripts/local_link_suffixer.js @@ -0,0 +1,52 @@ +import path from "path"; +import fs from "fs"; + +const __dirname = path.resolve('dist'); + +function getFiles(targetDirectory = '.') { + const directoryPath = path.join(__dirname, targetDirectory); + return readDirectoryRecursive(directoryPath, `${__dirname}/${targetDirectory}`); +} + +function readDirectoryRecursive(directory, currentPath) { + const dirents = fs.readdirSync(directory, { withFileTypes: true }); + + const files = dirents.filter(dirent => dirent.isFile()).map(dirent => `${dirent.name}`); + const directories = dirents.filter(dirent => dirent.isDirectory()); + directories.forEach(dir => { + const recursedFiles = readDirectoryRecursive(path.join(currentPath, dir.name), `${currentPath}/${dir.name}`).map(filename => `${dir.name}/${filename}`); + files.push(...recursedFiles); + }); + return files; +} + +function readFile(file) { + return fs.readFileSync(path.resolve(__dirname, file), "utf-8"); +} + +function writeFile(file, content) { + fs.writeFileSync(path.resolve(__dirname, file), content, "utf-8"); +} + +function suffixLink(match, url) { + const urlObj = new URL(url, 'https://docs.code0.tech'); + if(urlObj.origin !== 'https://docs.code0.tech') { + // not a relative url + return match; + } + + if(urlObj.pathname === '/' && urlObj.hash !== '') { + // mostly a link to a hash on the same page + return match; + } + + return match.replace(url, url.split('#')[0] + 'index.html' + urlObj.hash); +} + +getFiles().forEach(file => { + const content = readFile(file); + + const replacedContent = content.replaceAll(/]*?href="([^"]+?)"[^>]*?>/g, suffixLink); + + writeFile(file, replacedContent); +});