1
0
forked from erp-dev/erp

feat: wecomm support image link

This commit is contained in:
2026-01-23 21:11:13 +08:00
parent 81640cb84b
commit 4d33bc7f68
4 changed files with 99 additions and 8 deletions

View File

@@ -10,6 +10,7 @@ This module is intended for reusable business logic that may be called by:
from __future__ import annotations
from dataclasses import dataclass
from urllib.parse import urlparse
from django.conf import settings
from django.utils import timezone
@@ -48,6 +49,26 @@ def _truncate_text(s: str, max_len: int) -> str:
return s[: max(0, int(max_len) - 6)] + "...(截断)"
def _is_http_url(s: str) -> bool:
"""
Minimal URL check for markdown linkify.
Only treat http/https absolute URLs as linkable.
"""
s = (s or "").strip()
if not s:
return False
try:
p = urlparse(s)
except Exception:
return False
return p.scheme in ("http", "https") and bool(p.netloc)
def _escape_markdown_link_text(s: str) -> str:
# avoid breaking markdown link text
return (s or "").replace("[", r"\[").replace("]", r"\]")
def render_process_params_markdown(*, params: dict, max_items: int = 60, max_value_len: int = 200) -> str:
"""
Render process params as markdown lines.
@@ -61,7 +82,13 @@ def render_process_params_markdown(*, params: dict, max_items: int = 60, max_val
for k in sorted(params.keys(), key=lambda x: str(x)):
v = params.get(k)
kk = _truncate_text(str(k), 80)
vv = _truncate_text(str(v), int(max_value_len))
v_str = "" if v is None else str(v)
v_href = v_str.strip()
if _is_http_url(v_href):
v_disp = _escape_markdown_link_text(_truncate_text(v_href, int(max_value_len)))
vv = f"[{v_disp}]({v_href})"
else:
vv = _truncate_text(v_str, int(max_value_len))
items.append(f"- **{kk}**{vv}")
if len(items) > int(max_items):

View File

@@ -0,0 +1,23 @@
from django.test import SimpleTestCase
class WeComMarkdownRenderTest(SimpleTestCase):
def test_render_process_params_markdown_linkify_http_url(self):
from printing.services import render_process_params_markdown
md = render_process_params_markdown(
params={
"图片": "https://example.com/a.png",
"备注": "不是链接",
}
)
self.assertIn("- **图片**[https://example.com/a.png](https://example.com/a.png)", md)
self.assertIn("- **备注**:不是链接", md)
def test_render_process_params_markdown_linkify_strips_whitespace(self):
from printing.services import render_process_params_markdown
md = render_process_params_markdown(params={"图片": " https://example.com/a.png "})
self.assertIn("[https://example.com/a.png](https://example.com/a.png)", md)