diff --git a/pyscriptjs/environment.yml b/pyscriptjs/environment.yml index f689c179..f65000d0 100644 --- a/pyscriptjs/environment.yml +++ b/pyscriptjs/environment.yml @@ -13,7 +13,7 @@ dependencies: - pre-commit - pillow - numpy - +- markdown - pip: - playwright - pytest-playwright diff --git a/pyscriptjs/src/plugins/python/py_markdown.py b/pyscriptjs/src/plugins/python/py_markdown.py index cf2eda9b..0e373f38 100644 --- a/pyscriptjs/src/plugins/python/py_markdown.py +++ b/pyscriptjs/src/plugins/python/py_markdown.py @@ -1,9 +1,11 @@ +import html from textwrap import dedent +from js import console from markdown import markdown -from pyscript import Plugin, console +from pyscript import Plugin -console.warning( +console.warn( "WARNING: This plugin is still in a very experimental phase and will likely change" " and potentially break in the future releases. Use it with caution." ) @@ -26,6 +28,7 @@ class PyMarkdown: self.element = element def connect(self): - self.element.innerHTML = markdown( - dedent(self.element.source), extensions=["fenced_code"] - ) + unescaped_content = html.unescape(self.element.originalInnerHTML) + original = dedent(unescaped_content) + inner = markdown(original, extensions=["fenced_code"]) + self.element.innerHTML = inner diff --git a/pyscriptjs/tests/integration/test_zz_examples.py b/pyscriptjs/tests/integration/test_zz_examples.py index 9d78e43e..7dbb629f 100644 --- a/pyscriptjs/tests/integration/test_zz_examples.py +++ b/pyscriptjs/tests/integration/test_zz_examples.py @@ -176,6 +176,17 @@ class TestExamples(PyScriptTest): zoom_out.click() self.assert_no_banners() + def test_markdown_plugin(self): + # Given the example page with: + # * PyMarkdown + # * #Hello world! + self.goto("examples/markdown-plugin.html") + self.wait_for_pyscript() + # ASSERT title is rendered correctly + assert self.page.title() == "PyMarkdown" + # ASSERT markdown is rendered to the corresponding HTML tag + wait_for_render(self.page, "*", "

Hello world!

") + def test_matplotlib(self): self.goto("examples/matplotlib.html") self.wait_for_pyscript() diff --git a/pyscriptjs/tests/py-unit/conftest.py b/pyscriptjs/tests/py-unit/conftest.py index bac4bbfd..474c3d73 100644 --- a/pyscriptjs/tests/py-unit/conftest.py +++ b/pyscriptjs/tests/py-unit/conftest.py @@ -4,5 +4,10 @@ import sys # current working directory base_path = pathlib.Path().absolute() +# add pyscript folder to path python_source = base_path / "src" / "python" sys.path.append(str(python_source)) + +# add Python plugins folder to path +python_plugins_source = base_path / "src" / "plugins" / "python" +sys.path.append(str(python_plugins_source)) diff --git a/pyscriptjs/tests/py-unit/test_python_plugins.py b/pyscriptjs/tests/py-unit/test_python_plugins.py new file mode 100644 index 00000000..6b1aafcc --- /dev/null +++ b/pyscriptjs/tests/py-unit/test_python_plugins.py @@ -0,0 +1,17 @@ +from unittest.mock import Mock + +import py_markdown + + +class TestPyMarkdown: + def test_plugin_hooks(self, monkeypatch): + console_mock = Mock() + monkeypatch.setattr(py_markdown, "console", console_mock) + config = "just a config" + runtime = "just a runtime" + + py_markdown.plugin.configure(config) + console_mock.log.assert_called_with("configuration received: just a config") + + py_markdown.plugin.afterStartup(runtime) + console_mock.log.assert_called_with("runtime received: just a runtime")