markdig
markdig copied to clipboard
PipeTableParser strip opening and ending characters
Here is my code that can run under LINQPad:
void Main()
{
var builder = new MarkdownPipelineBuilder();
var pt = new PipeTableExtension();
pt.Setup(builder);
var pipeline = builder.Build();
var markdownText = GetText();
var document = Markdown.Parse(markdownText, pipeline);
var blocks = document.ToList();
foreach (var item in blocks)
{
string blockText = ExtractText(markdownText, item);
blockText.Pre().Dump("Parsed Output");
}
}
public static string ExtractText(string text, Block item)
{
var pos = item.ToPositionText(); // $295, 0, 13301-13375
// parse "$295, 0, 13301-13375" into lines, columns, range variables
var line = pos.Split(",")[0].TrimStart('$').Trim();
var column = pos.Split(",")[1].Trim();
var range = pos.Split(",")[2].Trim();
var start = range.Split("-")[0];
var end = range.Split("-")[1];
var blockText = text.Substring(int.Parse(start), int.Parse(end) - int.Parse(start) + 1);
return blockText;
}
string GetText() => """
| Attributes | Details |
|:--- |:--- |
| `<docs-card-container>` | All cards must be nested inside a container |
| `title` | Card title |
| card body contents | Anything between `<docs-card>` and `</docs-card>` |
| `link` | (Optional) Call to Action link text |
| `href` | (Optional) Call to Action link href |
""";
When running this code, the parsed output will remove the opening and ending characters. It seems a bug.
It might be a bug in the position (not all elements, specially the one not CommonMark like table are necessarily tested for their text position)
That being said, this ExtractText
function feels complicated to extract the line/column/span information. Not sure why you are not using the properties behind (Span, Line, Column) here
public static string ExtractText(string text, Block item)
{
var pos = item.ToPositionText(); // $295, 0, 13301-13375
// parse "$295, 0, 13301-13375" into lines, columns, range variables
var line = pos.Split(",")[0].TrimStart('$').Trim();
var column = pos.Split(",")[1].Trim();
var range = pos.Split(",")[2].Trim();
var start = range.Split("-")[0];
var end = range.Split("-")[1];
var blockText = text.Substring(int.Parse(start), int.Parse(end) - int.Parse(start) + 1);
return blockText;
}