`strip` option preserves inner text of removed tags — request for content-removing option
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 65/100
Research direction
Start with the process_tag entry point shown in the issue, then inspect get_conv_fn_cached and the existing handling of strip. Verify the provided script, style, and noscript example and confirm that decompose removes both the tags and their child content while preserving surrounding text.
Written by the indexing model from the issue text.
Description
Environment
- markdownify: 1.2.2
- Python: 3.12.3
Current Behavior
strip=["script"] removes the tag but preserves its inner text as plain text:
```python
from markdownify import markdownify as md
html = '
Hello
<script>alert("js noise")</script>World
'print(md(html, strip=["script"]))
Hello
alert("js noise") ← script content remains as plain text
World
```
Root Cause
process_tag() collects and joins all child text before checking
should_convert_tag(). By the time get_conv_fn_cached() returns None
for a stripped tag, the child text has already been assembled:
```python
def process_tag(self, node, ...):
# Children processed unconditionally — strip has no effect here
child_strings = [self.process_element(el, ...) for el in children_to_convert]
text = ''.join(child_strings)
# should_convert_tag() checked only here — too late to suppress children
convert_fn = self.get_conv_fn_cached(node.name) # returns None for stripped tags
if convert_fn is not None:
text = convert_fn(node, text, ...)
return text # child text always returned
```
Proposed Solution
Add a decompose parameter that suppresses child processing entirely:
```python
md(html, decompose=["script", "style", "noscript"])
→ Hello\n\nWorld
```
Implementation: early return '' in process_tag when the tag is in the
decompose list.
Workaround
```python
from bs4 import BeautifulSoup
from markdownify import markdownify as md
soup = BeautifulSoup(html, "html.parser")
for tag in soup(["script", "style", "noscript"]):
tag.decompose()
md(str(soup), heading_style="ATX")
```
Use Case
This affects web scraping pipelines where <script> and <style> content
is meaningless as plain text — analytics snippets, minified JS bundles, and
inline CSS all leak into the markdown output when using strip.
- Dominant language
- Python
- Stars
- 2.2k
- Forks
- 203
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from matthewwithanm/python-markdownify
-
RecursionError on deeply nested HTML (about 330 levels), separate from the cyclic-tree case in #256 Open
Difficulty 3/5 1-2 days Newbie friendliness 68/100
-
Difficulty 4/5 3-5 days Newbie friendliness 52/100
matthewwithanm/python-markdownify#261 · 1 comment · 1 reaction ·
-
Difficulty 3/5 1-2 days Newbie friendliness 50/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 50/100
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
All issues in matthewwithanm/python-markdownify
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100