|
1 | | -namespace HtmlToMarkdown |
2 | | - |
3 | | -open System.Management.Automation |
4 | | - |
5 | | -open ReverseMarkdown |
6 | | - |
7 | | -/// Converts HTML to Markdown. |
8 | | -[<Cmdlet(VerbsData.Convert, "HtmlToMarkdown")>] |
9 | | -[<OutputType(typeof<string>)>] |
10 | | -[<Alias("Convert-HtmlToCommonMark")>] |
11 | | -type ConvertHtmlToMarkdownCommand () = |
12 | | - inherit PSCmdlet () |
13 | | - |
14 | | - /// HTML to convert. |
15 | | - [<Parameter(Mandatory=true,ValueFromPipeline=true,ValueFromPipelineByPropertyName=true)>] |
16 | | - [<ValidateNotNullOrEmpty>] |
17 | | - [<Alias("InputObject","OuterHtml")>] |
18 | | - member val Html : string = null with get, set |
19 | | - |
20 | | - /// Option to set the default code block language for Github style markdown if class based language markers are not available. |
21 | | - [<Parameter>] |
22 | | - [<ValidateNotNullOrEmpty>] |
23 | | - [<Alias("CodeDefault")>] |
24 | | - member val DefaultCodeBlockLanguage : string = null with get, set |
25 | | - |
26 | | - /// Github style markdown for br, pre and table. |
27 | | - [<Parameter>] |
28 | | - [<Alias("GFM")>] |
29 | | - member val GithubFlavored : SwitchParameter = SwitchParameter false with get, set |
30 | | - |
31 | | - /// Sets the bullet character to use for unordered lists. |
32 | | - [<Parameter>] |
33 | | - [<Alias("Bullets")>] |
34 | | - member val ListBulletChar : char = '-' with get, set |
35 | | - |
36 | | - /// Remove comment tags with text. |
37 | | - [<Parameter>] |
38 | | - [<Alias("StripComments")>] |
39 | | - member val RemoveComments : SwitchParameter = SwitchParameter false with get, set |
40 | | - |
41 | | - /// Outputs link as auto-linking text (not an explicit link) when the text matches the link. |
42 | | - [<Parameter>] |
43 | | - [<Alias("AutoLink")>] |
44 | | - member val SmartHrefHandling : SwitchParameter = SwitchParameter false with get, set |
45 | | - |
46 | | - /// What to do with unknown tags: PassThrough includes it as is, Drop removes it, Bypass ignores it, and Raise throws an exception. |
47 | | - [<Parameter>] |
48 | | - [<Alias("UnknownElements")>] |
49 | | - member val UnknownTags : Config.UnknownTagsOption = Config.UnknownTagsOption.PassThrough with get, set |
50 | | - |
51 | | - /// Pass a list of tags to pass through as-is without any processing. |
52 | | - [<Parameter>] |
53 | | - [<ValidateNotNullOrEmpty>] |
54 | | - [<Alias("PassThroughElements","KeepTags","KeepElements")>] |
55 | | - member val PassThroughTags : string[] = Array.empty with get, set |
56 | | - |
57 | | - /// Specify which schemes (without trailing colon) are to be allowed for links and images. Empty string allows unknown schemes. |
58 | | - [<Parameter>] |
59 | | - [<Alias("AllowlistUriSchemes")>] |
60 | | - member val WhitelistUriSchemes : string[] = Array.empty with get, set |
61 | | - |
62 | | - /// What to do about tables without a header row. Default: first row will be used as header row. EmptyRow: an empty header row is created. |
63 | | - [<Parameter>] |
64 | | - [<Alias("TableHeaderDefault")>] |
65 | | - member val TableWithoutHeaderRowHandling : Config.TableWithoutHeaderRowHandlingOption = |
66 | | - Config.TableWithoutHeaderRowHandlingOption.Default with get, set |
67 | | - |
68 | | - member val Converter : Converter = null with get, set |
69 | | - |
70 | | - // optional: setup before pipeline input starts (e.g. Name is set, InputObject is not) |
71 | | - override x.BeginProcessing () = |
72 | | - base.BeginProcessing () |
73 | | - x.Converter |
74 | | - <- Converter |
75 | | - <| Config(DefaultCodeBlockLanguage = x.DefaultCodeBlockLanguage, |
76 | | - GithubFlavored = x.GithubFlavored, |
77 | | - ListBulletChar = x.ListBulletChar, |
78 | | - RemoveComments = x.RemoveComments, |
79 | | - SmartHrefHandling = x.SmartHrefHandling, |
80 | | - UnknownTags = x.UnknownTags, |
81 | | - PassThroughTags = x.PassThroughTags, |
82 | | - WhitelistUriSchemes = x.WhitelistUriSchemes, |
83 | | - TableWithoutHeaderRowHandling = x.TableWithoutHeaderRowHandling) |
84 | | - |
85 | | - // optional: handle each pipeline value (e.g. InputObject) |
86 | | - override x.ProcessRecord () = |
87 | | - base.ProcessRecord () |
88 | | - x.Converter.Convert(x.Html) |> x.WriteObject |
89 | | - |
90 | | - // optional: finish after all pipeline input |
91 | | - override x.EndProcessing () = |
92 | | - base.EndProcessing () |
| 1 | +namespace HtmlToMarkdown |
| 2 | + |
| 3 | +open System.Management.Automation |
| 4 | + |
| 5 | +open ReverseMarkdown |
| 6 | + |
| 7 | +/// Converts HTML to Markdown. |
| 8 | +[<Cmdlet(VerbsData.Convert, "HtmlToMarkdown")>] |
| 9 | +[<OutputType(typeof<string>)>] |
| 10 | +[<Alias("Convert-HtmlToCommonMark")>] |
| 11 | +type ConvertHtmlToMarkdownCommand () = |
| 12 | + inherit PSCmdlet () |
| 13 | + |
| 14 | + /// HTML to convert. |
| 15 | + [<Parameter(Mandatory=true,ValueFromPipeline=true,ValueFromPipelineByPropertyName=true)>] |
| 16 | + [<ValidateNotNullOrEmpty>] |
| 17 | + [<Alias("InputObject","OuterHtml")>] |
| 18 | + member val Html : string = null with get, set |
| 19 | + |
| 20 | + /// Option to set the default code block language for Github style markdown if class based language markers are not available. |
| 21 | + [<Parameter>] |
| 22 | + [<ValidateNotNullOrEmpty>] |
| 23 | + [<Alias("CodeDefault")>] |
| 24 | + member val DefaultCodeBlockLanguage : string = null with get, set |
| 25 | + |
| 26 | + /// Github style markdown for br, pre and table. |
| 27 | + [<Parameter>] |
| 28 | + [<Alias("GFM")>] |
| 29 | + member val GithubFlavored : SwitchParameter = SwitchParameter false with get, set |
| 30 | + |
| 31 | + /// Sets the bullet character to use for unordered lists. |
| 32 | + [<Parameter>] |
| 33 | + [<Alias("Bullets")>] |
| 34 | + member val ListBulletChar : char = '-' with get, set |
| 35 | + |
| 36 | + /// Remove comment tags with text. |
| 37 | + [<Parameter>] |
| 38 | + [<Alias("StripComments")>] |
| 39 | + member val RemoveComments : SwitchParameter = SwitchParameter false with get, set |
| 40 | + |
| 41 | + /// Outputs link as auto-linking text (not an explicit link) when the text matches the link. |
| 42 | + [<Parameter>] |
| 43 | + [<Alias("AutoLink")>] |
| 44 | + member val SmartHrefHandling : SwitchParameter = SwitchParameter false with get, set |
| 45 | + |
| 46 | + /// What to do with unknown tags: PassThrough includes it as is, Drop removes it, Bypass ignores it, and Raise throws an exception. |
| 47 | + [<Parameter>] |
| 48 | + [<Alias("UnknownElements")>] |
| 49 | + member val UnknownTags : Config.UnknownTagsOption = Config.UnknownTagsOption.PassThrough with get, set |
| 50 | + |
| 51 | + /// Pass a list of tags to pass through as-is without any processing. |
| 52 | + [<Parameter>] |
| 53 | + [<ValidateNotNullOrEmpty>] |
| 54 | + [<Alias("PassThroughElements","KeepTags","KeepElements")>] |
| 55 | + member val PassThroughTags : string[] = Array.empty with get, set |
| 56 | + |
| 57 | + /// Specify which schemes (without trailing colon) are to be allowed for links and images. Empty string allows unknown schemes. |
| 58 | + [<Parameter>] |
| 59 | + [<Alias("AllowlistUriSchemes")>] |
| 60 | + member val WhitelistUriSchemes : string[] = Array.empty with get, set |
| 61 | + |
| 62 | + /// What to do about tables without a header row. Default: first row will be used as header row. EmptyRow: an empty header row is created. |
| 63 | + [<Parameter>] |
| 64 | + [<Alias("TableHeaderDefault")>] |
| 65 | + member val TableWithoutHeaderRowHandling : Config.TableWithoutHeaderRowHandlingOption = |
| 66 | + Config.TableWithoutHeaderRowHandlingOption.Default with get, set |
| 67 | + |
| 68 | + member val Converter : Converter = null with get, set |
| 69 | + |
| 70 | + // optional: setup before pipeline input starts (e.g. Name is set, InputObject is not) |
| 71 | + override x.BeginProcessing () = |
| 72 | + base.BeginProcessing () |
| 73 | + let cfg = Config(DefaultCodeBlockLanguage = x.DefaultCodeBlockLanguage, |
| 74 | + GithubFlavored = x.GithubFlavored, |
| 75 | + ListBulletChar = x.ListBulletChar, |
| 76 | + RemoveComments = x.RemoveComments, |
| 77 | + SmartHrefHandling = x.SmartHrefHandling, |
| 78 | + UnknownTags = x.UnknownTags, |
| 79 | + TableWithoutHeaderRowHandling = x.TableWithoutHeaderRowHandling) |
| 80 | + Array.iter (cfg.PassThroughTags.Add >> ignore) x.PassThroughTags |
| 81 | + Array.iter (cfg.WhitelistUriSchemes.Add >> ignore) x.WhitelistUriSchemes |
| 82 | + x.Converter <- Converter <| cfg |
| 83 | + |
| 84 | + // optional: handle each pipeline value (e.g. InputObject) |
| 85 | + override x.ProcessRecord () = |
| 86 | + base.ProcessRecord () |
| 87 | + x.Converter.Convert(x.Html) |> x.WriteObject |
| 88 | + |
| 89 | + // optional: finish after all pipeline input |
| 90 | + override x.EndProcessing () = |
| 91 | + base.EndProcessing () |
0 commit comments