Jupyterlab extension to fix issues with TOC navigation in Markdown Viewer in Jupyterlab
Project description
jupyterlab_markdown_viewer_toc_fix
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#sectionfails to scroll to target - Console shows "Heading element not found" errors
Root Cause - Attribute Selector Mismatch:
- JupyterLab's HTML sanitizer removes
idattributes for XSS protection - Headings get
data-jupyter-idattributes instead (default settingallowNamedProperties = false) - Navigation code queries for
idattributes that don't exist querySelector()returnsnulland scroll attempts fail silently- Example:
## Introductionrenders 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
activeHeadingChangedsignal - Uses conditional attribute selector (
data-jupyter-idvsid) based onsanitizer.allowNamedPropertiessetting - 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
#idand[data-jupyter-id]selectors with fallback chain - Handles JupyterLab router
routedsignal for in-app navigation - Listens to global
hashchangeevents for URL-based navigation
Case-Insensitive Fallback:
- Loops through all headings with
data-jupyter-idoridattributes - 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jupyterlab_markdown_viewer_toc_fix-1.0.10.tar.gz.
File metadata
- Download URL: jupyterlab_markdown_viewer_toc_fix-1.0.10.tar.gz
- Upload date:
- Size: 251.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6649eabb168e2e5932c22e13870b0be1377718de3668871dc65590e0568fdda0
|
|
| MD5 |
eb3c4607c7014aa3bd63fefc9976bbf2
|
|
| BLAKE2b-256 |
9a5d8d0d86cdaa66ccb85723ae767fbad751bff3d8724887067aa1a6ccb0cdca
|
File details
Details for the file jupyterlab_markdown_viewer_toc_fix-1.0.10-py3-none-any.whl.
File metadata
- Download URL: jupyterlab_markdown_viewer_toc_fix-1.0.10-py3-none-any.whl
- Upload date:
- Size: 18.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb9cd3d73a84490a1a037e5e117a868f322afc52e42090ad262aafd3a1b5ea86
|
|
| MD5 |
997567b8dca458cff2bb928206c9f7c1
|
|
| BLAKE2b-256 |
5620d009218ac31a1f26959a24c4e9b5ff8c199ae068961b424c970fb4258873
|