From 321e9eb5f64ee15474d2853efee5ed3cffe47a64 Mon Sep 17 00:00:00 2001 From: Bruno Miguens Date: Fri, 5 Feb 2021 19:38:24 +0000 Subject: [PATCH] Add ignore comment tags --- markdownify/__init__.py | 6 ++++-- tests/test_advanced.py | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/markdownify/__init__.py b/markdownify/__init__.py index 6d93e47..5c008d3 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -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) diff --git a/tests/test_advanced.py b/tests/test_advanced.py index 4c480d7..2f8aeb1 100644 --- a/tests/test_advanced.py +++ b/tests/test_advanced.py @@ -4,3 +4,11 @@ from markdownify import markdownify as md def test_nested(): text = md('

This is an example link.

') assert text == 'This is an [example link](http://example.com/).\n\n' + +def test_ignore_comments(): + text = md("") + assert text == "" + +def test_ignore_comments_with_other_tags(): + text = md("example link") + assert text == "[example link](http://example.com/)" \ No newline at end of file