Add HTML to Text tool - #5
Merged
Merged
Conversation
A pure tokenizer in core/ (no DOM, no new dependency) that keeps paragraph, list and table structure, drops script/style/head/svg, hidden and role=img subtrees, and decodes entities including the windows-1252 numeric remap. Optional link URLs and class-based hidden heuristics.
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.
New tool at
/html-text(Web category). Paste HTML, get readable plain text back.Why a hand-written tokenizer
DOMParserbreaks thecore/purity rule and would need jsdom to test.html-to-textpulls in a parser for something that doesn't need a tree: extracting text only requires knowing which blocks, lists, cells and hidden subtrees you're inside.core/extract.tsis one pass over the string with a small stack of open elements. There's no new dependency, and it never throws.Rules
div,section,header,li,tr, …p,h1–h6,blockquote,pre,table, top-level listsli-or1.(honours<ol start>), nested lists indentedtd/thbr/pre kept as a spacescript,style,head,template,svg,math, media, commentshiddenattribute, inlinedisplay:none,role="img"expand_less)Browser-matching details:
li,p,td/th,tranddt/ddare closed implicitly, as the browser does.€–Ÿmap to windows-1252, so–becomes–rather than a C1 control character.Entities: all of Latin-1, plus about 90 common named ones and all numeric references. Unknown names are left as they are rather than guessed. The full HTML5 table would add about 10 kB gzip for names almost nobody uses.
Options (both off by default):
docs <https://…>. Skipped when the link text already is the URL, and for#/javascript:links..hidden/.sr-only: guesses from class names, so it's opt-in.Known gap
Layout that comes from CSS can't be recovered. In the Play Console fixture, the badge, app name and date sit in one flex row, but they come out on three lines because they're
<div>s. The only way to fix that is to render the pasted HTML, which we won't do.Checks
core/:<pre>, entities, skipped subtrees, links<, unterminated tags and comments, stray close tagsstore: 'idb', because pasted page source can run to several MB.pnpm test,tsc --noEmitandpnpm lintare clean. The one lint warning is indiff/MergePane.tsx, which this PR doesn't touch.