-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertTo-Markdown.ps1
More file actions
542 lines (454 loc) · 17.5 KB
/
Copy pathConvertTo-Markdown.ps1
File metadata and controls
542 lines (454 loc) · 17.5 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
function ConvertTo-Markdown
{
<#
.SYNOPSIS
Converts a URL or local file path to Markdown using Pandoc.
.DESCRIPTION
Wraps the pandoc CLI to convert an HTTP(S) URL or local file path to Markdown.
Conversion output is always written to a Markdown file.
When -OutputPath is omitted, a file path is auto-generated from the URL
or local input filename (for example, https://example.com => example-com.md).
Use -OutputPath to write the conversion result to a specific file path.
Dependencies:
- Get-EncodingFromName: Resolves profile encoding names to .NET encoding instances.
- Get-FileEncoding: Detects current output file encoding before optional conversion.
Both dependencies are automatically loaded when needed.
.PARAMETER InputObject
The source to convert. Accepts:
- HTTP/HTTPS URLs
- Local file paths
Supports pipeline input and accepts multiple values.
.PARAMETER To
Pandoc Markdown output format. Defaults to GitHub-flavored Markdown ('gfm').
Accepted values: commonmark, commonmark_x, gfm, markdown, markdown_github,
markdown_mmd, markdown_phpextra, markdown_strict, markua.
Supports extension modifiers in Pandoc format syntax:
FORMAT[+EXTENSION|-EXTENSION]...
Example: gfm+task_lists+pipe_tables-smart
To view all possible extensions, run:
pandoc --list-extensions markdown
.PARAMETER From
Pandoc input format (for example: html, docx, rst).
Defaults to html.
.PARAMETER OutputPath
Optional explicit destination file path for Markdown output.
When specified, only a single InputObject value is supported.
When omitted, an output path is auto-generated per input item.
.PARAMETER PandocArgs
Additional arguments to append to the Pandoc command.
.PARAMETER Encoding
Specifies the target output file encoding. When set to 'Auto' (default),
the encoding produced by Pandoc is preserved.
Valid values:
- Auto: Preserve Pandoc output encoding (default)
- UTF8: UTF-8 without BOM
- UTF8BOM: UTF-8 with BOM
- UTF16LE: UTF-16 Little Endian with BOM
- UTF16BE: UTF-16 Big Endian with BOM
- UTF32: UTF-32 Little Endian with BOM
- UTF32BE: UTF-32 Big Endian with BOM
- ASCII: 7-bit ASCII encoding
- ANSI: System default ANSI encoding (code page dependent)
.PARAMETER PassThru
Returns the resolved output path after successful conversion:
- Explicit -OutputPath path
- Auto-generated path when -OutputPath is omitted
.EXAMPLE
PS > ConvertTo-Markdown -InputObject 'https://example.com'
Converts a web page to GitHub-flavored Markdown and writes it to ./example-com.md.
.EXAMPLE
PS > ConvertTo-Markdown -InputObject './report.html' -OutputPath './report.md'
Converts a local HTML file to Markdown and writes it to report.md.
.EXAMPLE
PS > ConvertTo-Markdown -InputObject './report.docx' -From docx -To gfm
Converts a Word document to GitHub-flavored Markdown.
.EXAMPLE
PS > ConvertTo-Markdown -InputObject './report.html' -To 'gfm+task_lists+pipe_tables-smart'
Converts HTML to GFM with task lists and pipe tables enabled, and smart typography disabled.
.EXAMPLE
PS > 'https://example.com', './notes.html' | ConvertTo-Markdown
Converts multiple inputs from the pipeline and writes one auto-named
markdown file per input.
.EXAMPLE
PS > ConvertTo-Markdown -InputObject './report.html' -Encoding 'UTF8BOM'
Converts HTML to Markdown and rewrites the output file as UTF-8 with BOM.
.OUTPUTS
System.String
Output file path when using -PassThru.
.NOTES
Requires Pandoc to be installed and available in PATH.
Author: Jon LaBelle
License: MIT
Source: https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/Utilities/ConvertTo-Markdown.ps1
.LINK
https://pandoc.org/
.LINK
https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/Utilities/ConvertTo-Markdown.ps1
#>
[CmdletBinding(SupportsShouldProcess)]
[OutputType([String])]
param(
[Parameter(Mandatory, ValueFromPipeline, Position = 0)]
[Alias('Path', 'Url', 'Uri')]
[ValidateNotNullOrEmpty()]
[String[]]$InputObject,
[Parameter()]
[ValidateNotNullOrEmpty()]
[ValidateScript({
$value = [String]$_
$pattern = '^(commonmark|commonmark_x|gfm|markdown|markdown_github|markdown_mmd|markdown_phpextra|markdown_strict|markua)([+-][a-z][a-z0-9_]*)*$'
if ($value -notmatch $pattern)
{
throw 'Value must be a Pandoc markdown writer (commonmark, commonmark_x, gfm, markdown, markdown_github, markdown_mmd, markdown_phpextra, markdown_strict, markua) optionally followed by extension modifiers such as +footnotes or -emoji.'
}
$true
})]
[String]$To = 'gfm',
[Parameter()]
[ValidateNotNullOrEmpty()]
[ValidateSet(
'asciidoc',
'biblatex',
'bibtex',
'bits',
'commonmark',
'commonmark_x',
'creole',
'csljson',
'csv',
'djot',
'docbook',
'docx',
'dokuwiki',
'endnotexml',
'epub',
'fb2',
'gfm',
'haddock',
'html',
'ipynb',
'jats',
'jira',
'json',
'latex',
'man',
'markdown',
'markdown_github',
'markdown_mmd',
'markdown_phpextra',
'markdown_strict',
'mdoc',
'mediawiki',
'muse',
'native',
'odt',
'opml',
'org',
'pod',
'pptx',
'ris',
'rst',
'rtf',
't2t',
'textile',
'tikiwiki',
'tsv',
'twiki',
'typst',
'vimwiki',
'xlsx',
'xml'
)]
[String]$From = 'html',
[Parameter()]
[ValidateNotNullOrEmpty()]
[String]$OutputPath,
[Parameter()]
[String[]]$PandocArgs,
[Parameter()]
[ValidateSet('Auto', 'UTF8', 'UTF8BOM', 'UTF16LE', 'UTF16BE', 'UTF32', 'UTF32BE', 'ASCII', 'ANSI')]
[String]$Encoding = 'Auto',
[Parameter()]
[Switch]$PassThru
)
begin
{
# Helper function to load dependencies on demand
function Import-DependencyIfNeeded
{
param(
[Parameter(Mandatory)]
[String]$FunctionName,
[Parameter(Mandatory)]
[String]$RelativePath
)
if (-not (Get-Command -Name $FunctionName -ErrorAction Ignore))
{
Write-Verbose "$FunctionName is required - attempting to load it"
# Resolve path from current script location
$dependencyPath = Join-Path -Path $PSScriptRoot -ChildPath $RelativePath
$dependencyPath = [System.IO.Path]::GetFullPath($dependencyPath)
if (Test-Path -Path $dependencyPath -PathType Leaf)
{
return $dependencyPath
}
else
{
throw "Required function '$FunctionName' could not be found. Expected location: $dependencyPath"
}
}
else
{
Write-Verbose "$FunctionName is already loaded"
return $null
}
}
$dependencyPath = Import-DependencyIfNeeded -FunctionName 'Get-EncodingFromName' -RelativePath 'Get-EncodingFromName.ps1'
if ($dependencyPath)
{
try
{
. $dependencyPath
Write-Verbose "Loaded Get-EncodingFromName from: $dependencyPath"
}
catch
{
throw "Failed to load required dependency 'Get-EncodingFromName' from '$dependencyPath': $($_.Exception.Message)"
}
}
$dependencyPath = Import-DependencyIfNeeded -FunctionName 'Get-FileEncoding' -RelativePath 'Get-FileEncoding.ps1'
if ($dependencyPath)
{
try
{
. $dependencyPath
Write-Verbose "Loaded Get-FileEncoding from: $dependencyPath"
}
catch
{
throw "Failed to load required dependency 'Get-FileEncoding' from '$dependencyPath': $($_.Exception.Message)"
}
}
function ConvertTo-FileSlug
{
param(
[Parameter()]
[AllowNull()]
[String]$Value
)
if ([String]::IsNullOrWhiteSpace($Value))
{
return $null
}
$decoded = [Uri]::UnescapeDataString($Value)
$slug = $decoded.ToLowerInvariant() -replace '[^a-z0-9]+', '-'
$slug = $slug.Trim('-')
if ([String]::IsNullOrWhiteSpace($slug))
{
return $null
}
return $slug
}
function Get-MarkdownOutputPathFromUri
{
param(
[Parameter(Mandatory)]
[Uri]$Uri
)
$hostSlug = ConvertTo-FileSlug -Value $Uri.DnsSafeHost
if (-not $hostSlug)
{
$hostSlug = 'web-page'
}
$pathSegmentSlugs = @()
$pathSegments = $Uri.AbsolutePath.Split('/', [System.StringSplitOptions]::RemoveEmptyEntries)
foreach ($segment in $pathSegments)
{
$segmentWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($segment)
$segmentSlug = ConvertTo-FileSlug -Value $segmentWithoutExtension
if ($segmentSlug)
{
$pathSegmentSlugs += $segmentSlug
}
}
$baseName = if ($pathSegmentSlugs.Count -gt 0)
{
@($hostSlug) + $pathSegmentSlugs -join '-'
}
else
{
$hostSlug
}
if ($Uri.Query)
{
$querySlug = ConvertTo-FileSlug -Value $Uri.Query.TrimStart('?')
if ($querySlug)
{
$baseName = "$baseName-$querySlug"
}
}
$fileName = "$baseName.md"
return Join-Path -Path (Get-Location).Path -ChildPath $fileName
}
function Get-MarkdownOutputPathFromLocalSource
{
param(
[Parameter(Mandatory)]
[String]$Path
)
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($Path)
$baseSlug = ConvertTo-FileSlug -Value $baseName
if (-not $baseSlug)
{
$baseSlug = 'document'
}
$fileName = "$baseSlug.md"
return Join-Path -Path (Get-Location).Path -ChildPath $fileName
}
$pandocCommand = Get-Command -Name 'pandoc' -ErrorAction SilentlyContinue
if (-not $pandocCommand)
{
throw 'Pandoc is not installed or not available in PATH. Please install Pandoc and try again.'
}
Write-Verbose "Pandoc found at: $($pandocCommand.Source)"
$pandocRequestUserAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36'
$processedCount = 0
$hasExplicitOutputPath = $PSBoundParameters.ContainsKey('OutputPath')
$resolvedOutputPath = $null
if ($hasExplicitOutputPath)
{
$resolvedOutputPath = $PSCmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath($OutputPath)
$outputDirectory = Split-Path -Path $resolvedOutputPath -Parent
if ($outputDirectory -and -not (Test-Path -LiteralPath $outputDirectory -PathType Container))
{
$null = New-Item -Path $outputDirectory -ItemType Directory -Force
}
Write-Verbose "Markdown output path resolved to: $resolvedOutputPath"
}
}
process
{
foreach ($item in $InputObject)
{
$processedCount++
if ($hasExplicitOutputPath -and $processedCount -gt 1)
{
throw 'When -OutputPath is specified, only one InputObject value is supported.'
}
$parsedUri = $null
$isWebUri = [Uri]::TryCreate($item, [UriKind]::Absolute, [ref]$parsedUri) -and
$parsedUri.Scheme -in @('http', 'https')
if ($isWebUri)
{
$pandocInput = $parsedUri.AbsoluteUri
}
else
{
if (-not (Test-Path -LiteralPath $item -PathType Leaf))
{
throw "Input path does not exist or is not a file: $item"
}
$pandocInput = $PSCmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath($item)
}
$autoOutputPath = $null
if (-not $hasExplicitOutputPath)
{
if ($isWebUri)
{
$autoOutputPath = Get-MarkdownOutputPathFromUri -Uri $parsedUri
}
else
{
$autoOutputPath = Get-MarkdownOutputPathFromLocalSource -Path $pandocInput
}
Write-Verbose "Auto-generated markdown output path: $autoOutputPath"
}
$effectiveOutputPath = if ($hasExplicitOutputPath) { $resolvedOutputPath } else { $autoOutputPath }
$pandocCallArgs = @()
if ($From)
{
$pandocCallArgs += @('--from', $From)
}
$pandocCallArgs += @('--to', $To)
if ($PandocArgs)
{
$pandocCallArgs += $PandocArgs
}
$pandocCallArgs += "--request-header=User-Agent: $pandocRequestUserAgent"
if ($effectiveOutputPath)
{
$pandocCallArgs += @('-o', $effectiveOutputPath)
}
$pandocCallArgs += $pandocInput
$target = $effectiveOutputPath
if (-not $PSCmdlet.ShouldProcess($item, "Convert to Markdown ($target)"))
{
continue
}
Write-Verbose "Executing: $($pandocCommand.Name) $($pandocCallArgs -join ' ')"
$global:LASTEXITCODE = 0
$null = & $pandocCommand.Name @pandocCallArgs
$exitCode = $LASTEXITCODE
if ($exitCode -ne 0)
{
throw "Pandoc failed for '$item' with exit code $exitCode."
}
if ($Encoding -ne 'Auto' -and $effectiveOutputPath)
{
$targetEncoding = Get-EncodingFromName -EncodingName $Encoding
if ($null -eq $targetEncoding)
{
throw "Failed to resolve target encoding '$Encoding'."
}
$sourceEncoding = Get-FileEncoding -FilePath $effectiveOutputPath
$sourceType = $sourceEncoding.ToString()
$targetType = $targetEncoding.ToString()
$sourceBomLength = $sourceEncoding.GetPreamble().Length
$targetBomLength = $targetEncoding.GetPreamble().Length
$encodingMatches = ($sourceType -eq $targetType) -and ($sourceBomLength -eq $targetBomLength)
if (-not $encodingMatches)
{
$reader = New-Object System.IO.StreamReader($effectiveOutputPath, $sourceEncoding, $true)
try
{
$markdownContent = $reader.ReadToEnd()
}
finally
{
$reader.Close()
}
$preamble = $targetEncoding.GetPreamble()
$contentBytes = $targetEncoding.GetBytes($markdownContent)
if ($preamble.Length -gt 0)
{
$newBytes = New-Object byte[] ($preamble.Length + $contentBytes.Length)
[Array]::Copy($preamble, 0, $newBytes, 0, $preamble.Length)
[Array]::Copy($contentBytes, 0, $newBytes, $preamble.Length, $contentBytes.Length)
}
else
{
$newBytes = $contentBytes
}
[System.IO.File]::WriteAllBytes($effectiveOutputPath, $newBytes)
}
}
if ($PassThru)
{
Write-Output $effectiveOutputPath
}
}
}
}
# Create 'url2markdown' alias only if it doesn't already exist
if (-not (Get-Command -Name 'url2markdown' -ErrorAction SilentlyContinue))
{
try
{
Write-Verbose "Creating 'url2markdown' alias for ConvertTo-Markdown"
Set-Alias -Name 'url2markdown' -Value 'ConvertTo-Markdown' -Force -ErrorAction Stop
}
catch
{
Write-Warning "ConvertTo-Markdown: Could not create 'url2markdown' alias: $($_.Exception.Message)"
}
}