Fix py-markdown plugin (#1008)

* fix wrong console method and unescape the tag content before running markdown on it

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* add markdown plugin example test

* add PyMarkdown minimal test

* remove commented code

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* remove import of console from pyscript

* remove unused imports

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Fabio Pliger <fpliger@anaconda.com>
This commit is contained in:
Fabio Pliger
2022-12-08 10:09:31 -08:00
committed by GitHub
parent 1c7cf0ba7d
commit 9a5bf9918e
5 changed files with 42 additions and 6 deletions

View File

@@ -13,7 +13,7 @@ dependencies:
- pre-commit - pre-commit
- pillow - pillow
- numpy - numpy
- markdown
- pip: - pip:
- playwright - playwright
- pytest-playwright - pytest-playwright

View File

@@ -1,9 +1,11 @@
import html
from textwrap import dedent from textwrap import dedent
from js import console
from markdown import markdown 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" "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." " and potentially break in the future releases. Use it with caution."
) )
@@ -26,6 +28,7 @@ class PyMarkdown:
self.element = element self.element = element
def connect(self): def connect(self):
self.element.innerHTML = markdown( unescaped_content = html.unescape(self.element.originalInnerHTML)
dedent(self.element.source), extensions=["fenced_code"] original = dedent(unescaped_content)
) inner = markdown(original, extensions=["fenced_code"])
self.element.innerHTML = inner

View File

@@ -176,6 +176,17 @@ class TestExamples(PyScriptTest):
zoom_out.click() zoom_out.click()
self.assert_no_banners() self.assert_no_banners()
def test_markdown_plugin(self):
# Given the example page with:
# * <title>PyMarkdown</title>
# * <py-md>#Hello world!</py-md>
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, "*", "<h1>Hello world!</h1>")
def test_matplotlib(self): def test_matplotlib(self):
self.goto("examples/matplotlib.html") self.goto("examples/matplotlib.html")
self.wait_for_pyscript() self.wait_for_pyscript()

View File

@@ -4,5 +4,10 @@ import sys
# current working directory # current working directory
base_path = pathlib.Path().absolute() base_path = pathlib.Path().absolute()
# add pyscript folder to path
python_source = base_path / "src" / "python" python_source = base_path / "src" / "python"
sys.path.append(str(python_source)) 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))

View File

@@ -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")