'use client'; // Mira 共用 Markdown 渲染器(河道 + Wiki 共用) // SDD: polaris/mira/.agents/specs/mira-app/design.md §3.5.7 import { useMemo } from 'react'; import ReactMarkdown from 'react-markdown'; import remarkGfm from 'remark-gfm'; export function MarkdownView({ text }: { text: string }) { // 三階段預處理:1. strip Logseq metadata;2. [[entity]] 轉 link;3. raw: 轉河道 deep-link const cleaned = useMemo( () => expandRawRefs(expandWikilinks(stripLogseqMeta(text))), [text], ); return (
{ const isInternal = typeof href === 'string' && (href.startsWith('/mira/wiki/') || href.startsWith('/mira/feed')); return ( {children} ); }, // 圖片不直接 inline 顯示(避免大圖打亂 feed),改成連結 img: ({ src, alt }) => { const href = typeof src === 'string' ? src : ''; return href ? ( 🖼 {alt || 'image'} ) : null; }, }} > {cleaned}
); } // Strip Logseq 專屬語法 // - 屬性行:`xxx:: yyy`、`collapsed:: true`、`id:: ...`、`logseq.order-list-type:: ...` // - block ref:`((uuid))` 暫時保留為純文字 export function stripLogseqMeta(text: string): string { return text .split('\n') .filter((line) => { const trimmed = line.trimStart(); if (/^[a-zA-Z][a-zA-Z0-9_.-]*::\s/.test(trimmed)) return false; return true; }) .join('\n'); } // 把 [[entity]] 轉成 markdown link 指向 /mira/wiki/wiki-{entity} // 對應 mira-app design.md §3.6.2 + tasks.md backlog #12 export function expandWikilinks(text: string): string { return text.replace(/\[\[([^\[\]\n]+?)\]\]/g, (_, entity: string) => { const e = entity.trim(); if (!e) return '[[]]'; const url = `/mira/wiki/${encodeURIComponent('wiki-' + e)}`; return `[${e}](${url})`; }); } // index-entry / wiki backlink 區塊內的 `raw:` bare 文字轉成可點河道 deep-link。 // 對應 leo 反饋 #3:index 顯示 uuid 無法點擊。河道 hash handler 認得 #raw= 並解析回 page_name。 // 已在 markdown link 內([..](..))的不重複處理;只抓裸 raw:uuid。 const UUID_RE = '[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}'; export function expandRawRefs(text: string): string { return text.replace( new RegExp(`(? `[raw:${id.slice(0, 8)}…](/mira/feed#raw=${id})`, ); }