feat: full Markdown support in service card comments (mistune)
This commit is contained in:
+7
-24
@@ -16,6 +16,7 @@ from typing import Optional
|
||||
|
||||
import docker
|
||||
import requests
|
||||
import mistune
|
||||
from fastapi import Depends, FastAPI, File, Form, HTTPException, Query, Request, UploadFile, status
|
||||
from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
@@ -299,34 +300,16 @@ def normalize_web_target(url: str) -> str:
|
||||
return f"http://{raw}"
|
||||
|
||||
|
||||
_md = mistune.create_markdown(
|
||||
escape=True,
|
||||
plugins=["strikethrough", "table", "task_lists"],
|
||||
)
|
||||
|
||||
def format_service_comment(raw_text: str) -> Markup:
|
||||
raw = (raw_text or "").replace("\r\n", "\n").replace("\r", "\n").strip()
|
||||
if not raw:
|
||||
return Markup("")
|
||||
escaped = str(escape(raw))
|
||||
# Support pasted/plain markdown-like bold fragments.
|
||||
escaped = re.sub(r"\*\*(.+?)\*\*", r"<strong>\1</strong>", escaped, flags=re.DOTALL)
|
||||
# Allow a small safe subset of pasted HTML tags.
|
||||
replacements = {
|
||||
"<b>": "<b>",
|
||||
"</b>": "</b>",
|
||||
"<strong>": "<strong>",
|
||||
"</strong>": "</strong>",
|
||||
"<i>": "<i>",
|
||||
"</i>": "</i>",
|
||||
"<em>": "<em>",
|
||||
"</em>": "</em>",
|
||||
"<u>": "<u>",
|
||||
"</u>": "</u>",
|
||||
"<br>": "<br>",
|
||||
"<br/>": "<br>",
|
||||
"<br />": "<br>",
|
||||
}
|
||||
for src, dst in replacements.items():
|
||||
escaped = escaped.replace(src, dst)
|
||||
escaped = escaped.replace("\n", "<br>")
|
||||
return Markup(escaped)
|
||||
|
||||
return Markup(_md(raw))
|
||||
|
||||
def parse_rdp_target(target: str) -> dict:
|
||||
raw = (target or "").strip()
|
||||
|
||||
Reference in New Issue
Block a user