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

Escape ` inside img alt descriptions #406

Merged
merged 1 commit into from
Sep 25, 2023
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
33 changes: 29 additions & 4 deletions src/doc_builder/convert_md_to_mdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,31 @@ def convert_img_links(text, page_info):
return text


_re_md_img_tag_alt = re.compile(r"!\[([^\]]+)\]", re.I)


def escape_img_alt_description(text):
"""
Escapes ` with ' inside <img> alt description since it causes svelte/mdsvex compiler error.
"""

def replace_alt_content(match):
alt_content = match.group(1)
new_alt_content = alt_content.replace("`", "'")
return match.group(0).replace(alt_content, new_alt_content)

if _re_md_img_tag_alt.search(text):
text = _re_md_img_tag_alt.sub(replace_alt_content, text)

return text


def fix_img_links(text, page_info):
text = convert_img_links(text, page_info)
text = escape_img_alt_description(text)
return text


def clean_doctest_syntax(text):
"""
Clean the doctest artifacts in a given content.
Expand Down Expand Up @@ -156,13 +181,13 @@ def convert_md_docstring_to_mdx(docstring, page_info):
def process_md(text, page_info):
"""
Processes markdown by:
1. Converting include
2. Converting literalinclude
1. Convert include
2. Convert literalinclude
3. Clean doctest syntax
4. Converting image links
4. Fix image links
"""
text = convert_include(text, page_info)
text = convert_literalinclude(text, page_info)
text = clean_doctest_syntax(text)
text = convert_img_links(text, page_info)
text = fix_img_links(text, page_info)
return text
18 changes: 18 additions & 0 deletions tests/test_convert_md_to_mdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
convert_include,
convert_literalinclude,
convert_md_to_mdx,
escape_img_alt_description,
process_md,
)

Expand Down Expand Up @@ -75,6 +76,23 @@ def test_convert_img_links(self):
convert_img_links(img_html, page_info), '<img src="/docs/transformers/v4.10.0/fr/imgs/img.gif"/>'
)

def test_escape_img_alt_description(self):
multiple_imgs_md = """![Animation exploring `model_args.pipeline_tag`](imgsrc)

### Some heading

![Animation exploring `model_args.pipeline_tag`](imgsrc)

![Animation exploring model_args.pipeline_tag](imgsrc)"""
expected_conversion = """![Animation exploring 'model_args.pipeline_tag'](imgsrc)

### Some heading

![Animation exploring 'model_args.pipeline_tag'](imgsrc)

![Animation exploring model_args.pipeline_tag](imgsrc)"""
self.assertEqual(escape_img_alt_description(multiple_imgs_md), expected_conversion)

def test_process_md(self):
page_info = {"package_name": "transformers", "version": "v4.10.0", "language": "fr"}

Expand Down
Loading