Skip to content

Instantly share code, notes, and snippets.

@nightroman
Last active December 24, 2021 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nightroman/1303971 to your computer and use it in GitHub Desktop.
Save nightroman/1303971 to your computer and use it in GitHub Desktop.
Invoke-Build tasks for markdown files (using MarkdownDeep)
# Invoke-Build tasks for markdown files (using MarkdownDeep)
# <https://gist.github.com/1303971>
<#
Synopsis: Convert markdown files to HTML files (using MarkdownDeep).
Requires:
* MarkdownDeep.dll and its environment variable $env:MarkdownDeep
* NuGet install MarkdownDeep.NET
* https://github.com/toptensoftware/MarkdownDeep
Inputs: Markdown files in the current location.
Outputs: *.htm files with same base names as input.
#>
task ConvertMarkdown -Partial -Inputs {Get-Item *.md, *.markdown} -Outputs {process{[System.IO.Path]::ChangeExtension($_, 'htm')}} {
begin {
Add-Type -Path $env:MarkdownDeep
$Markdown = New-Object MarkdownDeep.Markdown
$Markdown.ExtraMode = $true
}
process {
$body = $Markdown.Transform([System.IO.File]::ReadAllText($_).Replace("`r`n", "`n"))
$writer = New-Object System.IO.StreamWriter $2, $false, ([System.Text.Encoding]::UTF8)
try {
$writer.Write("<html><title>$([System.IO.Path]::GetFileNameWithoutExtension($_))</title><body>`n")
$writer.Write($body)
$writer.Write("</body></html>`n")
}
finally {
$writer.Close()
}
}
}
<#
Synopsis: Remove HTML files presumably created by ConvertMarkdown.
All *.htm files having *.md or *.markdown pairs are removed.
#>
task RemoveMarkdownHtml {
foreach($_ in Get-Item *.md, *.markdown) {
$path = [System.IO.Path]::ChangeExtension($_.FullName, 'htm')
if (Test-Path -LiteralPath $path) { Remove-Item -LiteralPath $path }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment