diff options
author | 2021-01-23 06:08:01 +0100 | |
---|---|---|
committer | 2021-01-23 06:08:01 +0100 | |
commit | f0b468d9c22eea43e36cd14960c23cb2c30cb335 (patch) | |
tree | ab9ee282a26ee2ccde4bfe561d630392bd3c5df6 | |
parent | lock markdownify version to 0.6.1 (diff) |
Avoid errors when the first element is longer than the truncation index
-rw-r--r-- | bot/exts/info/doc/_parsing.py | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/bot/exts/info/doc/_parsing.py b/bot/exts/info/doc/_parsing.py index 45a81a4cb..0251b0105 100644 --- a/bot/exts/info/doc/_parsing.py +++ b/bot/exts/info/doc/_parsing.py @@ -195,9 +195,16 @@ def _get_truncated_description( return result # Determine the actual truncation index. - # Truncate at the last Markdown element that comes before the truncation index. - markdown_truncate_index = max(cut for cut in markdown_element_ends if cut < truncate_index) - return result[:markdown_truncate_index].strip(_TRUNCATE_STRIP_CHARACTERS) + "..." + possible_truncation_indices = [cut for cut in markdown_element_ends if cut < truncate_index] + if not possible_truncation_indices: + # In case there is no Markdown element ending before the truncation index, use shorten as a fallback. + truncated_result = textwrap.shorten(result, truncate_index) + else: + # Truncate at the last Markdown element that comes before the truncation index. + markdown_truncate_index = max(possible_truncation_indices) + truncated_result = result[:markdown_truncate_index] + + return truncated_result.strip(_TRUNCATE_STRIP_CHARACTERS) + "..." def _create_markdown(signatures: Optional[List[str]], description: Iterable[Tag], url: str) -> str: |