Add support for tables of contents in articles

toc-extension
Lucidiot 2021-01-31 21:49:37 +01:00
parent fbe572c5f4
commit daa67656a4
Signed by: lucidiot
GPG Key ID: 3358C1CA6906FB8D
1 changed files with 4 additions and 1 deletions

View File

@ -5,6 +5,7 @@ from shutil import copy
from typing import Optional, Callable from typing import Optional, Callable
from markdown import markdown from markdown import markdown
from markdown.extensions.toc import TocExtension
DOUBLE_NEWLINE_RE = re.compile(r'\n\n', flags=re.MULTILINE|re.DOTALL) DOUBLE_NEWLINE_RE = re.compile(r'\n\n', flags=re.MULTILINE|re.DOTALL)
HEADER_TITLE_RE = re.compile(r'<h([12])>(.*?)</h\1>') HEADER_TITLE_RE = re.compile(r'<h([12])>(.*?)</h\1>')
@ -189,7 +190,9 @@ def compile_markdown(file_path:str) -> str:
"""Given a string of markdown, compiles it and returns the result.""" """Given a string of markdown, compiles it and returns the result."""
return markdown( return markdown(
slurp(file_path), slurp(file_path),
output_format='html5') output_format='html5',
# Allow tables of contents and exclude <h1> tags (page titles)
extensions=[TocExtension(toc_depth='2-6')])
def compile_plaintext(file_path:str) -> str: def compile_plaintext(file_path:str) -> str:
output = '<p>\n' output = '<p>\n'