Skip to content

Commit

Permalink
Support es modules in js includes (#2063)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
timkpaine and pre-commit-ci[bot] authored Oct 31, 2023
1 parent a3aed8d commit 0e290e1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
6 changes: 3 additions & 3 deletions nbconvert/exporters/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,10 @@ def resources_include_lab_theme(name):
code = """<style type="text/css">\n%s</style>""" % data
return markupsafe.Markup(code)

def resources_include_js(name):
"""Get the resources include JS for a name."""
def resources_include_js(name, module=False):
"""Get the resources include JS for a name. If module=True, import as ES module"""
env = self.environment
code = """<script>\n%s</script>""" % (env.loader.get_source(env, name)[0])
code = f"""<script {'type="module"' if module else ""}>\n{env.loader.get_source(env, name)[0]}</script>"""
return markupsafe.Markup(code)

def resources_include_url(name):
Expand Down
4 changes: 4 additions & 0 deletions tests/exporters/files/esmodule.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{%- extends 'lab/index.html.j2' -%}
{%- block html_head_js -%}
{{ resources.include_js("esmodule.js", True) }}
{%- endblock html_head_js -%}
2 changes: 2 additions & 0 deletions tests/exporters/files/esmodule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const blerg = true
export { blerg };
9 changes: 9 additions & 0 deletions tests/exporters/test_templateexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,15 @@ def test_local_template_file_extending_lab(self):
output, resources = exporter.from_notebook_node(nb)
assert "UNIQUE" in output

def test_local_template_file_esmodule_js(self):
template_file = os.path.join(self._get_files_path(), "esmodule.html.j2")
exporter = HTMLExporter(template_file=template_file, template_name="lab")
nb = v4.new_notebook()
nb.cells.append(v4.new_code_cell("some_text"))
output, resources = exporter.from_notebook_node(nb)
print(output[:1000])
assert '<script type="module">\nconst blerg = true' in output

def test_raw_template_attr(self):
"""
Verify that you can assign a in memory template string by overwriting
Expand Down

0 comments on commit 0e290e1

Please sign in to comment.