Repository: matthewwithanm/python-markdownify

Issue:
`<a>` tag links should not be constructed inside preformatted tags
The conversion for `<a>` tags creates links inside `<code>` and `<pre>` tags:

``````python
from markdownify import markdownify as md

print(md("<code><a href='http://foo.com/'>foo.com</a></code>"))
# `[foo.com](http://foo.com/)`

print(md("<pre><a href='http://foo.com/'>foo.com</a></pre>"))
# ```
# [foo.com](http://foo.com/)
# ```
``````

This is incorrect because content inside Markdown code spans and code blocks is interpreted literally. In these cases, the tags should be rendered as plaintext.

Discussion:

Patch:
diff --git a/markdownify/__init__.py b/markdownify/__init__.py
index 3272ce5..0fbb7a5 100644
--- a/markdownify/__init__.py
+++ b/markdownify/__init__.py
@@ -264,6 +264,8 @@ class MarkdownConverter(object):
         return '\n\n%s\n%s\n\n' % (text, pad_char * len(text)) if text else ''
 
     def convert_a(self, el, text, convert_as_inline):
+        if el.find_parent(['pre', 'code', 'kbd', 'samp']):
+            return text
         prefix, suffix, text = chomp(text)
         if not text:
             return ''


Test patch:
diff --git a/tests/test_conversions.py b/tests/test_conversions.py
index 2283c29..55702e3 100644
--- a/tests/test_conversions.py
+++ b/tests/test_conversions.py
@@ -39,6 +39,11 @@ def test_a_no_autolinks():
     assert md('<a href="https://google.com">https://google.com</a>', autolinks=False) == '[https://google.com](https://google.com)'
 
 
+def test_a_in_code():
+    assert md('<code><a href="https://google.com">Google</a></code>') == '`Google`'
+    assert md('<pre><a href="https://google.com">Google</a></pre>') == '\n```\nGoogle\n```\n'
+
+
 def test_b():
     assert md('<b>Hello</b>') == '**Hello**'