Skip to content
Permalink
Browse files

fix: Don't mutate the original Markdown config for permalinks

This was a regression from #203.
This ended up disabling permalinks outright for all docs.
  • Loading branch information
oprypin committed Jan 3, 2021
1 parent 3ee6bf1 commit 8f6b163b50551da22f65e9b736e042562f77f2d7
Showing with 44 additions and 7 deletions.
  1. +4 −1 src/mkdocstrings/handlers/base.py
  2. +40 −6 tests/test_extension.py
@@ -232,7 +232,10 @@ def update_env(self, md: Markdown, config: dict) -> None:
# Prevent a bug that happens due to treeprocessors running on the same fragment both as the inner doc and as
# part of the re-integrated doc. Namely, the permalink '¶' would be appended twice. This is the only known
# non-idempotent effect of an extension, so specifically prevent it on the inner doc.
configs.setdefault("toc", {})["permalink"] = False
try:
configs["toc"] = dict(configs["toc"], permalink=False)
except KeyError:
pass

md = Markdown(extensions=extensions, extension_configs=configs)

@@ -1,7 +1,9 @@
"""Tests for the extension module."""
import copy
from contextlib import contextmanager
from textwrap import dedent

import pytest
from markdown import Markdown

from mkdocstrings.extension import MkdocstringsExtension
@@ -25,11 +27,18 @@ def ext_markdown(**kwargs):
"mkdocstrings": {"default_handler": "python", "custom_templates": None, "watch": [], "handlers": {}},
}
config.update(kwargs)
original_config = copy.deepcopy(config)

handlers = Handlers(config)
config["mdx"].append(MkdocstringsExtension(config, handlers))
extension = MkdocstringsExtension(config, handlers)
config["mdx"].append(extension)
original_config["mdx"].append(extension)

yield Markdown(extensions=config["mdx"], extension_configs=config["mdx_configs"])
handlers.teardown()

assert config == original_config # Inadvertent mutations would propagate to the outer doc!


def test_render_html_escaped_sequences():
"""Assert HTML-escaped sequences are correctly parsed as XML."""
@@ -86,8 +95,33 @@ def test_reference_inside_autodoc():
assert snippet in output


def test_no_double_toc():
"""Asserts that the 'toc' extension doesn't apply its modification twice."""
with ext_markdown(mdx=["toc"], mdx_configs={"toc": {"permalink": "@@@"}}) as md:
output = md.convert("::: tests.fixtures.headings")
assert 3 <= output.count("@@@") < 6
@pytest.mark.parametrize(
("permalink_setting", "expect_permalink"),
[
("@@@", "@@@"),
(True, "&para;"),
],
)
def test_no_double_toc(permalink_setting, expect_permalink):
"""
Assert that the 'toc' extension doesn't apply its modification twice.
Arguments:
permalink_setting: The 'permalink' setting of 'toc' extension.
expect_permalink: Text of the permalink to search for in the output.
"""
with ext_markdown(mdx=["toc"], mdx_configs={"toc": {"permalink": permalink_setting}}) as md:
output = md.convert(
dedent(
"""
# aa
::: tests.fixtures.headings
rendering:
show_root_toc_entry: false
# bb
"""
)
)
assert output.count(expect_permalink) == 5

0 comments on commit 8f6b163

Please sign in to comment.