Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a formatting step using mdformat as part of generate_mkdocs.py #10484

Merged
merged 4 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/requirements-insiders.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ black==23.10.0
mkdocs==1.5.0
mkdocs-material @ git+ssh://git@github.com/astral-sh/mkdocs-material-insiders.git@38c0b8187325c3bab386b666daf3518ac036f2f4
mkdocs-redirects==1.2.1
mdformat==0.7.17
mdformat-mkdocs==2.0.4
mdformat-admon==2.0.2
3 changes: 3 additions & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ black==23.10.0
mkdocs==1.5.0
mkdocs-material==9.1.18
mkdocs-redirects==1.2.1
mdformat==0.7.17
mdformat-mkdocs==2.0.4
mdformat-admon==2.0.2
44 changes: 44 additions & 0 deletions scripts/_mdformat_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import mdformat

if TYPE_CHECKING:
import argparse

from markdown_it import MarkdownIt
from mdformat.renderer import RenderContext, RenderTreeNode


class NoEscapeTextPlugin:
zanieb marked this conversation as resolved.
Show resolved Hide resolved
r"""Overrides the default text formatting behavior of mdformat.

By default mdformat will escape any markdown special character found in a
text block, e.g., <. Some of these characters are found in our
documentation, and when escaped (i.e. \<) will be rendered incorrectly by
mkdocs, i.e., the backslash will appear in the render. Because our only
purpose in using mdformat is to manage the line-breaks, it makes sense to
override its text formatting behavior.
"""

def __init__(self: NoEscapeTextPlugin) -> None:
self.POSTPROCESSORS = {"text": NoEscapeTextPlugin.text}
self.RENDERERS = {}

@staticmethod
def add_cli_options(parser: argparse.ArgumentParser) -> None:
pass

@staticmethod
def update_mdit(mdit: MarkdownIt) -> None:
pass

@staticmethod
def text(_text: str, node: RenderTreeNode, _context: RenderContext) -> str:
return node.content


def add_no_escape_text_plugin() -> None:
"""Add NoEscapeTextPlugin to the list of mdformat extensions."""
mdformat.plugins.PARSER_EXTENSIONS["no-escape-text"] = NoEscapeTextPlugin()
8 changes: 8 additions & 0 deletions scripts/generate_mkdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
from pathlib import Path
from typing import NamedTuple

import mdformat
import yaml

from _mdformat_utils import add_no_escape_text_plugin


class Section(NamedTuple):
"""A section to include in the MkDocs documentation."""
Expand Down Expand Up @@ -140,6 +143,11 @@ def main() -> None:

f.write(clean_file_content(file_content, title))

# Format rules docs
add_no_escape_text_plugin()
for rule_doc in Path("docs/rules").glob("*.md"):
zanieb marked this conversation as resolved.
Show resolved Hide resolved
mdformat.file(rule_doc, extensions=["mkdocs", "admonition", "no-escape-text"])

with Path("mkdocs.template.yml").open(encoding="utf8") as fp:
config = yaml.safe_load(fp)

Expand Down
Loading