8
8
#############################################################################
9
9
10
10
import mimetypes
11
+ from typing import Optional
11
12
12
13
import traitlets
13
14
from traitlets .config import Config
20
21
from nbconvert .exporters .html import HTMLExporter
21
22
from nbconvert .exporters .templateexporter import TemplateExporter
22
23
from nbconvert .filters .highlight import Highlight2HTML
23
- from nbconvert .filters .markdown_mistune import IPythonRenderer , MarkdownWithMath
24
-
24
+ from nbconvert .filters .markdown_mistune import (
25
+ IPythonRenderer ,
26
+ MarkdownWithMath ,
27
+ )
25
28
from .static_file_handler import TemplateStaticFileHandler
26
29
from .utils import create_include_assets_functions
27
30
31
+ try :
32
+ from nbconvert .filters .markdown_mistune import MISTUNE_V3 # noqa
33
+
34
+ NB_CONVERT_760 = True
35
+ except ImportError :
36
+ NB_CONVERT_760 = False
37
+
28
38
29
39
class VoilaMarkdownRenderer (IPythonRenderer ):
30
40
"""Custom markdown renderer that inlines images"""
@@ -33,14 +43,20 @@ def __init__(self, contents_manager, *args, **kwargs):
33
43
self .contents_manager = contents_manager
34
44
super ().__init__ (* args , ** kwargs )
35
45
36
- def image (self , src , title , text ):
46
+ def image (self , text : str , url : str , title : Optional [ str ] = None ):
37
47
contents_manager = self .contents_manager
48
+ # for nbconvert <7.6.0, the first argument is the URL
49
+ src = url if NB_CONVERT_760 else text
50
+
38
51
if contents_manager .file_exists (src ):
39
52
content = contents_manager .get (src , format = "base64" )
40
53
data = content ["content" ].replace ("\n " , "" ) # remove the newline
41
54
mime_type , encoding = mimetypes .guess_type (src )
42
55
src = f"data:{ mime_type } ;base64,{ data } "
43
- return super ().image (src , title , text )
56
+ if NB_CONVERT_760 :
57
+ return super ().image (text , src , title )
58
+ else :
59
+ return super ().image (src , url , title )
44
60
45
61
46
62
class VoilaExporter (HTMLExporter ):
0 commit comments