Skip to content

Conversation

@zacharysyoung
Copy link
Contributor

@zacharysyoung zacharysyoung commented Aug 19, 2024

This PR correctly formats the Markdown in #13, but creates (uncovers?) an issue with escaped square brackets in the label. I didn't want to change other code, so I've added six round-trip-failure exceptions, just to prove that at least reference links print correctly.

The new issue arises with input like:

[foo][ref\[]

[ref\[]: /uri

being formatted and printed as:

[foo][]

A guard clause at the top of normalizeLabel sees the bracket in ref\[ (from parseLinkLabel) as invalid and returns the empty string, so the link (and title) are mapped to the empty string in the links map; and my code also tries to associate the parsed link with the empty string:

markdown/link.go

Lines 398 to 405 in 868a055

func normalizeLabel(s string) string {
if strings.Contains(s, "[") || strings.Contains(s, "]") {
// Labels cannot have [ ] so avoid the work of translating.
// This is especially important for pathlogical cases like
// [[[[[[[[[[a]]]]]]]]]] which would otherwise generate quadratic
// amounts of garbage.
return ""
}

Removing that guard allows those six tests to pass, along with the new ref-link tests, and doesn't affect any other test.

Given that parseLinkLabel allows escaped brackets (I believe it allows all escaped chars), can a label with a bracket be used as a key?

Removing that guard clause also makes the BigTest/nested_brackets test take about 5s on my machine to run. Does that test mean anything, given that there's a 999-char limit on a label?

I'd be happy to reconsider my approach my to reference links.

I also saw a TODO in normalizeLabel about using the normalized label only as a key in the links map, and printing the original label. If that work fits in the scope of this issue/PR, I'd work on that.

Thanks.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant