Add ignore comment tags

This commit is contained in:
Bruno Miguens
2021-02-05 19:38:24 +00:00
parent 77d1e99bd5
commit 321e9eb5f6
2 changed files with 12 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
from bs4 import BeautifulSoup, NavigableString
from bs4 import BeautifulSoup, NavigableString, Comment
import re
import six
@@ -75,7 +75,9 @@ class MarkdownConverter(object):
# Convert the children first
for el in node.children:
if isinstance(el, NavigableString):
if isinstance(el, Comment):
continue
elif isinstance(el, NavigableString):
text += self.process_text(six.text_type(el))
else:
text += self.process_tag(el, convert_children_as_inline)

View File

@@ -4,3 +4,11 @@ from markdownify import markdownify as md
def test_nested():
text = md('<p>This is an <a href="http://example.com/">example link</a>.</p>')
assert text == 'This is an [example link](http://example.com/).\n\n'
def test_ignore_comments():
text = md("<!-- This is a comment -->")
assert text == ""
def test_ignore_comments_with_other_tags():
text = md("<!-- This is a comment --><a href='http://example.com/'>example link</a>")
assert text == "[example link](http://example.com/)"