Skip to content

chore(tools): Fix Wikipedia section extraction for wrapped headings - #913

Merged
JeanMertz merged 2 commits into
mainfrom
web-fetch-wikipedia
Jul 30, 2026
Merged

chore(tools): Fix Wikipedia section extraction for wrapped headings#913
JeanMertz merged 2 commits into
mainfrom
web-fetch-wikipedia

Conversation

@JeanMertz

Copy link
Copy Markdown
Collaborator

MediaWiki nests each heading in a <div class="mw-heading"> together with its "[edit]" link, so a section's prose is a sibling of that wrapper div, not of the heading itself. Walking the heading's own siblings only picked up the edit-link chrome, so web fetch section extraction and previews for Wikipedia (and other MediaWiki) pages returned empty or chrome-only content instead of the actual prose.

extract_heading_section and extract_preview_after_heading now walk from a heading_walk_origin, which hoists through wrapper elements (div/header/hgroup) whose only other children are inline chrome. Section-boundary checks use wrapped_heading_level so a wrapper div around the next heading still stops the walk. Hoisting is conservative: containers that mix a heading with real block content (callout boxes, nested sections) are never promoted, so their content stays attached to the section it belongs to.

Covers both MediaWiki output shapes: Parsoid's nested <section> elements and the flat legacy parser layout.

MediaWiki nests each heading in a `<div class="mw-heading">` together
with its "[edit]" link, so a section's prose is a sibling of that
wrapper div, not of the heading itself. Walking the heading's own
siblings only picked up the edit-link chrome, so `web fetch` section
extraction and previews for Wikipedia (and other MediaWiki) pages
returned empty or chrome-only content instead of the actual prose.

`extract_heading_section` and `extract_preview_after_heading` now walk
from a `heading_walk_origin`, which hoists through wrapper elements
(div/header/hgroup) whose only other children are inline chrome.
Section-boundary checks use `wrapped_heading_level` so a wrapper div
around the next heading still stops the walk. Hoisting is conservative:
containers that mix a heading with real block content (callout boxes,
nested sections) are never promoted, so their content stays attached to
the section it belongs to.

Covers both MediaWiki output shapes: Parsoid's nested `<section>`
elements and the flat legacy parser layout.

Signed-off-by: Jean Mertz <git@jeanmertz.com>
@JeanMertz
JeanMertz force-pushed the web-fetch-wikipedia branch from 4f29809 to dd515bc Compare July 30, 2026 13:36
…dings

Signed-off-by: Jean Mertz <git@jeanmertz.com>
@JeanMertz
JeanMertz merged commit 39faadd into main Jul 30, 2026
16 checks passed
@JeanMertz
JeanMertz deleted the web-fetch-wikipedia branch July 30, 2026 13:50
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.

1 participant