PSMarkdig.psm1
function Get-MdDocument { [cmdletBinding(DefaultParameterSetName='byString')] [OutputType([Markdig.Syntax.MarkdownDocument])] param ( [Parameter(Mandatory,ParameterSetName='byString')] [string]$String, [Parameter(Mandatory,ParameterSetName='byFile')] [System.IO.FileInfo]$FilePath, [Parameter()] [string[]]$Extension ) process { #Get markdown string switch ($PSCmdlet.ParameterSetName) { 'byString' { $mdString = $String break } 'byFile' { $mdString = Get-Content -Path $FilePath -Raw break } } #Parse markdown $pipeline = [Markdig.MarkdownPipelineBuilder]::new() if ($PSBoundParameters.ContainsKey('Extension')) { $pipeline = [Markdig.MarkDownExtensions]::Configure($pipeline,($Extension -join '+')) } $result = [Markdig.Parsers.MarkdownParser]::Parse($mdString,$pipeline.Build()) $PSCmdlet.WriteObject($result,$false) } } function Get-MdElement { [cmdletBinding()] param ( [Parameter(Mandatory)] [Markdig.Syntax.MarkdownDocument]$Document, [Parameter(Mandatory)] [string]$TypeName ) process { #Check Type $type = $TypeName -as [Type] if (-not $type) { throw "Type: '$TypeName' not found" } $mdExtensionsType = [Markdig.Syntax.MarkdownObjectExtensions] $methodDescendants = [Markdig.Syntax.MarkdownObjectExtensions].GetMethod('Descendants',1,[Markdig.Syntax.MarkdownObject]) $method = $methodDescendants.MakeGenericMethod($Type) $method.Invoke($mdExtensionsType,@(,$Document)) | ForEach-Object {$PSCmdlet.WriteObject($_,$false)} } } |