Fix Markdown errors in docs (#3187)

This commit is contained in:
Jonathan Plasse 2023-02-24 19:06:48 +01:00 committed by GitHub
parent 2c692e3acf
commit 39b9a1637f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 59 additions and 33 deletions

View file

@ -34,6 +34,32 @@ FATHOM_SCRIPT: str = (
)
def clean_file_content(content: str, title: str) -> str:
"""Add missing title, fix the headers depth, and remove trailing empty lines."""
lines = content.splitlines()
if lines[0].startswith("# "):
return content
in_code_block = False
for i, line in enumerate(lines):
if line.startswith("```"):
in_code_block = not in_code_block
if not in_code_block and line.startswith("#"):
lines[i] = line[1:]
# Remove trailing empty lines.
for line in reversed(lines):
if line == "":
del lines[-1]
else:
break
content = "\n".join(lines) + "\n"
# Add a missing title.
return f"# {title}\n\n" + content
def main() -> None:
"""Generate an MkDocs-compatible `docs` and `mkdocs.yml`."""
@ -59,33 +85,29 @@ def main() -> None:
continue
if filename == "settings.md":
f.write(
subprocess.check_output(
["cargo", "dev", "generate-options"],
encoding="utf-8",
),
file_content = subprocess.check_output(
["cargo", "dev", "generate-options"],
encoding="utf-8",
)
continue
else:
block = content.split(f"<!-- Begin section: {title} -->\n\n")
if len(block) != 2:
msg = f"Section {title} not found in README.md"
raise ValueError(msg)
block = content.split(f"<!-- Begin section: {title} -->")
if len(block) != 2:
msg = f"Section {title} not found in README.md"
raise ValueError(msg)
block = block[1].split(f"\n<!-- End section: {title} -->")
if len(block) != 2:
msg = f"Section {title} not found in README.md"
raise ValueError(msg)
block = block[1].split(f"<!-- End section: {title} -->")
if len(block) != 2:
msg = f"Section {title} not found in README.md"
raise ValueError(msg)
file_content = block[0]
f.write(block[0])
if filename == "rules.md":
f.write(
subprocess.check_output(
if filename == "rules.md":
file_content += "\n" + subprocess.check_output(
["cargo", "dev", "generate-rules-table"],
encoding="utf-8",
),
)
)
f.write(clean_file_content(file_content, title))
# Add the nav section to mkdocs.yml.
with Path("mkdocs.template.yml").open(encoding="utf8") as fp: