-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertTo-MarkdownFromHtml.ps1
More file actions
73 lines (57 loc) · 2.38 KB
/
ConvertTo-MarkdownFromHtml.ps1
File metadata and controls
73 lines (57 loc) · 2.38 KB
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
<#
.SYNOPSIS
Convert HTML content or a HTML file to Markdown using Pandoc
.PARAMETER File
The file path of the html file. Required when Content is not specified.
.PARAMETER Content
The HTML content to convert to Markdown. Required when File is not specified.
.PARAMETER OutFile
The resulting markdown file, if specified.
#>
function ConvertTo-MarkdownFromHtml {
param(
[Parameter(Mandatory, ParameterSetName = "FromFile")]
[ValidateScript({ Test-Path $_ -PathType Leaf })]
[string]$File,
[Parameter(Mandatory, ParameterSetName = "FromContent")]
[string]$Content,
[Parameter(ParameterSetName = "FromFile")]
[Parameter(Mandatory=$false, ParameterSetName = "FromContent")]
[string]$OutFile
)
# when FromContent is specified, write the content to a temporary file
if ($PSCmdlet.ParameterSetName -eq "FromContent") {
# If content is provided, create a temporary file
$tempFile = [System.IO.Path]::GetTempFileName() + ".html"
Set-Content -Path $tempFile -Value $Content
$File = $tempFile
}
# ensure that the file is an absolute path because pandoc.exe doesn't like powershell relative paths
$File = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($File)
# Use pandoc to convert the markdown to Html
$pandocArgs = "`"{0}`" " -f $File
$pandocArgs += "-f {0} " -f $BloggerSession.PandocHtmlFormat
$pandocArgs += "-t {0} " -f $BloggerSession.PandocMarkdownFormat
# add additional command-line arguments
if ($BloggerSession.PandocAdditionalArgs) {
Write-Verbose "Using additional args"
$pandocArgs += "{0} " -f $BloggerSession.PandocAdditionalArgs
}
if (!($OutFile)) {
$OutFile = Join-Path (Split-Path $File -Parent) ((Split-Path $File -LeafBase) + ".md")
Write-Verbose "Using OutFile: $OutFile"
}
# ensure that the file is an absolute path because pandoc.exe doesn't like powershell relative paths
$OutFile = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($OutFile)
$pandocArgs += "-o `"{0}`" " -f $OutFile
Write-Verbose ">> pandoc $($pandocArgs)"
Start-Process pandoc -ArgumentList $pandocArgs -NoNewWindow -Wait
$content = Get-Content $OutFile -Raw
# remove temporary files
if ($PSCmdlet.ParameterSetName -eq "FromContent") {
Remove-Item $File
} elseif (!$PSBoundParameters.ContainsKey("OutFile")) {
Remove-Item $OutFile
}
return $content
}