aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/content/utils.py
diff options
context:
space:
mode:
authorGravatar kosayoda <[email protected]>2021-03-29 17:51:10 +0800
committerGravatar kosayoda <[email protected]>2021-03-29 17:51:10 +0800
commit69f33290b8bd12b95bb6c620a9a1422a5d11b798 (patch)
treed94133a648ec0b1d92798155bdc7dd50a358d2fe /pydis_site/apps/content/utils.py
parentDocument additional markdown features. (diff)
Allow adding a table of contents to a page.
Diffstat (limited to 'pydis_site/apps/content/utils.py')
-rw-r--r--pydis_site/apps/content/utils.py12
1 files changed, 9 insertions, 3 deletions
diff --git a/pydis_site/apps/content/utils.py b/pydis_site/apps/content/utils.py
index 726c991f..d6886ce2 100644
--- a/pydis_site/apps/content/utils.py
+++ b/pydis_site/apps/content/utils.py
@@ -44,14 +44,20 @@ def get_page(path: Path) -> Tuple[str, Dict]:
raise Http404("Page not found.")
metadata, content = frontmatter.parse(path.read_text(encoding="utf-8"))
- html = markdown.markdown(
- content,
+ toc_depth = metadata.get("toc", 1)
+
+ md = markdown.Markdown(
extensions=[
"extra",
# Empty string for marker to disable text searching for [TOC]
# By using a metadata key instead, we save time on long markdown documents
- TocExtension(title="Table of Contents:", permalink=True, marker="")
+ TocExtension(permalink=True, marker="", toc_depth=toc_depth)
]
)
+ html = md.convert(content)
+
+ # Don't set the TOC if the metadata does not specify one
+ if "toc" in metadata:
+ metadata["toc"] = md.toc
return str(html), metadata