Unexpected Changes to Image Locations with Hugo
Hello!
I’ve been using Hugo asa static website builder for over 18 months now and I’m really happy with it: I have backups of the website thank to using git, I cna create posts offline, it’s free, GitLab has great hosting capabilities, I can make changes to the look of the site and review them on my desktop prior to publishing them, and despite my limited knowledge of Go and general skills required to build a website I’ve been able to maintain the look and feel with any changes that occur as Hugo continues to be developed upon.
However recently there was one change that affected all of my posts. I was using relative paths to display the images in posts, and that feature seemed to have stopped working a couple of versions ago. Thanks fully i was able to identify the change required, and with a bit of munging the posts I was able to identify and update all of the affected blog posts using the PowerShell below.
Get-ChildItem "C:\Users\Richie\source\repos\source.bzzztio\content\post" -Recurse -Filter *.md |
Foreach-Object {
$content = Get-Content $_.FullName
ForEach ($line in $content) {
$ignore = $false
if ($line -match "!\[" ) {
try {
$after = $line.Replace('..\\..\\..', '').Replace('\\','/')
Write-Host $line
Write-Host $after
$content = $content.replace($line, $after)
}
catch {
"oh dear"
$Ignore = $true
}
}
}
if ($ignore -eq $false) {
Write-Host $_.FullName
set-content $_.FullName $content -Force
}
}
Then it was a case of running hugo server
locally to verify that the change worked, then commit and push to GitLab and let the CI/CD pipeline publish all the changes. Simples!