Skip to content

Commit

Permalink
add redirector (:<
Browse files Browse the repository at this point in the history
  • Loading branch information
couleurm committed Dec 4, 2023
1 parent 8e5180c commit 138b78b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
9 changes: 8 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ jobs:
- uses: actions/setup-python@v4
with:
python-version: 3.x
- run: echo "cache_id=$(date --utc '+%V')" >> $GITHUB_ENV
- run: echo "cache_id=$(date --utc '+%V')" >> $GITHUB_ENV
- uses: actions/cache@v3
with:
key: mkdocs-material-${{ env.cache_id }}
path: .cache
restore-keys: |
mkdocs-material-
- name: you know material for mkdocs wasnt safe from my powershell
shell: pwsh
run: |
$env:YES_MAKE_TONS_OF_FOLDERS=1
./redirector.ps1
- run: pip install -r requirements.txt
- run: mkdocs gh-deploy --force -f mkdocs.github.yml
81 changes: 81 additions & 0 deletions redirector.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<#
This file makes a bunch of redirects for all pages on the site
It's meant to be run by GitHub Actions before actually building the site,
not on your computer (unless you'd enjoy dozens of folders in /docs/)
#>

Set-Location $PSScriptRoot

function New-HtmlRedirect ($url) {
return @"
<meta http-equiv="refresh" content="0"; url="$url"/>
<style>body {background-color: rgb(17, 17, 17);}</style>
"@
}


filter Get-RelativePath {
<#
It strips three things from the path you give it:
1. Remove ./docs/ and anything before that from it's path
2. Replace backslashes by forward slashes
3. If the first character is a slash, remove it
#>
$PSItem -replace [Regex]::Escape((Get-Item ./docs).FullName) -replace "\\", "/" -replace "^/"
}

$mdFiles = Get-ChildItem .\docs -Recurse -Include *.md

foreach ($filepath in $mdFiles.FullName) {

# this is what's gonna be the redirect, e.g ctt.cx/foo
$filename = [IO.Path]::GetFileNameWithoutExtension($filepath)

if (Test-Path ./docs/$filename) {
continue
}

# this is what it's gonna link to, e.g /video/obs/output#nvenc
$target = ($filepath | Get-RelativePath) -replace "\/index\.md$" -replace "\.md$"

if ($target -notlike "*/*") { continue } # no need to make redirects for non-nested

if ($env:YES_MAKE_TONS_OF_FOLDERS) { mkdir ./docs/$filename }

$Parameters = @{
Path = Join-Path (Resolve-Path ./docs) $filename/index.html
Value = New-HtmlRedirect -url "/$target"
}
if ($env:YES_MAKE_TONS_OF_FOLDERS) { Set-Content @Parameters }

Write-Host "ctt.cx/$filename -> $target"
}

$table = @{
sm = "video/smoothie"
obsvkcapture = "video/obs/linux/obs-vkcapture"
vkcapture = "video/obs/linux/obs-vkcapture"
wt = 'software/windows-terminal'
windowsterminal = 'software/windows-terminal'
}

foreach ($key in [string[]]$table.keys) {

if (Test-Path ./docs/$key) { continue }

if ($env:YES_MAKE_TONS_OF_FOLDERS) { mkdir ./docs/$key }

$Parameters = @{
Path = Join-Path (Resolve-Path ./docs) $key/index.html
Value = New-HtmlRedirect -url "/$($table.$key)"
}
if ($env:YES_MAKE_TONS_OF_FOLDERS) { Set-Content @Parameters }

Write-Host "ctt.cx/$key -> $($table.$key)"
}


0 comments on commit 138b78b

Please sign in to comment.