From 48d80f1889dac720430ad02895602750cd87bdb2 Mon Sep 17 00:00:00 2001 From: hkgnp Date: Tue, 15 Feb 2022 15:52:29 +0800 Subject: [PATCH] fixing cors issue --- README.md | 2 +- package.json | 2 +- src/convert.ts | 60 +++++++++++++++++++++++++++++++------------------- src/index.ts | 15 ++----------- 4 files changed, 41 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 62385a3..4e9332c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[:heart: Sponsor this project on Github](https://github.com/sponsors/hkgnp) +[[:gift_heart: Sponsor this project on Github](https://github.com/sponsors/hkgnp) or [:coffee: Get me a coffee](https://www.buymeacoffee.com/hkgnp.dev) if you like this plugin! # Overview diff --git a/package.json b/package.json index 7a3e360..8da62c7 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "icon": "./icon.svg" }, "name": "logseq-mermaid-plugin", - "version": "1.0.5", + "version": "1.0.6", "description": "", "main": "dist/index.html", "targets": { diff --git a/src/convert.ts b/src/convert.ts index 1177613..2d9e80f 100644 --- a/src/convert.ts +++ b/src/convert.ts @@ -1,7 +1,9 @@ -export const convert = async (data: string, colour: string) => { - const matchData = data.match(/```mermaid(.|\n)*?```/gm); +export const renderMermaid = async (type, payload, colour, mermaidUUID) => { + await logseq.Editor.editBlock(payload.uuid); + await logseq.Editor.exitEditingMode(); - console.log(colour); + const mermaidBlock = await logseq.Editor.getBlock(mermaidUUID); + const matchData = mermaidBlock.content.match(/```mermaid(.|\n)*?```/gm); let toDecode = matchData[0]; toDecode = toDecode.replace('```mermaid', '').replace('```', ''); @@ -9,28 +11,40 @@ export const convert = async (data: string, colour: string) => { const jsonString = btoa(toDecode); - let status = ''; - await fetch(`https://mermaid.ink/img/${jsonString}`, { method: 'GET' }) - .then((res) => { - if (res.ok) { - if (colour === undefined) { - status = ``; - } else if (colour.startsWith('#')) { - status = ``); + } else if (colour.startsWith('#')) { + renderBlock( + ``; - } else if (!colour.startsWith('#')) { - status = ``; - } else { - status = ``; - } + )}" />` + ); + } else if (!colour.startsWith('#')) { + renderBlock( + `` + ); } else { - status = `

There is an error with your mermaid syntax. Please rectify and render again.

`; + renderBlock(``); } - }) - .catch((err) => console.log('Error:', err)); + } else { + renderBlock( + '

There is an error with your mermaid syntax. Please rectify and render again.

' + ); + } + }; - const outcome = status; - - return outcome; + let req = new XMLHttpRequest(); + req.open('GET', `https://mermaid.ink/img/${jsonString}`, true); + req.send(); + req.onload = handleEvent; }; diff --git a/src/index.ts b/src/index.ts index ee6c8dc..bbdb0fd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ import '@logseq/libs'; -import { convert } from './convert'; +import { renderMermaid } from './convert'; const uniqueIdentifier = () => Math.random() @@ -42,18 +42,7 @@ const main = () => { logseq.provideModel({ async show() { - await logseq.Editor.editBlock(payload.uuid); - await logseq.Editor.exitEditingMode(); - - const mermaidBlock = await logseq.Editor.getBlock(mermaidUUID); - - const outcomeStr = await convert(mermaidBlock.content, colour); - - await logseq.Editor.updateBlock( - payload.uuid, - `${outcomeStr} -{{renderer ${type}}}` - ); + renderMermaid(type, payload, colour, mermaidUUID); }, });