diff options
| author | 2020-07-22 02:06:50 +0200 | |
|---|---|---|
| committer | 2020-07-22 02:06:50 +0200 | |
| commit | 6795a7f05e3720f375a9195182b996a14d754ea0 (patch) | |
| tree | f5aedd3125cf3501f6042c7be4b14e7e5bc8c243 | |
| parent | Move DocMarkdownConverter to parsing. (diff) | |
Fix ordered list indices in markdown converter.
markdownify relies on the parent tag's index method,
which goes through all of its contents, if there is anything else
in the contents apart from the li tags, those indices are then shifted.
| -rw-r--r-- | bot/cogs/doc/parsing.py | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/bot/cogs/doc/parsing.py b/bot/cogs/doc/parsing.py index 8f6688bd2..25001b83d 100644 --- a/bot/cogs/doc/parsing.py +++ b/bot/cogs/doc/parsing.py @@ -3,10 +3,12 @@ import re import string from functools import partial from typing import Callable, List, Optional, Tuple, Union +from urllib.parse import urljoin from aiohttp import ClientSession from bs4 import BeautifulSoup from bs4.element import PageElement, Tag +from markdownify import MarkdownConverter from .cache import async_cache @@ -32,6 +34,22 @@ class DocMarkdownConverter(MarkdownConverter): super().__init__(**options) self.page_url = page_url + def convert_li(self, el: PageElement, text: str) -> str: + """Fix markdownify's erroneous indexing in ol tags.""" + parent = el.parent + if parent is not None and parent.name == 'ol': + li_tags = parent.find_all("li") + bullet = '%s.' % (li_tags.index(el)+1) + else: + depth = -1 + while el: + if el.name == 'ul': + depth += 1 + el = el.parent + bullets = self.options['bullets'] + bullet = bullets[depth % len(bullets)] + return '%s %s\n' % (bullet, text or '') + def convert_code(self, el: PageElement, text: str) -> str: """Undo `markdownify`s underscore escaping.""" return f"`{text}`".replace('\\', '') |