Skip to content

Commit

Permalink
Support es modules in js includes
Browse files Browse the repository at this point in the history
  • Loading branch information
timkpaine committed Oct 31, 2023
1 parent 6bce6da commit e3b7ddb
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 e3b7ddb

Please sign in to comment.