forked from couleur-tweak-tips/CTT
-
Notifications
You must be signed in to change notification settings - Fork 1
/
redirector.ps1
104 lines (77 loc) · 2.8 KB
/
redirector.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<#
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 @"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="refresh" content="0;url=$url">
<style>
body {background-color: #1e2129;}
</style>
<script>
window.onload = function() {
var currentFragment = window.location.hash;
var newUrl = "$url" + currentFragment;
window.location.replace(newUrl);
}
</script>
</head>
<body>
</body>
</html>
"@
}
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)
# this is what it's gonna link to, e.g /video/obs/output#nvenc
$target = ($filepath | Get-RelativePath) -replace "\/index\.md$" -replace "\.md$"
if ($filename -eq "index"){
$filename = $target | Split-Path -Leaf
}
if (Test-Path ./docs/$filename) {
continue
}
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)"
}