JavaScript-Rendered Pages with Lightpanda
File: Lightpanda.lean
Many web pages build their content with JavaScript. curl returns only the HTML the server sends, so for such a page the text is missing even though the request succeeded. Lightpanda is a headless browser: it loads the page, runs its scripts, and prints the result as HTML or Markdown. It uses little memory compared with a full browser, which matters when you fetch many pages.
Dump Formats
| Format | Use |
|---|---|
html |
raw DOM after scripts, for link extraction |
markdown |
text for an LLM or a summary |
semantic_tree |
structured tree with roles and text |
semantic_tree_text |
compact text version of the tree |
main asks for markdown for the page and html for the links. The semantic tree is better when you need headings, lists, or link roles rather than raw tags.
The Code
1 -- lean --run Lightpanda.lean (needs the lightpanda binary on PATH)
2
3 -- dump is one of: html, markdown, semantic_tree, semantic_tree_text
4 def lightpanda (url : String) (dump : String := "html") : IO String := do
5 let out ← IO.Process.output {
6 cmd := "lightpanda"
7 args := #["fetch", "--dump", dump, "--log_level", "warn", url] }
8 if out.exitCode != 0 then
9 throw (IO.userError s!"lightpanda failed: {out.stderr}")
10 return out.stdout
11
12 def links (html : String) : List String :=
13 (html.splitOn "href=\"").drop 1 |>.map fun rest =>
14 String.ofList (rest.toList.takeWhile (· ≠ '"'))
15
16 def main : IO Unit := do
17 IO.println (← lightpanda "https://markwatson.com/" "markdown")
18 IO.println (links (← lightpanda "https://markwatson.com/"))
How It Works
lightpandastarts the browser withIO.Process.output, the same call as in the last chapter. Thedumpparameter picks the output format and defaults to"html".linksfinds each link target with string functions alone.splitOn "href=\""cuts the page at everyhref=".drop 1removes the text before the first link. For each piece that remains,takeWhile (· ≠ '"')keeps the characters up to the closing quote.linksis a pure function. Its type has noIO, so you can test it with#evalon a short string.
links is a string split, not a parser. It misses single-quoted href='...', relative URLs that need a base, duplicate targets, and links that scripts build from data. It also keeps fragments and stylesheet paths, as the output shows. The semantic tree output gives structured links, and a real crawler should read it rather than split HTML.
Lean 4 Notes
Fields on separate lines. In a structure value you can put each field on its own line and leave out the commas. lightpanda does this for cmd and args.
Pure and IO code in one file. lightpanda returns IO String and links returns List String. The types show which function does input and output. Keep as much code as you can pure. You can test pure code with #eval and state facts about it with example:
1 #eval links "<a href=\"a.html\">x</a>" -- ["a.html"]
|>. after a call with arguments. (html.splitOn "href=\"").drop 1 |>.map fun rest => ... applies map to the result of drop 1. The function is the last argument, so it needs no parentheses.
Characters in quotes. '"' is the double-quote character. Inside a string the same character needs a backslash: "href=\"".
Printing a list. IO.println accepts any value whose type has a ToString instance. List String has one, so the second line of main prints the list in brackets.
Running It
Install Lightpanda from https://lightpanda.io and put the lightpanda program on your PATH. The first call prints my home page as Markdown. The second prints its links. I cut both outputs short here.
1 $ lean --run Lightpanda.lean
2 [Mark Watson ](https://markwatson.com/#)
3 - [Systems Thinking](https://markwatson.com/#systems)
4 - [Books](https://markwatson.com/#books)
5 - [Connect](https://markwatson.com/#connect)
6 - [Free Mentoring](https://markwatson.com/#mentoring)
7 - [Flagstaff Arizona](https://markwatson.com/flagstaff.html)
8 ...
9 [/index.css, #, #systems, #books, #connect, #mentoring,
10 /flagstaff.html, https://mark-watson.blogspot.com/,
11 https://marklwatson.substack.com, https://github.com/mark-watson,
12 https://leanpub.com/u/markwatson, ...]
Using It
Pass the Markdown output to one of the LLM chapters to summarize a page. Filter links to keep the targets that start with https://, then fetch each one to build a small crawler.
Before you crawl, read the site’s robots.txt and keep the request rate low. A headless browser uses more memory and time than curl, even a small one like Lightpanda, and many sites rate-limit or block automated fetches. Try this: fetch two pages, deduplicate the links with eraseDups, keep only targets that start with https://, and pass the Markdown to the Ollama chapter for a summary.