I have requirement to automate Azure wiki page with readme files via github actions. I have my readme files in several paths in github repo. "/community/widgets/document-details/readme.md" "/.pipeline/readme.md" "/examples/widgets/conference-session/readme" I need to pull all the paths, get the contents of the readme file and look for .png strings. png string are updated in format inside readme files.
Once I get all the png strings i need to replace it as below so that it is recognized in Azure wiki.
I am struggling to make this work using Get-Item, Get-content and for loop. Please help me with scripts
$Path = "/community/widgets/document-details/readme.md","/.pipeline/readme.md"
$OldText = "image.png"
$NewText = "/.attachments/$OldText"
ForEach ($item in $Path) {
$content = Get-Content $Path | Where-Object {$_ -like '.png'}
Write-Host "content list is "$content
foreach ($entry in $content) {
Write-Host $entry
$entry -replace $OldText, $NewText
}
Set-Content $Path
}
If I understand correctly you're looking to prepend /.attachments/
to any .png
Markdown link, if that's the case, you could use the pattern (?<=\]\()(?=.+\.png\))
and replace with /.attachments/
.
An example below:
$text = @'
text text text ![foo](foo.png)
![bar](bar.png) text text text
text ![baz](baz.png) text text
'@
$text -replace '(?<=\]\()(?=.+\.png\))', '/.attachments/'
# Outputs:
#
# text text text ![foo](/.attachments/foo.png)
# ![bar](/.attachments/bar.png) text text text
# text ![baz](/.attachments/baz.png) text text
Applied to your code:
$Path = '/community/widgets/document-details/readme.md', '/.pipeline/readme.md'
foreach ($item in $Path) {
(Get-Content $item -Raw) -replace '(?<=\]\()(?=.+\.png\))', '/.attachments/' |
Set-Content $item
}
See https://regex101.com/r/NbyUnq/3 for regex details.