refactor: 收敛重复的 Markdown 渲染与应用状态常量

This commit is contained in:
2026-08-02 11:36:18 +08:00
parent 69861d54bb
commit 96d574b99f
5 changed files with 40 additions and 88 deletions
+2 -35
View File
@@ -11,6 +11,8 @@ import {
type TaskApplication,
} from "@/api/tasks";
import { getUrlTitle } from "@/utils/fetchTitle";
import { renderMarkdown } from "@/utils/markdown";
import { applicationStatusOptions } from "@/utils/taskApplication";
const router = useRouter();
const route = useRoute();
@@ -32,11 +34,6 @@ const priority = ref({
});
const priorityOptions = [0, 1, 2, 3, 4, 5];
const applicationStatusOptions: { label: string; value: TaskApplication["status"] }[] = [
{ label: "待应用", value: "TODO" },
{ label: "进行中", value: "DOING" },
{ label: "已完成", value: "DONE" },
];
const applications = ref<TaskApplication[]>([]);
const applicationLoading = ref(false);
@@ -277,36 +274,6 @@ const isMobile = computed(() => screenWidth.value <= 900);
const showPreview = ref(false);
const materialPreview = ref("");
const escapeHtml = (s: string) => s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
const escapeAttr = (s: string) => s.replace(/"/g, "&quot;").replace(/&/g, "&amp;");
function renderMarkdown(raw: string, urlTitles: Record<string, string> = {}): string {
if (!raw.trim()) return "";
let html = raw;
// 1) [text](url) → &lt;a&gt; 占位,防止后续裸 URL 匹配进属性里
const links: string[] = [];
html = html.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (_m: string, text: string, url: string) => {
const tag = `<a href="${escapeAttr(url)}" target="_blank" rel="noopener">${escapeHtml(text)}</a>`;
links.push(tag);
return `__LINK_${links.length - 1}__`;
});
// 2) 裸 URL → &lt;a&gt; 占位
html = html.replace(/https?:\/\/[^\s)\u3001\uFF09\u300D\u300B<>]+/g, (url) => {
const clean = url.replace(/[.。,;!??)】」』\]]+$/, "");
const label = urlTitles[clean] || url;
const tag = `<a href="${escapeAttr(clean)}" target="_blank" rel="noopener">${escapeHtml(label)}</a>`;
links.push(tag);
return `__LINK_${links.length - 1}__`;
});
// 3) 换行
html = html.replace(/\n/g, "<br>");
// 4) 还原占位
for (let i = 0; i < links.length; i++) {
html = html.replace(`__LINK_${i}__`, links[i]);
}
return html;
}
async function previewMaterial() {
showPreview.value = true;
const raw = materialUrl.value || "";