14
246
Fork
You've already forked pyfedi
75

Better handle links with parentheses #1689

Merged
rimu merged 1 commit from wjs018/pyfedi:links_w_parens into main 2026年03月14日 22:09:52 +01:00
Collaborator
Copy link

This PR addresses #1625. Basically, it parses the resulting html after markdown2 runs, and uses BeautifulSoup to look at all the hrefs as well as the text inside and immediately after the link.

  • If it looks like a parenthesis was dropped from the end of the link, it will stick it back on the href and link text while stripping it from the trailing text.
  • If it looks like a trailing parenthesis was added accidentally onto the link, it will be stripped from the link href and text and then added to the trailing text.

This determination is made with the assumption that a url with parentheses are likely to have a balanced number of ( and ) characters. As far as I can tell, this is a decent assumption. Basically the only place I have systematically seen this be an issue is with wikipedia links since they love to use parentheses (and their urls are going to be well-formed).

I added some new tests and made sure that all the tests for both markdown_to_html and allowlist_html pass. I did have to make one tweak in allowlist_html to properly handle <hr/> tags that result from the analysis I do with BeautifulSoup when the html has a horizontal rule in it.

This PR addresses #1625. Basically, it parses the resulting html after markdown2 runs, and uses BeautifulSoup to look at all the hrefs as well as the text inside and immediately after the link. - If it looks like a parenthesis was dropped from the end of the link, it will stick it back on the href and link text while stripping it from the trailing text. - If it looks like a trailing parenthesis was added accidentally onto the link, it will be stripped from the link href and text and then added to the trailing text. This determination is made with the assumption that a url with parentheses are likely to have a balanced number of `(` and `)` characters. As far as I can tell, this is a decent assumption. Basically the only place I have systematically seen this be an issue is with wikipedia links since they love to use parentheses (and their urls are going to be well-formed). I added some new tests and made sure that all the tests for both `markdown_to_html` and `allowlist_html` pass. I did have to make one tweak in `allowlist_html` to properly handle `<hr/>` tags that result from the analysis I do with BeautifulSoup when the html has a horizontal rule in it.
Sign in to join this conversation.
No reviewers
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
rimu/pyfedi!1689
Reference in a new issue
rimu/pyfedi
No description provided.
Delete branch "wjs018/pyfedi:links_w_parens"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?