Ptree: Interactive Tree of Life Explorer Blog & articles

Blog · Backend · · 3 min read

Serving offline Wikipedia inside the viewer

Every taxon card in Ptree includes a Wikipedia article tab inside a sandboxed <iframe>. Fetching live Wikipedia pages directly in the browser would break cross-origin theming, leak visitor requests to third-party servers, fail when upstream HTML structure shifts, and leave dead links to non-biological topics outside the tree. Instead, Ptree builds a taxon-only ZIM archive offline, serves it through a local Kiwix instance, and lets Apache inject the viewer's stylesheet into every proxied article on the fly.

Building a taxon-only ZIM archive

A full English Wikipedia ZIM carries millions of articles and a massive Xapian full-text index. Ptree's ZIM subset builder derives a much smaller taxon book (floored at 450,000 kept articles) using @openzim/libzim.

Ptree opens articles by exact title (/wiki/<lang>/<Title>) and runs taxon search against its own SQLite FTS index, so .configIndexing(false, "eng") drops the Xapian full-text index that otherwise roughly doubled the file for nothing. node-libzim writes items uncompressed unless COMPRESS: 1 is passed, which would bloat HTML ~60×, so the builder sets COMPRESS: 1 for text, CSS, fonts, and SVGs inside 2 MiB Zstd clusters while leaving raster images (png, webp, jpeg, gif) raw.

node-libzim's addIllustration expects a streaming ContentProvider whose feed() returns an empty blob to terminate; a naive callback re-feeds the icon forever and balloons the archive to hundreds of gigabytes. Because Kiwix sits behind /wiki/ without serving a standalone favicon, the builder omits illustrations cleanly. libzim's Creator stores only what you hand it without parsing HTML, so the builder also owns the complete asset closure: it copies the shared MediaWiki skin bundles, extracts every url(...) reference from those stylesheets and every kept article's src, href, and style attributes, drops PDFs, and rewrites redirects whose resolved target lands in the keep-set directly to that target without chains.

Rewriting article DOM at build time

Each kept article is parsed with linkedom across parallel worker lanes (100 articles per worker batch). Because the rewrite operates on a real DOM rather than regular expressions, it cleanly removes balanced nested blocks that Apache's line-based substitution could never match safely:

for (const el of document.querySelectorAll(
  "script, table.infobox.biota, div.navbox, div.navbox-styles, table.sidebar"
)) el.remove();

Taxoboxes go because the tree and taxon card already present the taxonomy. Navboxes and their sibling .navbox-styles TemplateStyles wrappers go because the tree is the navigation. Every <script> goes because the iframe is sandboxed without allow-scripts, so script tags are dead bytes that only trigger console warnings.

Every internal <a href> is resolved through up to six redirect hops (including MediaWiki soft redirects):

  • External http/https links get target="_blank" rel="noopener".
  • Links whose resolved target is missing from the taxon keep-set are de-anchored (a.replaceWith(...a.childNodes)), leaving readable plain text with zero broken links.
  • Links that match a displayed taxon receive .taxon plus one class per backbone that holds that taxon, along with interestfor="tree-choices". Clicking or hovering a taxon link inside an article offers to jump straight to that organism in Ptree's own tree.

Injecting viewer styling through Apache

Kiwix runs on an internal container port with --threads=16 serving the taxon ZIM. Apache proxies /wiki/ to Kiwix and runs a five-stage output filter chain:

SetOutputFilter INFLATE;SUBSTITUTE;INCLUDES;BROTLI_COMPRESS;DEFLATE
Substitute "s|</head>|<!--#include virtual=\"/inc/wiki-head.html\" --></head>|n"

Apache inflates Kiwix's upstream response if needed, splices a single Server Side Include directly before </head>, lets mod_include expand the head fragment (which links Ptree's article stylesheet and theme styling), and recompresses the response with Brotli or Deflate before sending it to the browser.

Updating the article typography, dark-mode colors, or link hover styles edits one CSS file immediately without rebuilding a multi-gigabyte ZIM archive, while every article remains fast, self-contained, and free of third-party scripts.