25 lines
743 B
Python
25 lines
743 B
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import re
|
||
|
|
|
||
|
|
|
||
|
|
def clean_for_xiaohongshu(text: str) -> str:
|
||
|
|
text = re.sub(r"```\w*\n?", "", 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"__([^_]+)__", r"\1", text)
|
||
|
|
text = re.sub(r"_([^_]+)_", r"\1", text)
|
||
|
|
text = re.sub(r"(?m)^#{1,6}\s+", "", text)
|
||
|
|
text = re.sub(r"(?m)^[-*_]{3,}\s*$", "", text)
|
||
|
|
text = re.sub(r"https?://\S+", "[链接]", text)
|
||
|
|
return text.strip()
|
||
|
|
|
||
|
|
|
||
|
|
def sanitize_text(text: str, payload: object | None = None) -> str:
|
||
|
|
return clean_for_xiaohongshu(text)
|
||
|
|
|
||
|
|
|
||
|
|
def markdown_to_native(md_text: str) -> str:
|
||
|
|
return clean_for_xiaohongshu(md_text)
|