55import sys
66
77from jinja2 import Environment , FileSystemLoader
8- from markdown import markdown
8+ from markdown import Markdown
99import yaml
1010
1111import i18n
@@ -103,6 +103,15 @@ def format_markdown(file_path):
103103 dashed_header_format = first_line == "---\n "
104104
105105 with open (file_path , "r" , encoding = "utf-8" ) as markdown_content :
106+
107+ md = Markdown (
108+ extensions = ["tables" , "fenced_code" , "codehilite" , "toc" , "sane_lists" ],
109+ extension_configs = {
110+ "codehilite" : {"css_class" : "highlight" },
111+ "toc" : {"anchorlink" : True , "anchorlink_class" : "headerlink" },
112+ },
113+ )
114+
106115 if dashed_header_format :
107116 headerless = []
108117 header_block_open = False
@@ -111,22 +120,12 @@ def format_markdown(file_path):
111120 header_block_open = not header_block_open
112121 elif not header_block_open :
113122 headerless .append (line )
114- return markdown (
115- "" .join (headerless ),
116- extensions = ["tables" , "fenced_code" , "codehilite" , "toc" , "sane_lists" ],
117- extension_configs = {
118- "codehilite" : {"css_class" : "highlight" },
119- "toc" : {"anchorlink" : True , "anchorlink_class" : "headerlink" },
120- },
121- )
122- return markdown (
123- "" .join (markdown_content .readlines ()[3 :]),
124- extensions = ["tables" , "fenced_code" , "codehilite" , "toc" , "sane_lists" ],
125- extension_configs = {
126- "codehilite" : {"css_class" : "highlight" },
127- "toc" : {"anchorlink" : True , "anchorlink_class" : "headerlink" },
128- },
129- )
123+
124+ body = md .convert ("" .join (headerless ))
125+ return {"body" : body , "toc" : md .toc_tokens }
126+
127+ body = md .convert ("" .join (markdown_content .readlines ()[3 :]))
128+ return {"body" : body , "toc" : md .toc }
130129
131130
132131if __name__ == "__main__" :
@@ -168,7 +167,9 @@ def format_markdown(file_path):
168167 f"{ LOCALIZED_SITE_PATH } { output_path } " , "w+" , encoding = "utf-8"
169168 ) as render_file :
170169 data = get_site_data (SLUG , locale ["code" ], content_path , version )
171- data ["content" ] = format_markdown (content_path )
170+ formatted_md = format_markdown (content_path )
171+ data ["content" ] = formatted_md ["body" ]
172+ data ["toc" ] = formatted_md ["toc" ]
172173 data ["path" ] = (
173174 f"/{ SLUG } { output_path } "
174175 if not version
0 commit comments