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.8.tar.gz (251.1 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.8.tar.gz.

File metadata

File hashes

Hashes for jupyterlab_markdown_viewer_toc_fix-1.0.8.tar.gz
Algorithm Hash digest
SHA256 0adab6098957daa6262769ddbc4063c3c117dad9f00b7116e57e283be38053ed
MD5 9267b891f5d34586f481282a52fcfd27
BLAKE2b-256 e5f8a1b54e4a41c45118ff1ea7568832097a7e65d3389c93f7c26b7cb62718c8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jupyterlab_markdown_viewer_toc_fix-1.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 db15ae38df6135d1077b558667b831ee0b2613482155938606a9729fb8b11814
MD5 7fc8ccfb8d11251161862bff284088ca
BLAKE2b-256 3c24dfe2c5c82249880934328ca46c08b7ea769aaf23d763e0c4ed12cc9a13ca

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