8000 Fix py-markdown plugin (#1008) · WilliamHackspeare/pyscript@9a5bf99 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9a5bf99

Browse files
Fix py-markdown plugin (pyscript#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>
1 parent 1c7cf0b commit 9a5bf99

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

pyscriptjs/environment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ dependencies:
1313
- pre-commit
1414
- pillow
1515
- numpy
16-
16+
- markdown
1717
- pip:
1818
- playwright
1919
- pytest-playwright

pyscriptjs/src/plugins/python/py_markdown.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import html
12
from textwrap import dedent
23

4+
from js import console
35
from markdown import markdown
4-
from pyscript import Plugin, console
6+
from pyscript import Plugin
57

6-
console.warning(
8+
console.warn(
79
"WARNING: This plugin is still in a very experimental phase and will likely change"
810
" and potentially break in the future releases. Use it with caution."
911
)
@@ -26,6 +28,7 @@ def __init__(self, element):
2628
self.element = element
2729

2830
def connect(self):
29-
self.element.innerHTML = markdown(
30-
dedent(self.element.source), extensions=["fenced_code"]
31-
)
31+
unescaped_content = html.unescape(self.element.originalInnerHTML)
32+
original = dedent(unescaped_content)
33+
inner = markdown(original, extensions=["fenced_code"])
34+
self.element.innerHTML = inner

pyscriptjs/tests/integration/test_zz_examples.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,17 @@ def test_folium(self):
176176
zoom_out.click()
177177
self.assert_no_banners()
178178

179+
def test_markdown_plugin(self):
180+
# Given the example page with:
181+
# * <title>PyMarkdown</title>
182+
# * <py-md>#Hello world!</py-md>
183+
self.goto("examples/markdown-plugin.html")
184+
self.wait_for_pyscript()
185+
# ASSERT title is rendered correctly
186+
assert self.page.title() == "PyMarkdown"
187+
# ASSERT markdown is rendered to the corresponding HTML tag
188+
wait_for_render(self.page, "*", "<h1>Hello world!</h1>")
189+
179190
def test_matplotlib(self):
180191
self.goto("examples/matplotlib.html")
181192
self.wait_for_pyscript()

pyscriptjs/tests/py-unit/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,10 @@
44

55
# current working directory
66
base_path = pathlib.Path().absolute()
7+
# add pyscript folder to path
78
python_source = base_path / "src" / "python"
89
sys.path.append(str(python_source))
10+
11+
# add Python plugins folder to path
12+
python_plugins_source = base_path / "src" / "plugins" / "python"
13+
sys.path.append(str(python_plugins_source))
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from unittest.mock import Mock
2+
3+
import py_markdown
4+
5+
6+
class TestPyMarkdown:
7+
def test_plugin_hooks(self, monkeypatch):
8+
console_mock = Mock()
9+
monkeypatch.setattr(py_markdown, "console", console_mock)
10+
config = "just a config"
11+
runtime = "just a runtime"
12+
13+
py_markdown.plugin.configure(config)
14+
console_mock.log.assert_called_with("configuration received: just a config")
15+
16+
py_markdown.plugin.afterStartup(runtime)
17+
console_mock.log.assert_called_with("runtime received: just a runtime")

0 commit comments

Comments
 (0)
0