From 453b6040962713ef90f6272c6984a5a19ec9d636 Mon Sep 17 00:00:00 2001 From: AlexVonB Date: Sat, 2 Jan 2021 17:22:27 +0100 Subject: [PATCH] Fixing autolinks When checking a links href and text for equality, first un-escape the underscores in the text -- because six escapes them. This should fix #29. --- markdownify/__init__.py | 3 ++- tests/test_conversions.py | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/markdownify/__init__.py b/markdownify/__init__.py index 2cd8fc8..c2e2ec0 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -131,7 +131,8 @@ class MarkdownConverter(object): return text href = el.get('href') title = el.get('title') - if self.options['autolinks'] and text == href and not title: + # For the replacement see #29: text nodes underscores are escaped + if self.options['autolinks'] and text.replace(r'\_', '_') == href and not title: # Shortcut syntax return '<%s>' % href title_part = ' "%s"' % title.replace('"', r'\"') if title else '' diff --git a/tests/test_conversions.py b/tests/test_conversions.py index f5fc1c2..2896322 100644 --- a/tests/test_conversions.py +++ b/tests/test_conversions.py @@ -34,7 +34,11 @@ def test_chomp(): def test_a(): - assert md('Google') == '[Google](http://google.com)' + assert md('Google') == '[Google](https://google.com)' + assert md('https://google.com', autolinks=False) == '[https://google.com](https://google.com)' + assert md('https://google.com') == '' + assert md('https://community.kde.org/Get_Involved') == '' + assert md('https://community.kde.org/Get_Involved', autolinks=False) == '[https://community.kde.org/Get\\_Involved](https://community.kde.org/Get_Involved)' def test_a_spaces():