"""Regression coverage for the browser markdown renderer.""" import json import shutil import subprocess import textwrap from pathlib import Path import pytest _REPO = Path(__file__).resolve().parent.parent _HAS_NODE = shutil.which("node") is not None @pytest.fixture(scope="module") def node_available(): if not _HAS_NODE: pytest.skip("node binary not on PATH") def _run_markdown_case(markdown: str, render_expr: str = "mod.mdToHtml(input)", with_katex: bool = False): script = textwrap.dedent( r""" import fs from 'node:fs'; globalThis.window = { location: { origin: 'http://localhost' }, katex: null }; if (__WITH_KATEX__) { // Minimal stand-in for the CDN katex global: wraps the source so tests // can assert what was (or wasn't) handed to KaTeX. const katexStub = { renderToString(src, opts) { const display = !!(opts && opts.displayMode); return `${src}`; }, }; globalThis.window.katex = katexStub; globalThis.katex = katexStub; } globalThis.document = { readyState: 'loading', addEventListener() {}, createElement(tag) { if (tag !== 'template') throw new Error(`unsupported element: ${tag}`); return { _html: '', content: { querySelectorAll() { return []; } }, set innerHTML(value) { this._html = value; }, get innerHTML() { return this._html; }, }; }, }; globalThis.MutationObserver = class { observe() {} }; let source = fs.readFileSync('./static/js/markdown.js', 'utf8'); source = source.replace( /import uiModule from ['"]\.\/ui\.js['"];/, '' ); source = source.replace( /import \{ splitTableRow \} from ['"]\.\/markdown\/tableRow\.js['"];/, `function splitTableRow(row) { return (row || '').replace(/^\\s*\\|/, '').replace(/\\|\\s*$/, '').split('|').map(c => c.trim()); }` ); // markdown.js imports the emoji-shortcode helpers relatively (issue #345), // which a data: URL module can't resolve. Inline the REAL helpers (minus // their export keywords) so the renderer's shortcode pass behaves exactly // as it does in the browser. const emojiSource = fs.readFileSync('./static/js/emojiShortcodes.js', 'utf8') .replace(/^export default .*$/m, '') .replace(/export const /g, 'const ') .replace(/export function /g, 'function '); source = source.replace( /import \{ replaceEmojiShortcodes, hasEmojiShortcode \} from ['"]\.\/emojiShortcodes\.js['"];/, () => emojiSource ); source = source.replace( /var escapeHtml = uiModule\.esc;/, `var escapeHtml = (value) => String(value ?? '') .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"') .replace(/'/g, ''');` ); const moduleUrl = 'data:text/javascript;base64,' + Buffer.from(source).toString('base64'); const mod = await import(moduleUrl); const input = JSON.parse(process.argv[1]); console.log(JSON.stringify({ html: __RENDER_EXPR__ })); """ ).replace("__RENDER_EXPR__", render_expr).replace( "__WITH_KATEX__", "true" if with_katex else "false" ) result = subprocess.run( ["node", "--input-type=module", "-e", script, json.dumps(markdown)], cwd=_REPO, capture_output=True, timeout=15, text=True, ) if result.returncode != 0: raise AssertionError(f"node failed:\nSTDERR:\n{result.stderr}\nSTDOUT:\n{result.stdout}") return json.loads(result.stdout.splitlines()[-1])["html"] def test_ordered_lists_render_as_one_unwrapped_ol(node_available): html = _run_markdown_case( "Before\n\n" "1. **Check against the home page** — that's the visual reference for how things should feel.\n" "2. **Open DevTools** and inspect the element — check fonts, colors, and spacing against this guide.\n" "3. **Flag it** — note the page, the section, what's wrong, and what CSS rule you suspect.\n" "4. **Small fixes** — if you know the fix (e.g. wrong CSS variable, wrong font), go ahead and change it in the CSS Module file.\n" "5. **Big changes** — Talk it through before making wide changes across many pages.\n\n" "After" ) assert html.count("
Before
" in html assert "After
" in html def test_table_separator_row_not_rendered_as_data(node_available): html = _run_markdown_case("| A | B |\n|---|---|\n| 1 | 2 |") assert html.count("$j = irm http://127.0.0.1:3000/x" in html
assert "___ALLOWED_HTML_" not in html
assert "irm" in html
assert 'href="https://example.com/page"' in html
def test_inline_code_content_is_html_escaped(node_available):
# Inline code is now extracted before the global escape pass, so it must be
# escaped at extraction time (matching the fenced-code-block handling).
html = _run_markdown_case("Render `$1 & 'q'` literally.")
assert "<b>$1 & 'q'</b>" in html
assert "" not in html
def test_currency_dollar_amounts_are_not_rendered_as_math(node_available):
# "$5 to $10" used to pair the two dollar signs as inline-math delimiters
# and render "5 to" through KaTeX. Pandoc-style rules now reject it: the
# closing $ is preceded by a space and followed by a digit.
html = _run_markdown_case(
"The price rose from $5 to $10 overnight.", with_katex=True
)
assert 'class="katex"' not in html
assert "$5" in html
assert "$10" in html
def test_inline_math_still_renders_through_katex(node_available):
html = _run_markdown_case("Pythagoras: $x^2 + y^2 = z^2$ holds.", with_katex=True)
assert 'x^2 + y^2 = z^2' in html
assert "$" not in html
def test_display_math_still_renders_through_katex(node_available):
html = _run_markdown_case("$$\\frac{a}{b}$$", with_katex=True)
assert 'data-display="true"' in html
assert "$$" not in html
def test_dotted_python_import_paths_are_not_autolinked(node_available):
html = _run_markdown_case(
"from imblearn.combine import SMOTETomek\n"
"from sklearn.metrics import f1_score\n"
"from sklearn.compose import ColumnTransformer\n\n"
"See example.com/docs for normal domain autolinking."
)
assert "___ALLOWED_HTML_" not in html
assert "imblearn.combine" in html
assert "sklearn.metrics" in html
assert "sklearn.compose" in html
assert 'href="https://imblearn.com' not in html
assert 'href="https://sklearn.me' not in html
assert 'href="https://example.com/docs"' in html