backstage-plugin-techdocs-addon-mermaid 0.12.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -2
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +1 -1
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -50,7 +50,7 @@ graph TD;
|
|
|
50
50
|
|
|
51
51
|
## Auto-Detection vs. Manual Detection
|
|
52
52
|
|
|
53
|
-
By default, this plugin will autodetect diagrams based on the starting token of the code block. In some cases, however, this auto-detection is not sufficient, for example because of an unrecognized
|
|
53
|
+
By default, this plugin will autodetect diagrams based on the starting token of the code block. In some cases, however, this auto-detection is not sufficient, for example, because of an unrecognized
|
|
54
54
|
diagram type or the use of front matter. In these cases, you can force the use of mermaid on blocks by adding configuration like this to your `mkdocs.yaml` file:
|
|
55
55
|
|
|
56
56
|
```yaml
|
|
@@ -61,4 +61,42 @@ markdown_extensions:
|
|
|
61
61
|
- name: mermaid
|
|
62
62
|
class: mermaid
|
|
63
63
|
format: !!python/name:pymdownx.superfences.fence_code_format
|
|
64
|
-
```
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Contributors Guide
|
|
67
|
+
|
|
68
|
+
This plugin can be developed in the context of an existing Backstage deployment or a [new local deployment](https://backstage.io/docs/getting-started/#1-create-your-backstage-app).
|
|
69
|
+
|
|
70
|
+
### Setup for Deployment
|
|
71
|
+
|
|
72
|
+
1. Fork and clone this repo into the plugins folder of your Backstage codebase.
|
|
73
|
+
2. To have yarn link the local version of the addon instead of the version on npm.
|
|
74
|
+
1. Change directories to the new `plugins/backstage-plugin-techdocs-addon-mermaid folder` and run `yarn link`.
|
|
75
|
+
2. Go up to the main Backstage directory and run `yarn link "backstage-plugin-techdocs-addon-mermaid`.
|
|
76
|
+
3. Run `yarn install` in the Backstage root.
|
|
77
|
+
4. Follow the earlier instructions to add the plugin to your TechDocs pages in your Backstage deployment such as `app.tsx`.
|
|
78
|
+
|
|
79
|
+
### Manual Testing
|
|
80
|
+
|
|
81
|
+
After making changes to the plugin and having unit tests pass, to do manual end-to-end testing, follow the instructions below.
|
|
82
|
+
|
|
83
|
+
#### Option #1 Techdocs CLI
|
|
84
|
+
|
|
85
|
+
You can use the [TechDocs CLI](https://backstage.io/docs/features/techdocs/cli/) to test against a local docs folder. You will need to customize the preview app bundle for that to work as the addon is not included in the [standard bundle](https://github.com/backstage/techdocs-cli/blob/main/packages/embedded-techdocs-app/src/App.tsx). Review the TechDoc's documentation for further instructions.
|
|
86
|
+
|
|
87
|
+
#### Option #2 Use a Remote Location
|
|
88
|
+
|
|
89
|
+
Register a component via URL like any other Backstage component and view that component's TechDocs.
|
|
90
|
+
For example, to use the SampleDocs component in this repo:
|
|
91
|
+
|
|
92
|
+
1. Generate a [GitHub personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens) for *public repos*.
|
|
93
|
+
2. Add the [GitHub integration](https://backstage.io/docs/integrations/github/locations) to your `app-config.local.yaml`.
|
|
94
|
+
3. `yarn dev` in the root of your Backstage codebase.
|
|
95
|
+
4. To register the demo docs, browse to `http://localhost:3000/catalog-import`
|
|
96
|
+
5. Register the URL pointing to the SampleDocs/catalog-info.yaml, example: `https://github.com/johanneswuerbach/backstage-plugin-techdocs-addon-mermaid/blob/main/sampledocs/catalog-info.yaml`
|
|
97
|
+
6. To iterate:
|
|
98
|
+
1. Create a branch for the addon.
|
|
99
|
+
2. Change the contents of the sampledocs.
|
|
100
|
+
3. Commit and push.
|
|
101
|
+
4. Register the catalog-info.yaml for your branch instead (keep in mind any security changes required for your personal access token).
|
|
102
|
+
5. Iterate changes to markdown and changes to the plugin.
|
package/dist/index.esm.js
CHANGED
|
@@ -4,7 +4,7 @@ import { useEffect } from 'react';
|
|
|
4
4
|
import { useTheme } from '@material-ui/core';
|
|
5
5
|
import mermaid from 'mermaid';
|
|
6
6
|
|
|
7
|
-
const mermaidStart = /^(\s*)(graph|flowchart|sequenceDiagram|classDiagram|stateDiagram|erDiagram|journey|gantt|pie|requirementDiagram|gitGraph|C4Context|C4Container|C4Component|C4Dynamic|C4Deployment|timeline)
|
|
7
|
+
const mermaidStart = /^(\s*)(graph|flowchart|sequenceDiagram|classDiagram|stateDiagram|erDiagram|journey|gantt|pie|requirementDiagram|gitGraph|C4Context|C4Container|C4Component|C4Dynamic|C4Deployment|timeline|mindmap|quadrantChart|xychart-beta|block-beta)/gm;
|
|
8
8
|
const isMermaidCode = (code) => {
|
|
9
9
|
if (code.startsWith("%%{init")) {
|
|
10
10
|
const codeSplitByDirectiveStart = code.split("%%");
|
package/dist/index.esm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.esm.js","sources":["../src/Mermaid/hooks.ts","../src/Mermaid/Mermaid.tsx","../src/plugin.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nconst mermaidStart =\n /^(\\s*)(graph|flowchart|sequenceDiagram|classDiagram|stateDiagram|erDiagram|journey|gantt|pie|requirementDiagram|gitGraph|C4Context|C4Container|C4Component|C4Dynamic|C4Deployment|timeline)/;\n\nexport const isMermaidCode = (code: string): boolean => {\n if (code.startsWith('%%{init')) {\n const codeSplitByDirectiveStart = code.split('%%');\n\n if (codeSplitByDirectiveStart.length > 2)\n return code.split('%%')[2].match(mermaidStart) !== null;\n }\n\n return code.match(mermaidStart) !== null;\n};\n","/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { useEffect } from 'react';\nimport { PaletteType, useTheme } from '@material-ui/core';\n\nimport { useShadowRootElements } from '@backstage/plugin-techdocs-react';\nimport mermaid from 'mermaid'\nimport { isMermaidCode } from './hooks';\nimport { MermaidProps } from './props';\nimport { BackstageTheme } from '@backstage/theme';\nimport { MermaidConfig } from 'mermaid';\n\nexport function selectConfig(backstagePalette: PaletteType, properties: MermaidProps): MermaidConfig {\n // Theme set directly in the Mermaid configuration takes\n // precedence for backwards-compatibility\n if(properties.config) {\n return properties.config;\n }\n\n if(backstagePalette === 'light') {\n return properties.lightConfig || {};\n }\n\n return Object.assign({ theme: 'dark' }, properties.darkConfig);\n}\n\n/**\n * Show report issue button when text is highlighted\n */\n\nlet diagramId = 0\n\nconst makeDiagram = async (el: HTMLDivElement | HTMLPreElement, diagramText: string, backstagePalette: PaletteType, properties: MermaidProps) => {\n el.style.display = 'none'\n\n const diagramElement = document.createElement('div')\n diagramElement.className = \"mermaid\"\n\n el.parentNode?.insertBefore(diagramElement, el.nextSibling);\n\n const id = `mermaid-${diagramId++}`\n\n const config = selectConfig(backstagePalette, properties);\n if(config) {\n mermaid.initialize(config);\n }\n\n const { svg, bindFunctions } = await mermaid.render(id, diagramText);\n diagramElement.innerHTML = svg\n bindFunctions?.(diagramElement);\n}\n\nexport const MermaidAddon = (properties: MermaidProps) => {\n const highlightTables = useShadowRootElements<HTMLDivElement>(['.highlighttable']);\n const highlightDivs = useShadowRootElements<HTMLDivElement>(['.highlight']);\n const mermaidPreBlocks = useShadowRootElements<HTMLPreElement>(['.mermaid']);\n const theme = useTheme<BackstageTheme>();\n\n useEffect(() => {\n highlightTables.forEach(highlightTable => {\n if (!highlightTable.classList.contains('language-text')) {\n return;\n }\n\n // Skip already processed\n if (highlightTable.style.display === 'none') {\n return\n }\n\n const codeBlock = highlightTable.querySelector('code')\n if (!codeBlock) {\n return\n }\n\n const diagramText = codeBlock.textContent || ''\n\n // Ideally we could detect mermaid based on some annotation, but use a regex for now\n if (!isMermaidCode(diagramText)) {\n return\n }\n\n makeDiagram(highlightTable, diagramText, theme.palette.type, properties)\n });\n }, [highlightTables, properties, theme.palette.type]);\n\n useEffect(() => {\n highlightDivs.forEach(highlightDiv => {\n if (!highlightDiv.classList.contains('language-text')) {\n return;\n }\n\n // Skip already processed\n if (highlightDiv.style.display === 'none') {\n return\n }\n\n // skip mkdocs-material < 9 code blocks (handled above)\n const table = highlightDiv.querySelector('table')\n if (!table) {\n return\n }\n\n const codeBlock = highlightDiv.querySelector('code')\n if (!codeBlock) {\n return\n }\n\n const diagramText = codeBlock.textContent || ''\n\n // Ideally we could detect mermaid based on some annotation, but use a regex for now\n if (!isMermaidCode(diagramText)) {\n return\n }\n\n makeDiagram(highlightDiv, diagramText, theme.palette.type, properties)\n });\n }, [highlightDivs, properties, theme.palette.type]);\n\n useEffect(() => {\n mermaidPreBlocks.forEach(mermaidPreBlock => {\n // Skip already processed\n if (mermaidPreBlock.style.display === 'none') {\n return\n }\n\n const codeBlock = mermaidPreBlock.querySelector('code')\n if (!codeBlock) {\n return\n }\n\n const diagramText = codeBlock.textContent || ''\n\n makeDiagram(mermaidPreBlock, diagramText, theme.palette.type, properties)\n });\n }, [mermaidPreBlocks, properties, theme.palette.type]);\n\n return null;\n};\n","/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { createPlugin } from '@backstage/core-plugin-api';\nimport {\n createTechDocsAddonExtension,\n TechDocsAddonLocations,\n} from '@backstage/plugin-techdocs-react';\n\nimport { MermaidAddon } from './Mermaid';\nimport type { MermaidProps } from './Mermaid';\n\n/**\n * The TechDocs addons mermaid plugin\n *\n * @public\n */\n\n\nexport const techdocsAddonMermaidPlugin = createPlugin({\n id: 'techdocs-addon-mermaid',\n});\n\n/**\n * TechDocs addon that lets you render Mermaid diagrams\n *\n * @public\n */\n\n export const Mermaid = techdocsAddonMermaidPlugin.provide(\n createTechDocsAddonExtension<MermaidProps>({\n name: 'MermaidDiagram',\n location: TechDocsAddonLocations.Content,\n component: MermaidAddon,\n }),\n);\n"],"names":[],"mappings":";;;;;;AAgBA,MAAM,YACJ,GAAA,6LAAA,CAAA;AAEW,MAAA,aAAA,GAAgB,CAAC,IAA0B,KAAA;AACtD,EAAI,IAAA,IAAA,CAAK,UAAW,CAAA,SAAS,CAAG,EAAA;AAC9B,IAAM,MAAA,yBAAA,GAA4B,IAAK,CAAA,KAAA,CAAM,IAAI,CAAA,CAAA;AAEjD,IAAA,IAAI,0BAA0B,MAAS,GAAA,CAAA;AACrC,MAAO,OAAA,IAAA,CAAK,MAAM,IAAI,CAAA,CAAE,CAAC,CAAE,CAAA,KAAA,CAAM,YAAY,CAAM,KAAA,IAAA,CAAA;AAAA,GACvD;AAEA,EAAO,OAAA,IAAA,CAAK,KAAM,CAAA,YAAY,CAAM,KAAA,IAAA,CAAA;AACtC,CAAA;;ACFgB,SAAA,YAAA,CAAa,kBAA+B,UAAyC,EAAA;AAGnG,EAAA,IAAG,WAAW,MAAQ,EAAA;AACpB,IAAA,OAAO,UAAW,CAAA,MAAA,CAAA;AAAA,GACpB;AAEA,EAAA,IAAG,qBAAqB,OAAS,EAAA;AAC/B,IAAO,OAAA,UAAA,CAAW,eAAe,EAAC,CAAA;AAAA,GACpC;AAEA,EAAA,OAAO,O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xHO,MAAM,6BAA6B,YAAa,CAAA;AAAA,EACrD,EAAI,EAAA,wBAAA;AACN,CAAC,EAAA;AAQO,MAAM,UAAU,0BAA2B,CAAA,OAAA;AAAA,EACjD,4BAA2C,CAAA;AAAA,IACzC,IAAM,EAAA,gBAAA;AAAA,IACN,UAAU,sBAAuB,CAAA,OAAA;AAAA,IACjC,SAAW,EAAA,YAAA;AAAA,GACZ,CAAA;AACH;;;;"}
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../src/Mermaid/hooks.ts","../src/Mermaid/Mermaid.tsx","../src/plugin.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nconst mermaidStart =\n /^(\\s*)(graph|flowchart|sequenceDiagram|classDiagram|stateDiagram|erDiagram|journey|gantt|pie|requirementDiagram|gitGraph|C4Context|C4Container|C4Component|C4Dynamic|C4Deployment|timeline|mindmap|quadrantChart|xychart-beta|block-beta)/gm;\n\nexport const isMermaidCode = (code: string): boolean => {\n if (code.startsWith('%%{init')) {\n const codeSplitByDirectiveStart = code.split('%%');\n\n if (codeSplitByDirectiveStart.length > 2)\n return code.split('%%')[2].match(mermaidStart) !== null;\n }\n\n return code.match(mermaidStart) !== null;\n};\n","/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { useEffect } from 'react';\nimport { PaletteType, useTheme } from '@material-ui/core';\n\nimport { useShadowRootElements } from '@backstage/plugin-techdocs-react';\nimport mermaid from 'mermaid'\nimport { isMermaidCode } from './hooks';\nimport { MermaidProps } from './props';\nimport { BackstageTheme } from '@backstage/theme';\nimport { MermaidConfig } from 'mermaid';\n\nexport function selectConfig(backstagePalette: PaletteType, properties: MermaidProps): MermaidConfig {\n // Theme set directly in the Mermaid configuration takes\n // precedence for backwards-compatibility\n if(properties.config) {\n return properties.config;\n }\n\n if(backstagePalette === 'light') {\n return properties.lightConfig || {};\n }\n\n return Object.assign({ theme: 'dark' }, properties.darkConfig);\n}\n\n/**\n * Show report issue button when text is highlighted\n */\n\nlet diagramId = 0\n\nconst makeDiagram = async (el: HTMLDivElement | HTMLPreElement, diagramText: string, backstagePalette: PaletteType, properties: MermaidProps) => {\n el.style.display = 'none'\n\n const diagramElement = document.createElement('div')\n diagramElement.className = \"mermaid\"\n\n el.parentNode?.insertBefore(diagramElement, el.nextSibling);\n\n const id = `mermaid-${diagramId++}`\n\n const config = selectConfig(backstagePalette, properties);\n if(config) {\n mermaid.initialize(config);\n }\n\n const { svg, bindFunctions } = await mermaid.render(id, diagramText);\n diagramElement.innerHTML = svg\n bindFunctions?.(diagramElement);\n}\n\nexport const MermaidAddon = (properties: MermaidProps) => {\n const highlightTables = useShadowRootElements<HTMLDivElement>(['.highlighttable']);\n const highlightDivs = useShadowRootElements<HTMLDivElement>(['.highlight']);\n const mermaidPreBlocks = useShadowRootElements<HTMLPreElement>(['.mermaid']);\n const theme = useTheme<BackstageTheme>();\n\n useEffect(() => {\n highlightTables.forEach(highlightTable => {\n if (!highlightTable.classList.contains('language-text')) {\n return;\n }\n\n // Skip already processed\n if (highlightTable.style.display === 'none') {\n return\n }\n\n const codeBlock = highlightTable.querySelector('code')\n if (!codeBlock) {\n return\n }\n\n const diagramText = codeBlock.textContent || ''\n\n // Ideally we could detect mermaid based on some annotation, but use a regex for now\n if (!isMermaidCode(diagramText)) {\n return\n }\n\n makeDiagram(highlightTable, diagramText, theme.palette.type, properties)\n });\n }, [highlightTables, properties, theme.palette.type]);\n\n useEffect(() => {\n highlightDivs.forEach(highlightDiv => {\n if (!highlightDiv.classList.contains('language-text')) {\n return;\n }\n\n // Skip already processed\n if (highlightDiv.style.display === 'none') {\n return\n }\n\n // skip mkdocs-material < 9 code blocks (handled above)\n const table = highlightDiv.querySelector('table')\n if (!table) {\n return\n }\n\n const codeBlock = highlightDiv.querySelector('code')\n if (!codeBlock) {\n return\n }\n\n const diagramText = codeBlock.textContent || ''\n\n // Ideally we could detect mermaid based on some annotation, but use a regex for now\n if (!isMermaidCode(diagramText)) {\n return\n }\n\n makeDiagram(highlightDiv, diagramText, theme.palette.type, properties)\n });\n }, [highlightDivs, properties, theme.palette.type]);\n\n useEffect(() => {\n mermaidPreBlocks.forEach(mermaidPreBlock => {\n // Skip already processed\n if (mermaidPreBlock.style.display === 'none') {\n return\n }\n\n const codeBlock = mermaidPreBlock.querySelector('code')\n if (!codeBlock) {\n return\n }\n\n const diagramText = codeBlock.textContent || ''\n\n makeDiagram(mermaidPreBlock, diagramText, theme.palette.type, properties)\n });\n }, [mermaidPreBlocks, properties, theme.palette.type]);\n\n return null;\n};\n","/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { createPlugin } from '@backstage/core-plugin-api';\nimport {\n createTechDocsAddonExtension,\n TechDocsAddonLocations,\n} from '@backstage/plugin-techdocs-react';\n\nimport { MermaidAddon } from './Mermaid';\nimport type { MermaidProps } from './Mermaid';\n\n/**\n * The TechDocs addons mermaid plugin\n *\n * @public\n */\n\n\nexport const techdocsAddonMermaidPlugin = createPlugin({\n id: 'techdocs-addon-mermaid',\n});\n\n/**\n * TechDocs addon that lets you render Mermaid diagrams\n *\n * @public\n */\n\n export const Mermaid = techdocsAddonMermaidPlugin.provide(\n createTechDocsAddonExtension<MermaidProps>({\n name: 'MermaidDiagram',\n location: TechDocsAddonLocations.Content,\n component: MermaidAddon,\n }),\n);\n"],"names":[],"mappings":";;;;;;AAgBA,MAAM,YACJ,GAAA,6OAAA,CAAA;AAEW,MAAA,aAAA,GAAgB,CAAC,IAA0B,KAAA;AACtD,EAAI,IAAA,IAAA,CAAK,UAAW,CAAA,SAAS,CAAG,EAAA;AAC9B,IAAM,MAAA,yBAAA,GAA4B,IAAK,CAAA,KAAA,CAAM,IAAI,CAAA,CAAA;AAEjD,IAAA,IAAI,0BAA0B,MAAS,GAAA,CAAA;AACrC,MAAO,OAAA,IAAA,CAAK,MAAM,IAAI,CAAA,CAAE,CAAC,CAAE,CAAA,KAAA,CAAM,YAAY,CAAM,KAAA,IAAA,CAAA;AAAA,GACvD;AAEA,EAAO,OAAA,IAAA,CAAK,KAAM,CAAA,YAAY,CAAM,KAAA,IAAA,CAAA;AACtC,CAAA;;ACFgB,SAAA,YAAA,CAAa,kBAA+B,UAAyC,EAAA;AAGnG,EAAA,IAAG,WAAW,MAAQ,EAAA;AACpB,IAAA,OAAO,UAAW,CAAA,MAAA,CAAA;AAAA,GACpB;AAEA,EAAA,IAAG,qBAAqB,OAAS,EAAA;AAC/B,IAAO,OAAA,UAAA,CAAW,eAAe,EAAC,CAAA;AAAA,GACpC;AAEA,EAAA,OAAO,OAAO,MAAO,CAAA,EAAE,OAAO,MAAO,EAAA,EAAG,WAAW,UAAU,CAAA,CAAA;AAC/D,CAAA;AAMA,IAAI,SAAY,GAAA,CAAA,CAAA;AAEhB,MAAM,WAAc,G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AA;AAAA,IACzC,IAAM,EAAA,gBAAA;AAAA,IACN,UAAU,sBAAuB,CAAA,OAAA;AAAA,IACjC,SAAW,EAAA,YAAA;AAAA,GACZ,CAAA;AACH;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "backstage-plugin-techdocs-addon-mermaid",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"main": "dist/index.esm.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"license": "MIT",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"@material-ui/core": "^4.12.2",
|
|
35
35
|
"@material-ui/icons": "^4.11.3",
|
|
36
36
|
"@material-ui/lab": "4.0.0-alpha.61",
|
|
37
|
-
"mermaid": "^10.
|
|
37
|
+
"mermaid": "^10.9.1",
|
|
38
38
|
"react-use": "^17.2.4"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
@@ -70,5 +70,6 @@
|
|
|
70
70
|
"type": "git",
|
|
71
71
|
"url": "https://github.com/johanneswuerbach/backstage-plugin-techdocs-addon-mermaid"
|
|
72
72
|
},
|
|
73
|
+
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e",
|
|
73
74
|
"module": "./dist/index.esm.js"
|
|
74
75
|
}
|