Adrián Bíro

90. Vibes Website With Powershell, Markdown to HTML

Universal utility as alternative to pandoc -f markdown -t html -o index.html index.md

#!/usr/bin/pwsh
#Requires -Version 6.1
Param(
    [system.io.fileinfo]$file
)
if ((-not $file ) -or -not (Test-Path -LiteralPath $file)) {
    Write-Host("Usage:`n`t{0} <file.md>" -f $MyInvocation.MyCommand.Name)
    exit 1  
}
(ConvertFrom-Markdown -Path $file).Html `
| Out-File -Encoding utf8 -LiteralPath (
    Join-Path -Path $file.DirectoryName -ChildPath ("{0}.html" -f $file.BaseName)
)

From this site, see scripts for deails.

#!/usr/bin/pwsh
#Requires -Version 6.1
function Get-Html {
    Param(
        [Parameter(Mandatory = $true,
            HelpMessage = "Path to file.md")] 
        [ValidateScript({
                if (-Not ($_ | Test-Path -PathType Leaf) ) {
                    throw "The Path argument must be a file. Folder paths are not allowed."
                }
                return $true
            })]
        [system.io.fileinfo]$file
    )
    [system.io.fileinfo]$html = (Join-Path -Path $file.DirectoryName -ChildPath ("{0}.html" -f $file.BaseName))
(ConvertFrom-Markdown -Path $file).Html -replace '(href=".*).md', '${1}.html' | Out-File -Encoding "utf8BOM" -LiteralPath $html 
    return $html
}
Get-ChildItem -Path (git rev-parse --show-cdup) -Recurse -Filter '*.md' -Exclude "README.md" | ForEach-Object {
    Write-Host("Generating {0}" -f (Get-Html -file $_.FullName))
}

Clean up Remove-Item -LiteralPath (Get-ChildItem -Recurse -Filter '*.html').FullName