Skip to content

Commit

Permalink
Add support for escaping { as required by MDX > v1, fix #325
Browse files Browse the repository at this point in the history
  • Loading branch information
nonprofittechy committed Apr 20, 2024
1 parent 3f20390 commit 562596c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
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("}", "\\}")

0 comments on commit 562596c

Please sign in to comment.