Skip to main content

JupyterLab extension that restores syntax-highlight colours in rendered Markdown fenced code blocks by mounting the CodeMirror highlight StyleModule, so token colours apply even when no editor is open

Project description

jupyterlab_markdown_syntax_rendering_fix

GitHub Actions npm version PyPI version Total PyPI downloads JupyterLab 4 Brought To You By KOLOMOLO Donate PayPal

JupyterLab 4.x extension that restores syntax-highlight colours in rendered Markdown fenced code blocks when no CodeMirror editor is open.

[!WARNING] This extension is a temporary fix for a JupyterLab 4.x behaviour where rendered Markdown code highlighting depends on a CodeMirror editor having been opened. Once JupyterLab core mounts the highlight style for the Markdown renderer independently, this extension will be obsolete and should not be installed.

The Problem

Fenced code blocks in rendered Markdown (the Markdown Preview, README files, .md documents) appear in a single flat colour instead of syntax-highlighted, even though the highlighter clearly ran.

Symptoms:

  • bash, python, json and other fenced blocks render in one uniform grey, not coloured tokens
  • Opening the same file in the editor, then returning to the preview, makes the colours appear - and they stay for the rest of the session
  • Affects any rendered Markdown when the session has not yet opened a CodeMirror editor (notebook cell, file editor, console)

Root cause - the highlight StyleModule is never mounted:

  • JupyterLab's Markdown renderer highlights code through @jupyterlab/codemirror, producing token <span>s with CodeMirror's generated highlight classes (e.g. ͼs, ͼ11)
  • Those class names are emitted by a CodeMirror StyleModule, and the CSS that gives them colour is mounted only when an EditorView is instantiated (via syntaxHighlighting(jupyterHighlightStyle))
  • With only Markdown previews open, no EditorView exists, so the StyleModule is never mounted - the spans carry the right classes but no colour rule, and inherit the plain code text colour
  • Opening any editor mounts the StyleModule document-wide, which is why the workaround of opening the file in the editor fixes the preview

The Fix

The extension mounts the highlight StyleModule's CSS once at startup, achieving the same effect as opening an editor - without one.

How it works:

  • On activation, reads the rules from jupyterHighlightStyle.module (the same StyleModule the Markdown renderer's spans reference) via getRules()
  • Injects them into a single <style> element in the document head, so every rendered Markdown code block is coloured immediately
  • Colours are expressed as --jp-mirror-editor-*-color CSS variables, so they resolve through whatever theme is active and update on theme change
  • Injection is idempotent (fixed element id) and frontend-only - no server component, no per-render DOM observer

Requirements

  • JupyterLab >= 4.0.0

Install

To install the extension, execute:

pip install jupyterlab_markdown_syntax_rendering_fix

Uninstall

To remove the extension, execute:

pip uninstall jupyterlab_markdown_syntax_rendering_fix

Contributing

Development install

Note: You will need NodeJS to build the extension package.

The jlpm command is JupyterLab's pinned version of yarn that is installed with JupyterLab. You may use yarn or npm in lieu of jlpm below.

# Clone the repo to your local environment
# Change directory to the jupyterlab_markdown_syntax_rendering_fix directory

# Set up a virtual environment and install package in development mode
python -m venv .venv
source .venv/bin/activate
pip install --editable "."

# Link your development version of the extension with JupyterLab
jupyter labextension develop . --overwrite

# Rebuild extension TypeScript source after making changes
# IMPORTANT: Unlike the steps above which are performed only once, do this step
# every time you make a change.
jlpm build

You can watch the source directory and run JupyterLab at the same time in different terminals to watch for changes in the extension's source and automatically rebuild the extension.

# Watch the source directory in one terminal, automatically rebuilding when needed
jlpm watch
# Run JupyterLab in another terminal
jupyter lab

Development uninstall

pip uninstall jupyterlab_markdown_syntax_rendering_fix

In development mode, you will also need to remove the symlink created by jupyter labextension develop command. To find its location, you can run jupyter labextension list to figure out where the labextensions folder is located. Then you can remove the symlink named jupyterlab_markdown_syntax_rendering_fix within that folder.

Testing the extension

Frontend tests

This extension is using Jest for JavaScript code testing.

To execute them, execute:

jlpm
jlpm test

Integration tests

This extension uses Playwright for the integration tests (aka user level tests). More precisely, the JupyterLab helper Galata is used to handle testing the extension in JupyterLab.

More information are provided within the ui-tests README.

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

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

File details

Details for the file jupyterlab_markdown_syntax_rendering_fix-1.0.2.tar.gz.

File metadata

File hashes

Hashes for jupyterlab_markdown_syntax_rendering_fix-1.0.2.tar.gz
Algorithm Hash digest
SHA256 01a5b02fb6c1c0e033caf709621963a5f6f4dde3936a5769c5ef6b8c242796db
MD5 b0bece77592bca6b09be68f3b10990a1
BLAKE2b-256 22de0743fcb2be7543a31e36549f3d1985f23fb72860f9b3e27262d02105fd6e

See more details on using hashes here.

File details

Details for the file jupyterlab_markdown_syntax_rendering_fix-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for jupyterlab_markdown_syntax_rendering_fix-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 11ea0db03e19e3c0421c800afcfa6e6de0a626c4aa4a184a04e3d5341c04e54e
MD5 cdfa1e224093c9f419aee6f242001105
BLAKE2b-256 f7d790e609d50e6a47f2b96eb1c9244a7eed482c1798d42363a9f2bb0f3b7573

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