Added support for changing background colour

This commit is contained in:
hkgnp 2022-02-15 14:46:23 +08:00
parent fdf00f96de
commit 550583db34
4 changed files with 34 additions and 5 deletions

View File

@ -10,6 +10,22 @@ This simple plugin allows you to quickly insert mermaid diagrams in your notes.
If not available in the marketplace, [download the latest release](https://github.com/hkgnp/logseq-mermaid-plugin/releases) and manually load it into Logseq after unzipping. If not available in the marketplace, [download the latest release](https://github.com/hkgnp/logseq-mermaid-plugin/releases) and manually load it into Logseq after unzipping.
# Usage
To start, simply trigger it using `/Draw mermaid diagram`. Then add your mermaid syntax in the code block and click `Render`.
## Changing background colour
Same steps as above, but before clicking `Render`, include the colour inside the renderer, e.g.
```
{{renderer :mermaid_abc123, purple}}
or
{{renderer :mermaid_abc123, #000000}}
```
# Credits # Credits
[mermaid.ink](https://github.com/jihchi/mermaid.ink) [mermaid.ink](https://github.com/jihchi/mermaid.ink)

View File

@ -5,7 +5,7 @@
"icon": "./icon.svg" "icon": "./icon.svg"
}, },
"name": "logseq-mermaid-plugin", "name": "logseq-mermaid-plugin",
"version": "1.0.4", "version": "1.0.5",
"description": "", "description": "",
"main": "dist/index.html", "main": "dist/index.html",
"targets": { "targets": {

View File

@ -1,6 +1,8 @@
export const convert = async (data: string) => { export const convert = async (data: string, colour: string) => {
const matchData = data.match(/```mermaid(.|\n)*?```/gm); const matchData = data.match(/```mermaid(.|\n)*?```/gm);
console.log(colour);
let toDecode = matchData[0]; let toDecode = matchData[0];
toDecode = toDecode.replace('```mermaid', '').replace('```', ''); toDecode = toDecode.replace('```mermaid', '').replace('```', '');
toDecode = toDecode.replace('\n', ' '); toDecode = toDecode.replace('\n', ' ');
@ -11,7 +13,17 @@ export const convert = async (data: string) => {
await fetch(`https://mermaid.ink/img/${jsonString}`, { method: 'GET' }) await fetch(`https://mermaid.ink/img/${jsonString}`, { method: 'GET' })
.then((res) => { .then((res) => {
if (res.ok) { if (res.ok) {
status = `<img src="https://mermaid.ink/img/${jsonString}" />`; if (colour === undefined) {
status = `<img src="https://mermaid.ink/img/${jsonString}" />`;
} else if (colour.startsWith('#')) {
status = `<img src="https://mermaid.ink/img/${jsonString}?bgColor=${colour.substring(
1
)}" />`;
} else if (!colour.startsWith('#')) {
status = `<img src="https://mermaid.ink/img/${jsonString}?bgColor=!${colour}" />`;
} else {
status = `<img src="https://mermaid.ink/img/${jsonString}" />`;
}
} else { } else {
status = `<p>There is an error with your mermaid syntax. Please rectify and render again.</p>`; status = `<p>There is an error with your mermaid syntax. Please rectify and render again.</p>`;
} }

View File

@ -15,6 +15,7 @@ const main = () => {
); );
const currBlock = await logseq.Editor.getCurrentBlock(); const currBlock = await logseq.Editor.getCurrentBlock();
await logseq.Editor.insertBlock( await logseq.Editor.insertBlock(
currBlock.uuid, currBlock.uuid,
`\`\`\`mermaid `\`\`\`mermaid
@ -27,7 +28,7 @@ const main = () => {
}); });
logseq.App.onMacroRendererSlotted(async ({ slot, payload }) => { logseq.App.onMacroRendererSlotted(async ({ slot, payload }) => {
const [type] = payload.arguments; const [type, colour] = payload.arguments;
const id = type.split('_')[1]?.trim(); const id = type.split('_')[1]?.trim();
const mermaidId = `mermaid_${id}`; const mermaidId = `mermaid_${id}`;
@ -46,7 +47,7 @@ const main = () => {
const mermaidBlock = await logseq.Editor.getBlock(mermaidUUID); const mermaidBlock = await logseq.Editor.getBlock(mermaidUUID);
const outcomeStr = await convert(mermaidBlock.content); const outcomeStr = await convert(mermaidBlock.content, colour);
await logseq.Editor.updateBlock( await logseq.Editor.updateBlock(
payload.uuid, payload.uuid,