![](/img/trans.png)
[英]Can Powershell return complete interpolated values using convertfrom-stringdata
[英]Using PowerShell Core ConvertFrom-Markdown to parse values in a markdown table
我對使用 ConvertFrom-Markdown cmdlet 來解析 markdown 表中的值很感興趣。 該 cmdlet 使用markdig markdown 處理器,它有一個抽象語法樹,應該能夠為此目的遍歷。
我們如何在以下 powershell 片段中搜索/枚舉令牌以返回行和列?
(@'
# header1
## header2
| Column1 | Column2 |
| ------- | ------- |
| Row1Column1 | Row1Column2 |
| Row2Column1 | Ro2Column2 |
'@ | ConvertFrom-Markdown).Tokens
我在令牌中看到的值看起來很有希望,我可以在Parent
字段中看到Markdig.Extensions.Tables.TableCell
,但這是我所能得到的。
這是一種方法。
注意我不確定,例如Table
是否可以只包含TableRow
s,所以| where-object {... }
| where-object {... }
可能不是必需的。
# set up some sample data
$md = @"
# header1
## header2
| Column1 | Column2 |
| ------- | ------- |
| Row1Column1 | Row1Column2 |
| Row2Column1 | Ro2Column2 |
"@ | ConvertFrom-Markdown
# walk the syntax tree
$mdDoc = $md.Tokens;
$mdTables = @( $mdDoc | where-object { $_ -is [Markdig.Extensions.Tables.Table] } );
foreach( $mdTable in $mdTables )
{
write-host "table";
$mdRows = @( $mdTable | where-object { $_ -is [Markdig.Extensions.Tables.TableRow] } );
foreach( $mdRow in $mdRows )
{
write-host " row";
write-host " header = $($mdRow.IsHeader)";
$mdCells = @( $mdRow | where-object { $_ -is [Markdig.Extensions.Tables.TableCell] } );
foreach( $mdCell in $mdCells )
{
write-host " cell";
$mdInline = $mdCell.Inline;
write-host " inline - $($mdInline.Content)";
}
}
}
其中給出以下 output:
table
row
header = True
cell
inline - Column1
cell
inline - Column2
row
header = False
cell
inline - Row1Column1
cell
inline - Row1Column2
row
header = False
cell
inline - Row2Column1
cell
inline - Ro2Column2
希望這足以讓你開始......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.