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

improve: Escape less than signs less #465

Merged
merged 1 commit into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/users/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Note that there is currently no guarantee for a stable Markdown formatting style
With this plugins can now read CLI arguments merged with values from `.mdformat.toml`.
- Changed
- Style: No longer escape square bracket enclosures.
- Style: No longer escape less than sign followed by space character.
- Improved
- Plugin interface: A trailing newline is added to fenced code blocks if a plugin fails to add it.

Expand Down
3 changes: 2 additions & 1 deletion src/mdformat/renderer/_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
decimalify_leading,
decimalify_trailing,
escape_asterisk_emphasis,
escape_less_than_sign,
escape_square_brackets,
escape_underscore_emphasis,
get_list_marker_type,
Expand Down Expand Up @@ -118,7 +119,7 @@ def text(node: RenderTreeNode, context: RenderContext) -> str:
text = escape_underscore_emphasis(text) # Escape emphasis/strong marker.
# Escape link label and link ref enclosures
text = escape_square_brackets(text, context.env["used_refs"])
text = text.replace("<", "\\<") # Escape URI enclosure
text = escape_less_than_sign(text) # Escape URI enclosure and HTML.
text = text.replace("`", "\\`") # Escape code span marker

# Escape "&" if it starts a sequence that can be interpreted as
Expand Down
13 changes: 13 additions & 0 deletions src/mdformat/renderer/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,16 @@ def escape_square_brackets(text: str, used_refs: Iterable[str]) -> str:


RE_SQUARE_BRACKET = re.compile(r"[\[\]]")


def escape_less_than_sign(text: str) -> str:
"""Escape less than sign ('<') to prevent unexpected HTML or autolink.

Current heuristic to use: Always escape, except when
- followed by a space: This should be safe. Neither HTML nor autolink
allow space after the '<' sign
"""
return RE_LESS_THAN_SIGN__NO_FOLLOWING_SPACE.sub(r"\\\g<0>", text)


RE_LESS_THAN_SIGN__NO_FOLLOWING_SPACE = re.compile("<(?:[^ ]|$)")
13 changes: 13 additions & 0 deletions tests/data/default_style.md
Original file line number Diff line number Diff line change
Expand Up @@ -495,3 +495,16 @@ Square bracket escapes

[link-label]: /url
.

Less than sign escapes
.
< no escape < no escape, now escape <
<

<
.
< no escape < no escape, now escape \<
\<

\<
.