Skip to content

fix: strip HTML tags only from markdown sources, not RST, Python or converted pages - #602

Merged
neuromechanist merged 6 commits into
OpenScience-Collective:developfrom
bendichter:fix/clean-markdown-rst-python
Oct 8, 2026
Merged

neuromechanist merged 6 commits into
OpenScience-Collective:developfrom
bendichter:fix/clean-markdown-rst-python

Conversation

@bendichter

@bendichter bendichter commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Refs #514 (section 4).

Summary

DocumentFetcher.fetch ran clean_markdown, and so strip_html_tags, on every document. The stripper removes whatever sits between a < and the next >, which is only markup in a markdown source. In every other source it is content:

clean_markdown("See the `NWB Inspector <https://nwbinspector.readthedocs.io/>`_ docs.\n    if a<b and c>d: pass")
# -> "See the `NWB Inspector `_ docs.\n    if ad: pass"

Now the fetcher strips HTML tags only when the source URL's path ends in .md, .markdown or .mdx. RST, Python and other sources are no longer stripped, and neither is an HTML page, which was already converted to markdown before it reached the cleaner and has no tags left. The rest of the cleaning (links, images, headers, code-block spacing, whitespace) is unchanged for every source, and clean_markdown itself is unchanged.

@kabilansri2008-gif, you offered to take this on #514; I went ahead because it was still open and we needed it for NWB. If you have work in progress, I am happy to combine.

Effect on the shipped communities

