Initial RuneStone project

This commit is contained in:
Knotty Wyvern
2026-09-26 01:25:42 -07:00
commit bdc15e952d
190 changed files with 25986 additions and 0 deletions
+121
View File
@@ -0,0 +1,121 @@
export const IMAGE_EXT = new Set([
"png",
"jpg",
"jpeg",
"gif",
"webp",
"svg",
"bmp",
"ico",
"avif",
]);
function fileExt(relPath: string): string {
const base = relPath.split("/").pop() ?? relPath;
const dot = base.lastIndexOf(".");
if (dot <= 0) {
return "";
}
return base.slice(dot + 1).toLowerCase();
}
export function isImagePath(relPath: string): boolean {
return IMAGE_EXT.has(fileExt(relPath));
}
export function isMarkdownPath(relPath: string): boolean {
const ext = fileExt(relPath);
return ext === "md" || ext === "markdown" || ext === "mdown";
}
export function markdownTitle(relPath: string): string {
return (relPath.replaceAll("\\", "/").split("/").pop() || "Untitled").replace(/\.(?:md|markdown|mdown)$/i, "");
}
export function dirnameOf(relPath: string): string {
const idx = relPath.replaceAll("\\", "/").lastIndexOf("/");
if (idx <= 0) {
return "";
}
return relPath.slice(0, idx);
}
export function resolveImageRel(noteRel: string, raw: string): string {
let path = raw.trim().replaceAll("\\", "/").replace(/^<|>$/g, "");
path = path.replace(/^\.\//, "");
if (path.startsWith("/")) {
return path.replace(/^\/+/, "");
}
const noteDir = dirnameOf(noteRel.replaceAll("\\", "/"));
if (path.startsWith("../")) {
const parts = [...noteDir.split("/").filter(Boolean), ...path.split("/")];
const out: string[] = [];
for (const part of parts) {
if (part === "..") {
out.pop();
} else if (part !== "." && part) {
out.push(part);
}
}
return out.join("/");
}
if (noteDir) {
return `${noteDir}/${path}`;
}
return path;
}
export function isRemoteImage(raw: string): boolean {
return /^https?:\/\//i.test(raw.trim());
}
export type MarkdownImage = {
alt: string;
src: string;
kind: "md" | "wiki";
};
export function findMarkdownImages(line: string): MarkdownImage[] {
const found: MarkdownImage[] = [];
const md = /!\[([^\]]*)\]\(([^)]+)\)/g;
const wiki = /!\[\[([^\]|#]+)(?:\|([^\]]+))?\]\]/g;
let match: RegExpExecArray | null;
while ((match = md.exec(line))) {
found.push({ alt: match[1], src: match[2].trim(), kind: "md" });
}
while ((match = wiki.exec(line))) {
found.push({ alt: match[2] ?? match[1], src: match[1].trim(), kind: "wiki" });
}
return found;
}
export function attachmentRel(fileName: string): string {
const raw = fileName || "image.png";
const safe = raw.replace(/[^\w.\-]+/g, "-");
const stamp = new Date().toISOString().replace(/[:.]/g, "").replace("T", "-").slice(0, 15);
return `attachments/${stamp}-${crypto.randomUUID()}-${safe}`;
}
export function imageFiles(data: DataTransfer | null): File[] {
return [...(data?.files ?? [])].filter(file => isImagePath(file.name));
}
// File contents and names are protected during dragover; files is usually empty.
export function hasFileDrag(data: DataTransfer | null): boolean {
return !!data && [...data.types].includes("Files");
}
export function imageInsertion(source: string, from: number, to: number, snippets: string[]): string {
const before = source.slice(0, from), after = source.slice(to);
const prefix = !before || /(?:\r?\n){2}$/.test(before) ? "" : before.endsWith("\n") ? "\n" : "\n\n";
const suffix = /^(?:\r?\n){2}/.test(after) ? "" : /^\r?\n/.test(after) ? "\n" : "\n\n";
return prefix + snippets.join("\n\n") + suffix;
}
export function workspaceImageLink(notePath: string, imagePath: string): string {
const from = notePath.replaceAll("\\", "/").split("/").slice(0, -1);
const to = imagePath.replaceAll("\\", "/").split("/");
while (from.length && to.length && from[0] === to[0]) { from.shift(); to.shift(); }
const relative = [...from.map(() => ".."), ...to].map(part => encodeURIComponent(part).replace(/[!'()*]/g, char => `%${char.charCodeAt(0).toString(16).toUpperCase()}`)).join("/");
return `![](${relative})`;
}