Skip to main content

Jupyterlab extension to fix issues with TOC navigation in Markdown Viewer in Jupyterlab

Project description

jupyterlab_markdown_viewer_toc_fix

GitHub Actions npm version PyPI version Total PyPI downloads JupyterLab 4

JupyterLab 4.x extension that fixes broken Table of Contents (TOC) navigation and anchor links in the Markdown Viewer.

[!WARNING] Extension will be deprecated: This extension is a temporary fix for a known issue in JupyterLab 4.0 - 4.4.10, where neither TOC nor in-markdown hyperlinks to markdown sections allow document to be scrolled. Once this is fixed in JupyterLab core, this extension will be obsolete and should not be installed.

Problem

JupyterLab's markdown viewer has broken TOC navigation and anchor links. Affects JupyterLab 4.0 through 4.4.10.

Symptoms:

  • Clicking TOC panel items produces no scrolling
  • In-document links like [Go to Section](#section) don't navigate
  • URL navigation file.md#section fails to scroll to target
  • Console shows "Heading element not found" errors

Root Cause - Attribute Selector Mismatch:

  • JupyterLab's HTML sanitizer removes id attributes for XSS protection
  • Headings get data-jupyter-id attributes instead (default setting allowNamedProperties = false)
  • Navigation code queries for id attributes that don't exist
  • querySelector() returns null and scroll attempts fail silently
  • Example: ## Introduction renders as <h2 data-jupyter-id="Introduction"> but TOC queries [id="Introduction"]

Why Notebooks Work But Markdown Viewer Doesn't:

  • Notebooks were fixed in August 2025 (commit c94607d591) with conditional attribute selectors
  • Markdown viewer was not updated with the same fix

Secondary Issue - Case Mismatch:

  • URL fragments are lowercase: #performance-optimization
  • Attributes preserve Title-Case from headings: data-jupyter-id="Performance-Optimization"
  • Direct string matching fails without case-insensitive comparison

Solution

Extension applies runtime patches to fix navigation without modifying JupyterLab core:

TOC Navigation Patch:

  • Connects to markdown viewer's activeHeadingChanged signal
  • Uses conditional attribute selector (data-jupyter-id vs id) based on sanitizer.allowNamedProperties setting
  • Properly awaits async heading ID resolution from markdown parser
  • Scrolls to correct heading element with scrollIntoView()

Fragment Navigation Patch:

  • Intercepts RenderedHTMLCommon.setFragment() prototype method
  • Queries both #id and [data-jupyter-id] selectors with fallback chain
  • Handles JupyterLab router routed signal for in-app navigation
  • Listens to global hashchange events for URL-based navigation

Case-Insensitive Fallback:

  • Loops through all headings with data-jupyter-id or id attributes
  • Compares lowercase versions of search term and attribute values
  • Returns first match when direct selectors fail
  • Resolves mismatch between lowercase URL fragments and Title-Case attributes

All patches include graceful fallbacks to original implementations if patching fails or elements cannot be found.

Install

pip install jupyterlab_markdown_viewer_toc_fix

Uninstall

pip uninstall jupyterlab_markdown_viewer_toc_fix

Development

# Clone and install dependencies
jlpm install

# Set up virtual environment
python -m venv .venv
source .venv/bin/activate
pip install -e .

# Link extension
jupyter labextension develop . --overwrite

# Build
jlpm build

# Watch mode (auto-rebuild on changes)
jlpm watch

Requirements

  • JupyterLab >= 4.0.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jupyterlab_markdown_viewer_toc_fix-1.0.10.tar.gz (251.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file jupyterlab_markdown_viewer_toc_fix-1.0.10.tar.gz.

File metadata

File hashes

Hashes for jupyterlab_markdown_viewer_toc_fix-1.0.10.tar.gz
Algorithm Hash digest
SHA256 6649eabb168e2e5932c22e13870b0be1377718de3668871dc65590e0568fdda0
MD5 eb3c4607c7014aa3bd63fefc9976bbf2
BLAKE2b-256 9a5d8d0d86cdaa66ccb85723ae767fbad751bff3d8724887067aa1a6ccb0cdca

See more details on using hashes here.

File details

Details for the file jupyterlab_markdown_viewer_toc_fix-1.0.10-py3-none-any.whl.

File metadata

File hashes

Hashes for jupyterlab_markdown_viewer_toc_fix-1.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 bb9cd3d73a84490a1a037e5e117a868f322afc52e42090ad262aafd3a1b5ea86
MD5 997567b8dca458cff2bb928206c9f7c1
BLAKE2b-256 5620d009218ac31a1f26959a24c4e9b5ff8c199ae068961b424c970fb4258873

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page