-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPostFixupScript.ps1
82 lines (71 loc) · 3.2 KB
/
PostFixupScript.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
$i = 1
Function IsProbablyAnImage($string){
write-host "check $string to see if a link or image..."
if ( $string -like "*.png*" -or $string -like "*.jpg*" -or $string -like "*.gif*"){
write-host "this looks like an image, process"
return $true
}
else{
write-host "this looks like a URL, skipping"
return $false
}
}
if (-not((get-location).Path -like "*1redone.github.io")){
throw "need to run from blogs directory"
}
#to use, place a source year into the posts directory
$FixedPostsPath = gci .\_posts | ? Extension -In ('.md',".markdown")
$FixedPostsPathBaseName = $FixedPostsPath | % { $_.Name.SubString(11) }
$PostsToProcess = gci .\_posts -Recurse |
? Extension -In ('.md',".markdown") | % {
try{if ( $FixedPostsPathBaseName -notcontains $_.Name `
-and $FixedPostsPathBaseName -notcontains $_.Name.Substring(11)){
write-host "need to fix [$($_.FullName)])"
$_
}}
catch{"can't parse $($_.Name)"}
}
"found $($PostsToProcess.Count) files to process"
"======================================================"
$fixedPosts = get-item .\_posts
forEach ($file in $PostsToProcess){
Write-host -fore Yellow "Parsing file [$($file.Name)]"
#scrape file for images
$fileContent = gc $file.FullName
$imagesToFix = $fileContent | select-string -pattern "\]\(.*\)"
#if not found => Continue
if (0 -eq $imagesToFix.Count){"---no images"}
#calculate new image path
$fileDate = $fileContent | select-string "date:" |convertfrom-string
forEach ($img in $imagesToFix){
$elementToFix = $img.ToString().Split('(')[-1]
if (IsProbablyAnImage $elementToFix){
$fixedElement = "../assets/images/$($file.Directory.Parent.Name)/$($file.Directory.Name)/$elementToFix"
"---replacing $elementToFix with `n`t`t$fixedElement"
$fileContent = $fileContent.Replace($elementToFix,$fixedElement)
}
#image should look like
#![](../assets/images/2020/08/images/quicker-auto-mocking-in-c.png)
}
#update and add redirect
$dateLine =$fileContent | select-string "date:"
$fileContent = $fileContent.Replace('\[/caption\]','')
$fileContent = $fileContent.Replace('\[/code\]',"`n``````").Replace("\[code lang='powershell'\]","``````powershell`n").Replace('\[code lang="powershell" light="true"\]',"``````powershell`n").Replace("\[code lang=`"powershell`"\]","``````powershell`n")
$fileContent = $fileContent.Replace('\[code\]',"```````n")
$redirectPayload = "/$($fileDate.P2.Replace('"','').Replace('-','/'))/$($file.BaseName)"
$addRedirect = @"
$($dateLine.Line)
redirect_from : $redirectPayload
coverImage: ..\assets\images\foxPlaceHolder.webp
"@
$fileContent = $fileContent -replace $dateLine.Line, $addRedirect
$newName = "$($FixedPosts.FullName)\$($fileDate.P2.Replace('"',''))-$($file.Name)"
Write-host -ForegroundColor Cyan "---new name determined to be `n`t`t$newName"
#move to base dir
set-content -Path "$newName" -Value $fileContent -Force
#exit
read-host "$i of $($PostsToProcess.Count)"
#pause to check
$i++
}
#find this string ![remote-tightVNCUI](images/remote-tightvncui.png)