okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0213-preview (68bead108) into f-0213-inte82c70698

main
2.7 KiB33 linesraw
1export interface PreviewArtifact { id: string; kind: 'website' | 'markdown' | 'pdf' | 'image' | 'code' | 'table'; title: string; url?: string; content?: string; demo?: boolean;
2  /** The host's version of the file this was read from (modified time and size), for a preview with an address instead of text. */
3  version?: string;
4  /** When this copy was read or made (ms): a message's copy never replaces a later one (lib/preview-merge.ts). */
5  at?: number; }
6export const PREVIEW_EXAMPLES: PreviewArtifact[] = [
7  { id:'sample-website', kind:'website', title:'Fieldnotes — website', url:'/examples/fieldnotes.html', demo:true },
8  { id:'sample-markdown', kind:'markdown', title:'project-brief.md', url:'/examples/project-brief.md', demo:true },
9  { id:'sample-image', kind:'image', title:'fieldnotes-cover.svg', url:'/examples/fieldnotes-cover.svg', demo:true },
10  { id:'sample-code', kind:'code', title:'note-card.ts', url:'/examples/note-card.ts', demo:true },
11  { id:'sample-table', kind:'table', title:'release-plan.csv', url:'/examples/release-plan.csv', demo:true },
12  { id:'sample-pdf', kind:'pdf', title:'project-brief.pdf', url:'/examples/project-brief.pdf', demo:true },
13];
14export function safePreviewUrl(value: string) {
15  try { const u = new URL(value, location.origin); return ['http:','https:','blob:'].includes(u.protocol) ? u.href : null; }
16  catch { return null; }
17}
18export function artifactFromUrl(url: string): PreviewArtifact | null {
19  const safe = safePreviewUrl(url); if (!safe) return null;
20  const path = new URL(safe).pathname;
21  const kind = /\.pdf$/i.test(path) ? 'pdf' : /\.(png|jpe?g|webp|gif|svg)$/i.test(path) ? 'image' : /\.csv$/i.test(path)?'table':/\.(tsx?|jsx?|mts|cts|mjs|cjs|pyw?|json|css|rs|go|sh|bash|zsh|ya?ml|java|c|h|cpp|cc|cxx|hpp|cs|sql)$/i.test(path)?'code': /\.(md|mdx|markdown|txt)$/i.test(path) ? 'markdown' : 'website';
22  let filename = path.split('/').at(-1) || 'Document';
23  try { filename = decodeURIComponent(filename); } catch { /* Keep malformed URL escapes readable. */ }
24  return { id:safe, kind, url:safe, title:kind === 'website' ? new URL(safe).hostname : filename };
25}
26
27export const artifactLabel=(kind:PreviewArtifact['kind'])=>({website:'Website',markdown:'Markdown',pdf:'PDF',image:'Image',code:'Code',table:'Table'}[kind]);
28
29export function artifactFilename(a:PreviewArtifact) {
30  if(a.url&&!a.url.startsWith('blob:')){try{const u=new URL(a.url,location.origin);const file=decodeURIComponent(u.pathname.split('/').filter(Boolean).at(-1)??'');return /\.[a-z0-9]{1,8}$/i.test(file)?file:a.kind==='website'?u.hostname:a.title;}catch{/* Use the supplied title. */}}
31  return a.title;
32}
33export const artifactIcon=(a:PreviewArtifact)=>a.kind==='website'?'link' as const:a.kind==='image'?'image' as const:'file' as const;