Every documentation source in src/assistants/*/config.yaml (325 documents, all fetched), cleaned the old way and the new way:

Community Source Documents Changed Characters no longer removed
nwb .rst 86 46 13,582
nwb .py 29 17 3,564
mne .html 32 19 32,868
openneuropet .rst 6 6 5,939
openneuropet no suffix 1 1 349
hed .rst 1 1 1,257
eeglab .rst 1 1 876
metabci .html 12 0 0
nemar no suffix 3 0 0
all .md 154 0 0

What comes back, by the most common spans:

  • .rst: link targets (171 <https://...>), cross-reference targets (<sec-DynamicTable>), placeholders (<subject_label>, 67 times).
  • .py: link targets and the targets of :py:meth: references (<hdmf.common.table.DynamicTable.add_column>).
  • MNE's .html pages: reprs in tutorial output (<class 'mne.epochs.Epochs'>, <Annotations | 3 segments: ...>).

This table is for the first version of the change, which left markdown sources alone; the follow-up below changes some of them.

For NWB answers, the earlier 25-question comparison in #514 ran OSA with and without this stripping, graded blind, and the version without it scored higher, mostly on whether the code would run.

Tests

  • Five new tests in tests/test_tools/test_fetcher.py: an RST source keeps link targets, a Python source keeps comparisons, generics and reprs, a converted HTML page keeps angle brackets, a markdown source still loses inline HTML (for each of the suffixes), and the suffix is read from the URL's path, not its query. They go through fetch() with a pre-filled cache, as test_fetch_uses_cache does, so no network and no mocks. Four of the five fail on develop; the markdown one passes on both, which is the point of it.
  • pytest tests/test_tools -m "not network" passes.
  • ruff check and ruff format --check on the two files.

Review follow-up

Commits added to this branch after review (000fd49 to a634ecf):

  • HTML detection skips a BOM, an XML prolog, comments and whitespace before looking for <!doctype, <html, <head or <body (case-insensitive; <header> does not match). For a source that is not markdown by its URL, a text/html or application/xhtml+xml Content-Type also counts, so an HTML fragment is converted; a .md URL is never converted on the header.
  • script and style elements are dropped with their text when an HTML page is converted. markdownify's strip kept the text, so the PetSurfer comment block and up to 18 KB of JavaScript per MNE tutorial page reached the model.
  • CHANGELOG.md entries under [Unreleased].

Additional tests:

  • tests/test_tools/test_fetcher.py now also drives the network branch of fetch() against a real 127.0.0.1 http.server, with a temporary cache and no mocks: BOM, XML-prolog and comment-prefixed pages, an HTML fragment (converted only when served as text/html), .md, .rst and .py bodies, and a 404. Table tests cover _is_html and _served_as_html, including 200 comments in a row. 17 of the new tests fail with the previous detection and pass now.
  • pytest tests/test_tools -m "not network": 327 passed. All checks green at a634ecf.
  • The HTML classification of all 324 distinct shipped source URLs is unchanged: the same 48 pages (all text/html) are HTML before and after, and no source is converted only because of its header.
  • Dropping script and style changes the output of 20 of the 48 HTML pages (18 MNE tutorials, the BIDS specification page, PetSurfer); in five sampled pages only JavaScript or CSS lines were removed.
  • Markdown sources keep what is not HTML markup (488dea5). The stripper dropped any angle-bracket text, so the BIDS specification's sub-<label> reached the model as sub- (242 times), and <https://...> autolinks and HED's schema placeholders (<name>, <node>, <description>) were lost. A tag is now removed only when its name is an HTML element. A placeholder that shares a name with an element (<label>) stays unless the text closes it or gives it attributes. Fenced code blocks and inline code are kept as written, and the body of a MyST directive (```{admonition}) is treated as markdown, since that is how the HED docs use it. Comments are still dropped.
  • Of the 153 markdown documents of the shipped communities, 45 change (BIDS 24, FieldTrip 8, HED 5, OpenNeuroPET 5, NWB 3), EEGLAB and MetaBCI none. After the change no HTML element is left in the output except the <label> and <source-entities> placeholders; the rest of what stays is placeholders, XML and code. The stripper takes under 5 ms per document.
  • 16 new unit tests for the stripper and one fetch() test for a markdown source; the tools suite passes (344 tests) on Python 3.12 and 3.14.

@codecov-commenter

codecov-commenter commented Oct 7, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 98.41270% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/tools/markdown_cleaner.py 97.91% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

…ments, or are served as text/html

_is_html only matched a leading doctype or html tag. A page that began with a byte
order mark, an <?xml?> prolog, a comment, <head> or <body>, or an HTML fragment served
as text/html, was not converted to markdown; since this change stopped stripping tags
from non-markdown sources, its raw markup reached the model.

_is_html now skips whitespace, a BOM, an XML prolog and comments, and matches the
doctype, html, head and body tags case-insensitively (header does not match head).
A bare div or p does not count, because markdown READMEs start with them. For a source
that is not markdown by its URL, a text/html or application/xhtml+xml Content-Type also
counts.

Checked over the 324 distinct documentation source URLs of the shipped communities:
the same 48 pages are classified as HTML before and after, and no source flips.
Unit and network-path tests follow in the next commit.
The five tests added earlier fill the cache and so only reach the cached branch of
fetch(). Add tests that fetch from a real local http.server (no mocks) through the
network branch: a page that starts with a BOM, an XML prolog or comments is converted;
an HTML fragment is converted only when served as text/html; a markdown source stays
markdown whatever its Content-Type, and READMEs that start with a p, a header or a lint
comment are not taken for HTML; RST, Python and markdown sources keep or lose angle
brackets as intended. Table tests cover _is_html and _served_as_html, including a run
of comments that must not backtrack.

The test of the markdown suffix no longer asserts that a markdown autolink is deleted,
which pinned a known limitation; it uses an inline br tag instead.

17 of the new tests fail with the previous _is_html and pass now. pytest tests/test_tools
-m 'not network' passes.
…ML page

markdownify's strip option removes a tag but keeps the text inside it, so the JavaScript
and CSS of a page reached the model as content: up to 18 KB of repr-toggle script in a
single MNE tutorial, and the <!--// ... //--> block of the PetSurfer page, which the HTML
tag stripper used to remove by accident before this change.

Decompose script and style elements with their text, as already done for nav, footer and
aside. Tested with a page served by a local server, with and without a main element.

Over the 48 HTML pages among the shipped documentation sources, the output changes for 20
(18 MNE tutorial pages, the BIDS specification page, PetSurfer); in the pages sampled the
removed lines are script or style text.
…e-Collective#602)

Records the narrower HTML stripping, the wider HTML detection and the script and style
cleanup under [Unreleased], as the changelog asks of the PR that makes the change.
Documentation only; no code touched in this commit.
A tag is removed only when its name is an HTML element; fenced code and inline code
are kept as written; a MyST directive body is markdown. Tested on the 153 shipped
markdown documents and under Python 3.12 and 3.14.
@neuromechanist
neuromechanist merged commit cee6bc0 into OpenScience-Collective:develop Oct 8, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants