Skip to content

Commit 6e28114

Browse files
authored
Merge branch 'main' into patch-1
2 parents 4f8eb61 + 18e10f6 commit 6e28114

File tree

3 files changed

+50
-13
lines changed

3 files changed

+50
-13
lines changed

.github/workflows/tests.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,28 @@ jobs:
114114
PIP_CONSTRAINT= hatch env run -e test -- pip install 'pip>=24.2'
115115
xvfb-run --auto-servernum hatch run test:nowarn || xvfb-run --auto-servernum hatch run test:nowarn --lf
116116
117+
test_mistune_30:
118+
name: Test Mistune 3.0
119+
timeout-minutes: 20
120+
runs-on: ubuntu-latest
121+
steps:
122+
- uses: actions/checkout@v4
123+
- name: Base Setup
124+
uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
125+
- name: Install dependencies
126+
run: |
127+
sudo apt-get update
128+
sudo apt-get install texlive-plain-generic inkscape texlive-xetex latexmk
129+
sudo apt-get install xvfb x11-utils libxkbcommon-x11-0 libxcb-xinerama0 python3-pyqt5
130+
131+
# pandoc is not up to date in the ubuntu repos, so we install directly
132+
wget https://github.com/jgm/pandoc/releases/download/2.9.2.1/pandoc-2.9.2.1-1-amd64.deb && sudo dpkg -i pandoc-2.9.2.1-1-amd64.deb
133+
134+
- name: Run tests
135+
run: |
136+
hatch env run -e test -- pip install 'mistune~=3.0.0'
137+
xvfb-run --auto-servernum hatch run test:nowarn || xvfb-run --auto-servernum hatch run test:nowarn --lf
138+
117139
test_prereleases:
118140
name: Test Prereleases
119141
runs-on: ubuntu-latest

docs/source/nbconvert_library.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@
486486
"cell_type": "markdown",
487487
"metadata": {},
488488
"source": [
489-
"@damianavila wrote the Nikola Plugin to [write blog post as Notebooks](http://damianavila.github.io/blog/posts/one-line-deployment-of-your-site-to-gh-pages.html) and is developing a js-extension to publish notebooks via one click from the web app."
489+
"@damianavila wrote the Nikola Plugin to [write blog post as Notebooks](https://damianavila.github.io/blog/posts/one-line-deployment-of-your-site-to-gh-pages) and is developing a js-extension to publish notebooks via one click from the web app."
490490
]
491491
}
492492
],

nbconvert/filters/markdown_mistune.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import mimetypes
1010
import os
1111
from html import escape
12-
from typing import Any, Callable, Dict, Iterable, Match, Optional, Tuple
12+
from typing import TYPE_CHECKING, Any, ClassVar, Dict, Iterable, Match, Optional, Protocol, Tuple
1313

1414
import bs4
1515
from pygments import highlight
@@ -20,6 +20,19 @@
2020

2121
from nbconvert.filters.strings import add_anchor
2222

23+
if TYPE_CHECKING:
24+
try:
25+
from mistune.plugins import Plugin
26+
except ImportError:
27+
28+
class Plugin(Protocol): # type: ignore[no-redef]
29+
"""Mistune plugin interface."""
30+
31+
def __call__(self, markdown: "Markdown") -> None:
32+
"""Apply the plugin on the markdown document."""
33+
...
34+
35+
2336
try: # for Mistune >= 3.0
2437
from mistune import ( # type:ignore[attr-defined]
2538
BlockParser,
@@ -32,6 +45,7 @@
3245
)
3346

3447
MISTUNE_V3 = True
48+
MISTUNE_V3_ATX = "atx_heading" in BlockParser.SPECIFICATION
3549

3650
except ImportError: # for Mistune >= 2.0
3751
import re
@@ -45,8 +59,9 @@
4559
)
4660

4761
MISTUNE_V3 = False
62+
MISTUNE_V3_ATX = False
4863

49-
def import_plugin(name: str) -> "MarkdownPlugin": # type: ignore[misc]
64+
def import_plugin(name: str) -> "Plugin": # type: ignore[misc]
5065
"""Simple implementation of Mistune V3's import_plugin for V2."""
5166
return PLUGINS[name] # type: ignore[no-any-return]
5267

@@ -75,8 +90,10 @@ class MathBlockParser(BlockParser):
7590
is ignored here.
7691
"""
7792

78-
AXT_HEADING_WITHOUT_LEADING_SPACES = (
79-
r"^ {0,3}(?P<axt_1>#{1,6})(?!#+)(?P<axt_2>[ \t]*(.*?)?)$"
93+
ATX_HEADING_WITHOUT_LEADING_SPACES = (
94+
r"^ {0,3}(?P<atx_1>#{1,6})(?!#+)(?P<atx_2>[ \t]*(.*?)?)$"
95+
if MISTUNE_V3_ATX
96+
else r"^ {0,3}(?P<axt_1>#{1,6})(?!#+)(?P<axt_2>[ \t]*(.*?)?)$"
8097
)
8198

8299
MULTILINE_MATH = _dotall(
@@ -92,12 +109,14 @@ class MathBlockParser(BlockParser):
92109

93110
SPECIFICATION = {
94111
**BlockParser.SPECIFICATION,
95-
"axt_heading": AXT_HEADING_WITHOUT_LEADING_SPACES,
112+
(
113+
"atx_heading" if MISTUNE_V3_ATX else "axt_heading"
114+
): ATX_HEADING_WITHOUT_LEADING_SPACES,
96115
"multiline_math": MULTILINE_MATH,
97116
}
98117

99118
# Multiline math must be searched before other rules
100-
DEFAULT_RULES: Tuple[str, ...] = ("multiline_math", *BlockParser.DEFAULT_RULES) # type: ignore[assignment]
119+
DEFAULT_RULES: ClassVar[Iterable[str]] = ("multiline_math", *BlockParser.DEFAULT_RULES) # type: ignore[assignment]
101120

102121
def parse_multiline_math(self, m: Match[str], state: BlockState) -> int:
103122
"""Send mutiline math as a single paragraph to MathInlineParser."""
@@ -139,7 +158,7 @@ class MathInlineParser(InlineParser):
139158
}
140159

141160
# Block math must be matched first, and all math must come before text
142-
DEFAULT_RULES: Tuple[str, ...] = (
161+
DEFAULT_RULES: ClassVar[Iterable[str]] = (
143162
"block_math_tex",
144163
"block_math_latex",
145164
"inline_math_tex",
@@ -442,10 +461,6 @@ def _html_embed_images(self, html: str) -> str:
442461
return str(parsed_html)
443462

444463

445-
# Represents an already imported plugin for Mistune
446-
MarkdownPlugin = Callable[[Markdown], None]
447-
448-
449464
class MarkdownWithMath(Markdown):
450465
"""Markdown text with math enabled."""
451466

@@ -464,7 +479,7 @@ def __init__(
464479
renderer: HTMLRenderer,
465480
block: Optional[BlockParser] = None,
466481
inline: Optional[InlineParser] = None,
467-
plugins: Optional[Iterable[MarkdownPlugin]] = None,
482+
plugins: Optional[Iterable["Plugin"]] = None,
468483
):
469484
"""Initialize the parser."""
470485
if block is None:

0 commit comments

Comments
 (0)