-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertTo-MarkdownFromHtml.ps1
More file actions
107 lines (84 loc) · 3.73 KB
/
ConvertTo-MarkdownFromHtml.ps1
File metadata and controls
107 lines (84 loc) · 3.73 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<#
.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.
.PARAMETER PassThru
Return markdown content as output, in addition to writing it to disk.
.EXAMPLE
# Convert HTML content to Markdown and save to a file
ConvertTo-MarkdownFromHtml -Content "<h1>Hello World</h1>" -OutFile "C:\path\to\file.md"
.EXAMPLE
# Convert a HTML file to Markdown and save to a file
ConvertTo-MarkdownFromHtml -File "C:\path\to\file.html" -OutFile "C:\path\to\file.md"
.EXAMPLE
# Convert a HTML file to Markdown and return the content
$content = ConvertTo-MarkdownFromHtml -File "C:\path\to\file.html"
.EXAMPLE
# Convert HTML content to Markdown and return the content
$content = ConvertTo-MarkdownFromHtml -Content "<h1>Hello World</h1>"
.EXAMPLE
# Convert HTML content to Markdown and save to a file, returning the content
$content = ConvertTo-MarkdownFromHtml -Content "<h1>Hello World</h1>" -OutFile "C:\path\to\file.md" -PassThru
.EXAMPLE
# Convert a HTML file to Markdown and save to a file, returning the content
$content = ConvertTo-MarkdownFromHtml -File "C:\path\to\file.html" -OutFile "C:\path\to\file.md" -PassThru
#>
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(ParameterSetName = "FromContent")]
[string]$OutFile,
[Parameter(ParameterSetName = "FromFile")]
[Parameter(ParameterSetName = "FromContent")]
[switch]$PassThru
)
# 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 output if not persisting to disk or PassThru is specified
if (!$PSBoundParameters.ContainsKey("OutFile") -or ($PassThru.IsPresent -and $PassThru)) {
Write-Verbose "Returning content"
return $content
}
}