diff --git a/src/file.ts b/src/file.ts index 79e0e48..25c012d 100644 --- a/src/file.ts +++ b/src/file.ts @@ -11,8 +11,10 @@ function toFile(title: string, content: string, mimeType: string){ export async function uploadAsset(fileID: string, mimeType: string, content: string) { const file = toFile(fileID + ".svg", content, mimeType); + console.log(1, file) let r = await upload('/' + ASSETS_PATH, [file]); + console.log(2, r) if(r.errFiles) { throw new Error("Failed to upload file"); } diff --git a/src/index.ts b/src/index.ts index 78f1768..adf4b94 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,6 +7,7 @@ import { imgSrcToIDs, generateTimeString, generateRandomString } from "@/helper"; import {editorTabInit, openEditorTab} from "@/editorTab"; +import {migrate} from "@/migration"; export default class DrawJSPlugin extends Plugin { @@ -17,6 +18,7 @@ export default class DrawJSPlugin extends Plugin { 'type': "whiteboard", init() { editorTabInit(this) } }); + migrate() this.protyleSlash = [{ id: "insert-drawing", diff --git a/src/migration.ts b/src/migration.ts new file mode 100644 index 0000000..290b700 --- /dev/null +++ b/src/migration.ts @@ -0,0 +1,50 @@ +import {sql} from "@/api"; +import {getFile, uploadAsset} from "@/file"; +import {ASSETS_PATH, DATA_PATH, SVG_MIME} from "@/const"; +import {replaceBlockContent} from "@/protyle"; +import {generateRandomString, getMarkdownBlock} from "@/helper"; + +export async function migrate() { + + let blocks = await findEmbedBlocks(); + console.log(blocks); + const found = blocks.length > 0; + + for(const block of blocks) { + const oldFileID = extractID(block.markdown); + if(oldFileID) { + const newFileID = generateRandomString() + "-" + oldFileID; + const file = await getFile(DATA_PATH + ASSETS_PATH + oldFileID + ".svg"); + console.log("file", file) + const r = await uploadAsset(newFileID, SVG_MIME, file); + console.log("r", r); + const newMarkdown = getMarkdownBlock(r.fileID, r.syncID); + await replaceBlockContent(block.id, block.markdown, newMarkdown); + } + } + +} + +function extractID(html: string): string | null { + // Match the pattern: id= followed by characters until & or quote + const regex = /id=([^&"']+)/; + const match = html.match(regex); + return match ? match[1] : null; +} + +async function findEmbedBlocks() { + + const sqlQuery = ` + SELECT id, markdown + FROM blocks + WHERE markdown like '%src="/plugins/siyuan-jsdraw-plugin/webapp/%' + `; + + try { + return await sql(sqlQuery); + } catch (error) { + console.error('Error searching for embed blocks:', error); + return []; + } + +} \ No newline at end of file