Browse Source

- Generate anchor link ids from headlines

Christoph Trappe 3 years ago
parent
commit
7f981a5667
1 changed files with 6 additions and 0 deletions
  1. 6 0
      main.py

+ 6 - 0
main.py

@@ -19,6 +19,9 @@ page_stack[0].head_html += docutils.core.publish_parts('', writer_name='html')['
 def example(content: Union[Element, str]):
 def example(content: Union[Element, str]):
     callFrame = inspect.currentframe().f_back.f_back
     callFrame = inspect.currentframe().f_back.f_back
     begin = callFrame.f_lineno
     begin = callFrame.f_lineno
+    def generateIdFromHeadline(html):
+        match = re.search(r'<h3.*?>(.*?)</h3>', html)
+        return re.sub('[^(a-z)(A-Z)(0-9)._-]', '', match.groups()[0].strip()) if match else ''
     with ui.row().classes('flex w-full'):
     with ui.row().classes('flex w-full'):
         if isinstance(content, str):
         if isinstance(content, str):
             ui.markdown(content).classes('mr-8 w-4/12')
             ui.markdown(content).classes('mr-8 w-4/12')
@@ -28,6 +31,9 @@ def example(content: Union[Element, str]):
                 html = docutils.core.publish_parts(doc, writer_name='html')['html_body']
                 html = docutils.core.publish_parts(doc, writer_name='html')['html_body']
                 html = html.replace('<p>', '<h3>', 1)
                 html = html.replace('<p>', '<h3>', 1)
                 html = html.replace('</p>', '</h3>', 1)
                 html = html.replace('</p>', '</h3>', 1)
+                headline_id = generateIdFromHeadline(html)
+                if headline_id:
+                    html = html.replace('<h3>', f'<h3 id="{id}">', 1)
                 html = Markdown.apply_tailwind(html)
                 html = Markdown.apply_tailwind(html)
                 ui.html(html).classes('mr-8 w-4/12')
                 ui.html(html).classes('mr-8 w-4/12')
             else:
             else: