Repository navigation
fix: strip HTML tags only from markdown sources, not RST, Python or converted pages - #602
Merged
neuromechanist merged 6 commits intoOct 8, 2026
Conversation
|
Codecov Report❌ Patch coverage is
📢 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
merged commit Oct 8, 2026
cee6bc0
into
OpenScience-Collective:develop
7 checks passed
11 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #514 (section 4).
Summary
DocumentFetcher.fetchranclean_markdown, and sostrip_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:Now the fetcher strips HTML tags only when the source URL's path ends in
.md,.markdownor.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, andclean_markdownitself 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
documentationsource insrc/assistants/*/config.yaml(325 documents, all fetched), cleaned the old way and the new way:.rst.py.html.rst.rst.rst.html.mdWhat 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>)..htmlpages: 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
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 throughfetch()with a pre-filled cache, astest_fetch_uses_cachedoes, so no network and no mocks. Four of the five fail ondevelop; the markdown one passes on both, which is the point of it.pytest tests/test_tools -m "not network"passes.ruff checkandruff format --checkon the two files.Review follow-up
Commits added to this branch after review (
000fd49toa634ecf):<!doctype,<html,<heador<body(case-insensitive;<header>does not match). For a source that is not markdown by its URL, atext/htmlorapplication/xhtml+xmlContent-Type also counts, so an HTML fragment is converted; a.mdURL is never converted on the header.scriptandstyleelements are dropped with their text when an HTML page is converted. markdownify'sstripkept the text, so the PetSurfer comment block and up to 18 KB of JavaScript per MNE tutorial page reached the model.CHANGELOG.mdentries under[Unreleased].Additional tests:
tests/test_tools/test_fetcher.pynow also drives the network branch offetch()against a real127.0.0.1http.server, with a temporary cache and no mocks: BOM, XML-prolog and comment-prefixed pages, an HTML fragment (converted only when served astext/html),.md,.rstand.pybodies, and a 404. Table tests cover_is_htmland_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 ata634ecf.text/html) are HTML before and after, and no source is converted only because of its header.scriptandstylechanges 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.488dea5). The stripper dropped any angle-bracket text, so the BIDS specification'ssub-<label>reached the model assub-(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.<label>and<source-entities>placeholders; the rest of what stays is placeholders, XML and code. The stripper takes under 5 ms per document.fetch()test for a markdown source; the tools suite passes (344 tests) on Python 3.12 and 3.14.