1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
"""Tests for the Markdown converter."""
from __future__ import annotations
import re
from textwrap import dedent
from typing import TYPE_CHECKING
import pytest
from markdown.extensions.toc import TocExtension
from markdown_exec import MarkdownConfig, markdown_config
if TYPE_CHECKING:
from markdown import Markdown
def test_rendering_nested_blocks(md: Markdown) -> None:
"""Assert nested blocks are properly handled.
Parameters:
md: A Markdown instance (fixture).
"""
html = md.convert(
dedent(
"""
````md exec="1"
```python exec="1"
print("**Bold!**")
```
````
""",
),
)
assert html == "<p><strong>Bold!</strong></p>"
def test_instantiating_config_singleton() -> None:
"""Assert that the Markdown config instances act as a singleton."""
assert MarkdownConfig() is markdown_config
markdown_config.save([], {})
markdown_config.reset()
@pytest.mark.parametrize(
("id", "id_prefix", "expected"),
[
("", None, 'id="exec-\\d+--heading"'),
("", "", 'id="heading"'),
("", "some-prefix-", 'id="some-prefix-heading"'),
("some-id", None, 'id="some-id-heading"'),
("some-id", "", 'id="heading"'),
("some-id", "some-prefix-", 'id="some-prefix-heading"'),
],
)
def test_prefixing_headings(md: Markdown, id: str, id_prefix: str | None, expected: str) -> None: # noqa: A002
"""Assert that we prefix headings as specified.
Parameters:
md: A Markdown instance (fixture).
id: The code block id.
id_prefix: The code block id prefix.
expected: The id we expect to find in the HTML.
"""
TocExtension().extendMarkdown(md)
prefix = f'idprefix="{id_prefix}"' if id_prefix is not None else ""
html = md.convert(
dedent(
f"""
```python exec="1" id="{id}" {prefix}
print("# HEADING")
```
""",
),
)
assert re.search(expected, html)
|