dsebastien / obsidian-replicate

Integrate Replicate.com with Obsidian
MIT License
5 stars 0 forks source link

Add option to automatically download the generated images #6

Open dsebastien opened 3 months ago

dsebastien commented 3 months ago

Goal: directly save the generated images locally

dsebastien commented 3 months ago

Download image:

export async function downloadImage(url: string): Promise<ArrayBuffer> {
  const res = await got(url, { responseType: "buffer" });
  return res.body;
}

Clean file name:

export const FORBIDDEN_SYMBOLS_FILENAME_PATTERN = /\s+/g;

export function cleanFileName(name: string) {
  const cleanedName = filenamify(name).replace(
    FORBIDDEN_SYMBOLS_FILENAME_PATTERN,
    "_"
  );
  return cleanedName;
}

Create image tag:

function createImageTag(anchor: string, link: string) {
  return `![${anchor}](${link})`;
}

Reference: https://github.com/aleksey-rezvov/obsidian-local-images/tree/master/src