42 lines
1.3 KiB
TypeScript
42 lines
1.3 KiB
TypeScript
|
|
const URL_PATTERN =
|
||
|
|
/https?:\/\/[^\s<>"')\]]+\/(?:MindSpace|temp)\/([a-z0-9._-]+)\/([^\s<>"')\]]+\.html)/gi;
|
||
|
|
|
||
|
|
export type MessageSaveKind = 'page' | 'article';
|
||
|
|
|
||
|
|
export type MessageSaveActions = {
|
||
|
|
kind: MessageSaveKind;
|
||
|
|
previewUrl: string | null;
|
||
|
|
links: Array<{ publicUrl: string; filename: string }>;
|
||
|
|
};
|
||
|
|
|
||
|
|
function decodeSegment(segment: string) {
|
||
|
|
try {
|
||
|
|
return decodeURIComponent(segment);
|
||
|
|
} catch {
|
||
|
|
return segment;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function extractStaticPageLinks(content: string, username?: string) {
|
||
|
|
const links: Array<{ publicUrl: string; filename: string }> = [];
|
||
|
|
const seen = new Set<string>();
|
||
|
|
for (const match of content.matchAll(URL_PATTERN)) {
|
||
|
|
const owner = decodeSegment(match[1]).toLowerCase();
|
||
|
|
const filename = decodeSegment(match[2]).split('/').pop() ?? match[2];
|
||
|
|
if (username && owner !== username.trim().toLowerCase()) continue;
|
||
|
|
if (seen.has(match[0])) continue;
|
||
|
|
seen.add(match[0]);
|
||
|
|
links.push({ publicUrl: match[0], filename });
|
||
|
|
}
|
||
|
|
return links;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function getMessageSaveActions(content: string, username?: string): MessageSaveActions {
|
||
|
|
const links = extractStaticPageLinks(content, username);
|
||
|
|
return {
|
||
|
|
kind: links.length > 0 ? 'page' : 'article',
|
||
|
|
previewUrl: links[0]?.publicUrl ?? null,
|
||
|
|
links,
|
||
|
|
};
|
||
|
|
}
|