From 562596cdb04fcabb3690d059df8ac70c6f185a42 Mon Sep 17 00:00:00 2001 From: Quinten Steenhuis Date: Sat, 20 Apr 2024 15:40:21 -0400 Subject: [PATCH 1/3] Add support for escaping { as required by MDX > v1, fix #325 --- src/pydoc_markdown/contrib/renderers/markdown.py | 7 ++++++- src/pydoc_markdown/util/misc.py | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/pydoc_markdown/contrib/renderers/markdown.py b/src/pydoc_markdown/contrib/renderers/markdown.py index 2e478b08..012b0854 100644 --- a/src/pydoc_markdown/contrib/renderers/markdown.py +++ b/src/pydoc_markdown/contrib/renderers/markdown.py @@ -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: @@ -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 @@ -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] diff --git a/src/pydoc_markdown/util/misc.py b/src/pydoc_markdown/util/misc.py index 8b98c044..296d2908 100644 --- a/src/pydoc_markdown/util/misc.py +++ b/src/pydoc_markdown/util/misc.py @@ -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("}", "\\}") \ No newline at end of file From f4ca7591bed402650df640743851076bde4ccb3c Mon Sep 17 00:00:00 2001 From: Quinten Steenhuis Date: Sat, 20 Apr 2024 16:13:12 -0400 Subject: [PATCH 2/3] Add changelog entry --- .changelog/_unreleased.toml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.changelog/_unreleased.toml b/.changelog/_unreleased.toml index dcf1f154..2311504d 100644 --- a/.changelog/_unreleased.toml +++ b/.changelog/_unreleased.toml @@ -25,3 +25,12 @@ 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 = "qsteenhuis@gmail.com" +issues = [ + "https://github.com/nonprofittechy/pydoc-markdown/issues/325", +] From 06395f68235a02e3dd59dae6c8116da8141bac82 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Sat, 20 Apr 2024 20:13:47 +0000 Subject: [PATCH 3/3] Updated PR references in 1 changelogs. skip-checks: true --- .changelog/_unreleased.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/.changelog/_unreleased.toml b/.changelog/_unreleased.toml index 2311504d..a53c4414 100644 --- a/.changelog/_unreleased.toml +++ b/.changelog/_unreleased.toml @@ -31,6 +31,7 @@ id = "f3ee1a25-3e4d-4917-b3b9-7ace18f6f9f0" type = "improvement" description = "Add option to escape { in markdown for compatibility with MDX >= 2" author = "qsteenhuis@gmail.com" +pr = "https://github.com/NiklasRosenstein/pydoc-markdown/pull/326" issues = [ "https://github.com/nonprofittechy/pydoc-markdown/issues/325", ]