20 lines
625 B
Python
20 lines
625 B
Python
|
|
import re
|
||
|
|
|
||
|
|
from yuxi.channel.extensions.jd.types import JD_TEXT_MAX_LENGTH
|
||
|
|
|
||
|
|
|
||
|
|
def remove_markdown(text: str) -> str:
|
||
|
|
text = re.sub(r"\*\*(.+?)\*\*", r"\1", text)
|
||
|
|
text = re.sub(r"\*(.+?)\*", r"\1", text)
|
||
|
|
text = re.sub(r"`(.+?)`", r"\1", text)
|
||
|
|
text = re.sub(r"\[(.+?)\]\(.+?\)", r"\1", text)
|
||
|
|
text = re.sub(r"^\s*[-*+]\s+", "• ", text, flags=re.MULTILINE)
|
||
|
|
text = re.sub(r"^\s*\d+\.\s+", "", text, flags=re.MULTILINE)
|
||
|
|
return text.strip()
|
||
|
|
|
||
|
|
|
||
|
|
def truncate(text: str, max_chars: int = JD_TEXT_MAX_LENGTH) -> str:
|
||
|
|
if len(text) > max_chars:
|
||
|
|
return text[: max_chars - 3] + "..."
|
||
|
|
return text
|