adding support for directives

This commit is contained in:
hkgnp 2022-02-15 17:42:53 +08:00
parent fbd366a778
commit 0ab3d60118
3 changed files with 44 additions and 13 deletions

View File

@ -32,10 +32,39 @@ or
Define in your plugin settings file. This will apply to **all** mermaid diagrams that you render.
```
%%{init: {'theme': 'dark'}}%%
flowchart LR
A[Hard] -->|Text| B(Round)
B --> C{Decision}
C -->|One| D[Result 1]
C -->|Three| E[Result 2]
```
```
%%{init: {'theme': 'dark'}}%%
flowchart LR
A[Hard] -->|Text| B(Round)
B --> C{Decision}
C -->|One| D[Result 1]
C -->|Three| E[Result 2]
```
```
{
"theme": '',
"colour": ''
"config": {
"theme": "dark",
"colour": "#2f3437"
}
}
or
{
"config": {
"theme": "dark",
"colour": "gray"
}
}
```

View File

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

View File

@ -6,14 +6,14 @@ export const renderMermaid = async (type, payload, colour, mermaidUUID) => {
const matchData = mermaidBlock.content.match(/```mermaid(.|\n)*?```/gm);
let toDecode = matchData[0];
if (logseq.settings) {
toDecode = toDecode.replace(
'```mermaid',
`%%{init: { 'theme': ${logseq.settings.theme} } }%%`
);
} else {
toDecode = toDecode.replace('```mermaid', '').replace('```', '');
if (logseq.settings.config) {
const initStr = `\n%%{init: {'theme': '${logseq.settings.config.theme}'}}%%`;
toDecode = toDecode.slice(0, 10) + initStr + toDecode.slice(10);
}
toDecode = toDecode.replace('```mermaid', '').replace('```', '');
toDecode = toDecode.replace('\n', ' ');
const jsonString = btoa(toDecode);
@ -27,14 +27,16 @@ export const renderMermaid = async (type, payload, colour, mermaidUUID) => {
};
const handleEvent = async () => {
if (logseq.settings) {
if (logseq.settings.colour.startsWith('#')) {
if (logseq.settings.config) {
const { colour } = logseq.settings.config;
if (colour.startsWith('#')) {
renderBlock(
`<img src="https://mermaid.ink/img/${jsonString}?bgColor=${colour.substring(
1
)}" />`
);
} else if (!colour.startsWith('#')) {
} else if (!logseq.settings.colour.startsWith('#')) {
renderBlock(
`<img src="https://mermaid.ink/img/${jsonString}?bgColor=!${colour}" />`
);