aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps
diff options
context:
space:
mode:
authorGravatar kosayoda <[email protected]>2021-03-24 19:55:16 +0800
committerGravatar kosayoda <[email protected]>2021-03-24 19:55:16 +0800
commit6ccec0d866c44cd7f9789a396ef6ec6cd2cd5df8 (patch)
tree15ffa3caff8b0a625028d1085a3246683c630d07 /pydis_site/apps
parentSimplify pathlib code and specify file encoding. (diff)
Replace `markdown2` with `markdown` and `python-frontmatter`.
This allows us to properly escape codeblocks within markdown, permalink to headers on a page, and decouples getting metadata from a file and getting generated HTML from the Markdown content.
Diffstat (limited to 'pydis_site/apps')
-rw-r--r--pydis_site/apps/content/resources/guides/pydis-guides/how-to-contribute-a-page.md59
-rw-r--r--pydis_site/apps/content/utils.py32
-rw-r--r--pydis_site/apps/content/views/page_category.py15
3 files changed, 81 insertions, 25 deletions
diff --git a/pydis_site/apps/content/resources/guides/pydis-guides/how-to-contribute-a-page.md b/pydis_site/apps/content/resources/guides/pydis-guides/how-to-contribute-a-page.md
index 8967e7da..12969ba2 100644
--- a/pydis_site/apps/content/resources/guides/pydis-guides/how-to-contribute-a-page.md
+++ b/pydis_site/apps/content/resources/guides/pydis-guides/how-to-contribute-a-page.md
@@ -69,18 +69,75 @@ You can learn more about Markdown metadata [here](https://github.com/trentm/pyth
Apart from standard Markdown, certain additions are available:
+### Abbreviations
+HTML `<abbr>` tags can be used in markdown using this format:
+
+**Markdown:**
+```nohighlight
+This website is HTML generated from YAML and Markdown.
+
+*[HTML]: Hyper Text Markup Language
+*[YAML]: YAML Ain't Markup Language
+```
+
+**Output:**
+
+This website is <abbr title="Hyper Text Markup Language">HTML</abbr>
+generated from <abbr title="YAML Ain't Markup Language">YAML</abbr> and Markdown.
+
+---
+
+### Footnotes
+**Markdown:**
+```nohighlight
+This footnote[^1] links to the bottom[^custom_label] of the page[^3].
+
+[^1]: Footnote labels start with a caret `^`.
+[^3]: The footnote link is numbered based on the order of the labels.
+[^custom label]: Footnote labels can contain any text within square brackets.
+```
+
+**Output:**
+
+This footnote[^1] links to the bottom[^custom label] of the page[^3].
+
+[^1]: Footnote labels start with a caret `^`.
+[^3]: The footnote link is numbered based on the order of the labels.
+[^custom label]: Footnote labels can contain any text within square brackets.
+
+---
+
### Tables
+**Markdown:**
+```nohighlight
+| This is header | This is another header |
+| -------------- | ---------------------- |
+| An item | Another item |
+```
+
+**Output:**
+
| This is header | This is another header |
| -------------- | ---------------------- |
| An item | Another item |
-| Big item | Small item |
+---
### Codeblock Syntax Highlighting
Syntax highlighting is provided by `highlight.js`.
To activate syntax highlighting, put the language directly after the starting backticks.
+**Markdown:**
+````nohighlight
+```python
+import os
+
+path = os.path.join("foo", "bar")
+```
+````
+
+**Output:**
```python
import os
diff --git a/pydis_site/apps/content/utils.py b/pydis_site/apps/content/utils.py
index 0faf722c..11d2b792 100644
--- a/pydis_site/apps/content/utils.py
+++ b/pydis_site/apps/content/utils.py
@@ -1,9 +1,11 @@
from pathlib import Path
-from typing import Dict, Union
+from typing import Dict, Tuple
+import frontmatter
+import markdown
import yaml
from django.http import Http404
-from markdown2 import markdown
+from markdown.extensions.toc import TocExtension
def get_category(path: Path) -> Dict[str, str]:
@@ -31,29 +33,25 @@ def get_category_pages(path: Path) -> Dict[str, Dict]:
for item in path.glob("*.md"):
if item.is_file():
- md = markdown(item.read_text(), extras=["metadata"])
- pages[item.stem] = md.metadata
+ pages[item.stem] = frontmatter.load(item)
return pages
-def get_page(path: Path) -> Dict[str, Union[str, Dict]]:
+def get_page(path: Path) -> Tuple[str, Dict]:
"""Get one specific page."""
if not path.is_file():
raise Http404("Page not found.")
- html = markdown(
- path.read_text(encoding="utf-8"),
- extras=[
- "metadata",
- "fenced-code-blocks",
- "highlightjs-lang",
- "header-ids",
- "strike",
- "target-blank-links",
- "tables",
- "task_list"
+ metadata, content = frontmatter.parse(path.read_text(encoding="utf-8"))
+ html = markdown.markdown(
+ content,
+ 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="")
]
)
- return {"content": str(html), "metadata": html.metadata}
+ return str(html), metadata
diff --git a/pydis_site/apps/content/views/page_category.py b/pydis_site/apps/content/views/page_category.py
index 91aed7f0..4a2ed2d6 100644
--- a/pydis_site/apps/content/views/page_category.py
+++ b/pydis_site/apps/content/views/page_category.py
@@ -35,18 +35,19 @@ class PageOrCategoryView(TemplateView):
if self.full_location.is_dir():
context["categories"] = utils.get_categories(self.full_location)
+ context["pages"] = utils.get_category_pages(self.full_location)
+
category = utils.get_category(self.full_location)
- context["category_info"] = category
context["page_title"] = category["name"]
context["page_description"] = category["description"]
- context["pages"] = utils.get_category_pages(self.full_location)
+
context["path"] = f"{self.location}/" # Add trailing slash here to simplify template
elif self.full_location.with_suffix(".md").is_file():
- page_result = utils.get_page(self.full_location.with_suffix(".md"))
- context["page"] = page_result
- context["page_title"] = page_result["metadata"]["title"]
- context["page_description"] = page_result["metadata"]["description"]
- context["relevant_links"] = page_result["metadata"].get("relevant_links", {})
+ page, metadata = utils.get_page(self.full_location.with_suffix(".md"))
+ context["page"] = page
+ context["page_title"] = metadata["title"]
+ context["page_description"] = metadata["description"]
+ context["relevant_links"] = metadata.get("relevant_links", {})
else:
raise Http404