mirror of
https://github.com/zsviczian/obsidian-excalidraw-plugin.git
synced 2025-08-06 05:46:28 +00:00
148 lines
4.4 KiB
TypeScript
148 lines
4.4 KiB
TypeScript
import { normalizePath, Notice, TAbstractFile, TFile, TFolder, Vault } from "obsidian";
|
|
import { ExcalidrawSettings } from "src/Settings";
|
|
|
|
/**
|
|
* Splits a full path including a folderpath and a filename into separate folderpath and filename components
|
|
* @param filepath
|
|
*/
|
|
|
|
export function splitFolderAndFilename(filepath: string): {
|
|
folderpath: string;
|
|
filename: string;
|
|
basename: string;
|
|
} {
|
|
const lastIndex = filepath.lastIndexOf("/");
|
|
const filename = lastIndex == -1 ? filepath : filepath.substring(lastIndex + 1);
|
|
return {
|
|
folderpath: normalizePath(filepath.substring(0, lastIndex)),
|
|
filename,
|
|
basename: filename.replace(/\.[^/.]+$/, ""),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Download data as file from Obsidian, to store on local device
|
|
* @param encoding
|
|
* @param data
|
|
* @param filename
|
|
*/
|
|
export function download(encoding: string, data: any, filename: string) {
|
|
const element = document.createElement("a");
|
|
element.setAttribute("href", (encoding ? `${encoding},` : "") + data);
|
|
element.setAttribute("download", filename);
|
|
element.style.display = "none";
|
|
document.body.appendChild(element);
|
|
element.click();
|
|
document.body.removeChild(element);
|
|
}
|
|
|
|
/**
|
|
* Generates the image filename based on the excalidraw filename
|
|
* @param excalidrawPath - Full filepath of ExclidrawFile
|
|
* @param newExtension - extension of IMG file in ".extension" format
|
|
* @returns
|
|
*/
|
|
/*export function getIMGPathFromExcalidrawFile(
|
|
excalidrawPath: string,
|
|
newExtension: string,
|
|
): string {
|
|
const isLegacyFile: boolean = excalidrawPath.endsWith(".excalidraw");
|
|
const replaceExtension: string = isLegacyFile ? ".excalidraw" : ".md";
|
|
return (
|
|
excalidrawPath.substring(0, excalidrawPath.lastIndexOf(replaceExtension)) +
|
|
newExtension
|
|
);
|
|
}*/
|
|
|
|
/**
|
|
* Generates the image filename based on the excalidraw filename
|
|
* @param path - path to the excalidraw file
|
|
* @param extension - extension without the preceeding "."
|
|
* @returns
|
|
*/
|
|
export function getIMGFilename(path: string, extension: string): string {
|
|
return `${path.substring(0, path.lastIndexOf("."))}.${extension}`;
|
|
}
|
|
|
|
/**
|
|
* Create new file, if file already exists find first unique filename by adding a number to the end of the filename
|
|
* @param filename
|
|
* @param folderpath
|
|
* @returns
|
|
*/
|
|
export function getNewUniqueFilepath(
|
|
vault: Vault,
|
|
filename: string,
|
|
folderpath: string,
|
|
): string {
|
|
let fname = normalizePath(`${folderpath}/${filename}`);
|
|
let file: TAbstractFile = vault.getAbstractFileByPath(fname);
|
|
let i = 0;
|
|
const extension = filename.endsWith(".excalidraw.md")
|
|
? ".excalidraw.md"
|
|
: filename.slice(filename.lastIndexOf("."));
|
|
while (file) {
|
|
fname = normalizePath(
|
|
`${folderpath}/${filename.slice(
|
|
0,
|
|
filename.lastIndexOf(extension),
|
|
)}_${i}${extension}`,
|
|
);
|
|
i++;
|
|
file = vault.getAbstractFileByPath(fname);
|
|
}
|
|
return fname;
|
|
}
|
|
|
|
export function getDrawingFilename(settings: ExcalidrawSettings): string {
|
|
return (
|
|
settings.drawingFilenamePrefix +
|
|
(settings.drawingFilenameDateTime !== ""
|
|
? window.moment().format(settings.drawingFilenameDateTime)
|
|
: "") +
|
|
(settings.compatibilityMode
|
|
? ".excalidraw"
|
|
: settings.useExcalidrawExtension
|
|
? ".excalidraw.md"
|
|
: ".md")
|
|
);
|
|
}
|
|
|
|
export function getEmbedFilename(
|
|
notename: string,
|
|
settings: ExcalidrawSettings,
|
|
): string {
|
|
return (
|
|
(settings.drawingEmbedPrefixWithFilename ? notename : "") +
|
|
settings.drawingFilnameEmbedPostfix +
|
|
(settings.drawingFilenameDateTime !== ""
|
|
? window.moment().format(settings.drawingFilenameDateTime)
|
|
: "") +
|
|
(settings.compatibilityMode
|
|
? ".excalidraw"
|
|
: settings.useExcalidrawExtension
|
|
? ".excalidraw.md"
|
|
: ".md")
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Open or create a folderpath if it does not exist
|
|
* @param folderpath
|
|
*/
|
|
export async function checkAndCreateFolder(folderpath: string) {
|
|
const vault = app.vault;
|
|
folderpath = normalizePath(folderpath);
|
|
//https://github.com/zsviczian/obsidian-excalidraw-plugin/issues/658
|
|
//@ts-ignore
|
|
const folder = vault.getAbstractFileByPathInsensitive(folderpath);
|
|
if (folder && folder instanceof TFolder) {
|
|
return;
|
|
}
|
|
if (folder && folder instanceof TFile) {
|
|
new Notice(`The folder cannot be created because it already exists as a file: ${folderpath}.`)
|
|
}
|
|
await vault.createFolder(folderpath);
|
|
}
|
|
|