Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PipeTableParser strip opening and ending characters #775

Open
doggy8088 opened this issue Feb 22, 2024 · 1 comment
Open

PipeTableParser strip opening and ending characters #775

doggy8088 opened this issue Feb 22, 2024 · 1 comment

Comments

@doggy8088
Copy link

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.

image

@xoofx
Copy link
Owner

xoofx commented Feb 29, 2024

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants