Skip to content

Commit 87db94d

Browse files
Add a toggle to skip the svg encoding of images (#2106)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 392f1e9 commit 87db94d

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

CHANGELOG.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
### Enhancements made
1010

11-
- Support configureable width and height of reveal presentations [#2104](https://github.com/jupyter/nbconvert/pull/2104) ([@franzhaas](https://github.com/franzhaas))
11+
- Support configurable width and height of reveal presentations [#2104](https://github.com/jupyter/nbconvert/pull/2104) ([@franzhaas](https://github.com/franzhaas))
1212

1313
### Maintenance and upkeep improvements
1414

@@ -440,7 +440,7 @@
440440

441441
### Maintenance and upkeep improvements
442442

443-
- Clean up license [#1949](https://github.com/jupyter/nbconvert/pull/1949) ([@dcsaba89](https://github.com/dcsaba89))
443+
- Clean up license [#1949](https://github.com/jupyter/nbconvert/pull/1949) ([@dcsaba89](https://github.com/dcsaba89))
444444
- Add more linting [#1943](https://github.com/jupyter/nbconvert/pull/1943) ([@blink1073](https://github.com/blink1073))
445445

446446
### Contributors to this release
@@ -564,7 +564,7 @@
564564

565565
### Bugs fixed
566566

567-
- clean_html: allow SVG tags and SVG attributes [#1890](https://github.com/jupyter/nbconvert/pull/1890) ([@akx](https://github.com/akx))
567+
- clean_html: allow SVG tags and SVG attributes [#1890](https://github.com/jupyter/nbconvert/pull/1890) ([@akx](https://github.com/akx))
568568

569569
### Maintenance and upkeep improvements
570570

@@ -1502,6 +1502,7 @@ raw template
15021502
{%- endblock in_prompt -%}
15031503
"""
15041504

1505+
15051506
exporter_attr = AttrExporter()
15061507
output_attr, _ = exporter_attr.from_notebook_node(nb)
15071508
assert "raw template" in output_attr

nbconvert/exporters/html.py

+6
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ def _template_name_default(self):
172172
),
173173
).tag(config=True)
174174

175+
skip_svg_encoding = Bool(
176+
False,
177+
help=("Whether the svg to image data attribute encoding should occur"),
178+
).tag(config=True)
179+
175180
embed_images = Bool(
176181
False, help="Whether or not to embed images as base64 in markdown cells."
177182
).tag(config=True)
@@ -352,4 +357,5 @@ def resources_include_url(name):
352357
resources["html_manager_semver_range"] = self.html_manager_semver_range
353358
resources["should_sanitize_html"] = self.sanitize_html
354359
resources["language_code"] = self.language_code
360+
resources["should_not_encode_svg"] = self.skip_svg_encoding
355361
return resources

share/templates/classic/base.html.j2

+5-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ unknown type {{ cell.type }}
132132
{%- if output.svg_filename %}
133133
<img src="{{ output.svg_filename | posix_path | escape_html }}">
134134
{%- else %}
135-
<img src="data:image/svg+xml;base64,{{ output.data['image/svg+xml'] | text_base64 | escape_html }}">
135+
{%- if resources.should_not_encode_svg %}
136+
{{ output.data['image/svg+xml'].encode("utf-8") | clean_html }}
137+
{%- else %}
138+
<img src="data:image/svg+xml;base64,{{ output.data['image/svg+xml'] | text_base64 | escape_html }}">
139+
{%- endif %}
136140
{%- endif %}
137141
</div>
138142
{%- endblock data_svg %}

share/templates/lab/base.html.j2

+5-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,11 @@ unknown type {{ cell.type }}
164164
{%- if output.svg_filename %}
165165
<img src="{{ output.svg_filename | posix_path | escape_html }}">
166166
{%- else %}
167-
<img src="data:image/svg+xml;base64,{{ output.data['image/svg+xml'] | text_base64 | escape_html }}">
167+
{%- if resources.should_not_encode_svg %}
168+
{{ output.data['image/svg+xml'].encode("utf-8") | clean_html }}
169+
{%- else %}
170+
<img src="data:image/svg+xml;base64,{{ output.data['image/svg+xml'] | text_base64 | escape_html }}">
171+
{%- endif %}
168172
{%- endif %}
169173
</div>
170174
{%- endblock data_svg %}

0 commit comments

Comments
 (0)