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

Add support for escaping { as required by MDX > v1 #326

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions .changelog/_unreleased.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@ type = "fix"
description = "Fix `escape_except_blockquotes` option for greater than 9 blockquotes in a docstring"
author = "@jackgerrits"
pr = "https://github.com/NiklasRosenstein/pydoc-markdown/pull/317"

[[entries]]
id = "f3ee1a25-3e4d-4917-b3b9-7ace18f6f9f0"
type = "improvement"
description = "Add option to escape { in markdown for compatibility with MDX >= 2"
author = "[email protected]"
pr = "https://github.com/NiklasRosenstein/pydoc-markdown/pull/326"
issues = [
"https://github.com/nonprofittechy/pydoc-markdown/issues/325",
]
7 changes: 6 additions & 1 deletion src/pydoc_markdown/contrib/renderers/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
SourceLinker,
)
from pydoc_markdown.util.docspec import ApiSuite, format_function_signature, is_method
from pydoc_markdown.util.misc import escape_except_blockquotes
from pydoc_markdown.util.misc import escape_except_blockquotes, escape_curly_brackets


def dotted_name(obj: docspec.ApiObject) -> str:
Expand Down Expand Up @@ -212,6 +212,9 @@ class MarkdownRenderer(Renderer, SinglePageRenderer, SingleObjectRenderer):
#: Escape html in docstring. Default to False.
escape_html_in_docstring: bool = False

#: Escape { and } in docstring. Default to False.
escape_curly_braces_in_docstring: bool = False

#: Render Novella `@anchor` tags before headings.
render_novella_anchors: bool = False

Expand Down Expand Up @@ -374,6 +377,8 @@ def _render_object(self, fp: t.TextIO, level: int, obj: docspec.ApiObject):
if self.escape_html_in_docstring
else obj.docstring.content
)
if self.escape_curly_braces_in_docstring:
docstring = escape_curly_brackets(docstring)
lines = docstring.split("\n")
if self.docstrings_as_blockquote:
lines = ["> " + x for x in lines]
Expand Down
7 changes: 7 additions & 0 deletions src/pydoc_markdown/util/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@ def escape_except_blockquotes(string: str) -> str:
escaped_string = escaped_string.replace(f"BLOCKQUOTE_TOKEN_{i}_END", match)

return escaped_string

def escape_curly_brackets(string: str) -> str:
"""
Escape curly brackets in a string.
"""

return string.replace("{", "\\{").replace("}", "\\}")