backstage-plugin-techdocs-addon-mermaid 0.4.0 → 0.4.2

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/dist/index.d.ts CHANGED
@@ -2,6 +2,8 @@ import * as _backstage_core_plugin_api from '@backstage/core-plugin-api';
2
2
  import { MermaidConfig } from 'mermaid';
3
3
 
4
4
  declare type MermaidProps = {
5
+ lightConfig?: MermaidConfig;
6
+ darkConfig?: MermaidConfig;
5
7
  config?: MermaidConfig;
6
8
  };
7
9
 
@@ -10,7 +12,7 @@ declare type MermaidProps = {
10
12
  *
11
13
  * @public
12
14
  */
13
- declare const techdocsAddonMermaidPlugin: _backstage_core_plugin_api.BackstagePlugin<{}, {}, {}>;
15
+ declare const techdocsAddonMermaidPlugin: _backstage_core_plugin_api.BackstagePlugin<{}, {}>;
14
16
  /**
15
17
  * TechDocs addon that lets you render Mermaid diagrams
16
18
  *
package/dist/index.esm.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { createPlugin } from '@backstage/core-plugin-api';
2
2
  import { useShadowRootElements, createTechDocsAddonExtension, TechDocsAddonLocations } from '@backstage/plugin-techdocs-react';
3
3
  import { useEffect } from 'react';
4
+ import { useTheme } from '@material-ui/core';
4
5
  import mermaid from 'mermaid';
5
6
 
6
7
  const mermaidStart = /^(\s*)(graph|flowchart|sequenceDiagram|classDiagram|stateDiagram|erDiagram|journey|gantt|pie|requirementDiagram|gitGraph)/;
@@ -8,9 +9,19 @@ const isMermaidCode = (code) => {
8
9
  return code.match(mermaidStart) !== null;
9
10
  };
10
11
 
12
+ function selectConfig(backstagePalette, properties) {
13
+ if (properties.config) {
14
+ return properties.config;
15
+ }
16
+ if (backstagePalette === "light") {
17
+ return properties.lightConfig || {};
18
+ }
19
+ return properties.darkConfig || {};
20
+ }
11
21
  let diagramId = 0;
12
22
  const MermaidAddon = (properties) => {
13
23
  const highlightTables = useShadowRootElements([".highlighttable"]);
24
+ const theme = useTheme();
14
25
  useEffect(() => {
15
26
  highlightTables.forEach((highlightTable) => {
16
27
  var _a;
@@ -30,27 +41,26 @@ const MermaidAddon = (properties) => {
30
41
  diagramElement.className = "mermaid";
31
42
  (_a = highlightTable.parentNode) == null ? void 0 : _a.insertBefore(diagramElement, highlightTable.nextSibling);
32
43
  const id = `mermaid-${diagramId++}`;
33
- if (properties == null ? void 0 : properties.config) {
34
- mermaid.initialize(properties == null ? void 0 : properties.config);
44
+ const config = selectConfig(theme.palette.type, properties);
45
+ if (config) {
46
+ mermaid.initialize(config);
35
47
  }
36
48
  mermaid.render(id, diagramText, (svgGraph) => {
37
49
  diagramElement.innerHTML = svgGraph;
38
50
  });
39
51
  });
40
- }, [highlightTables, properties]);
52
+ }, [highlightTables, properties, theme.palette.type]);
41
53
  return null;
42
54
  };
43
55
 
44
56
  const techdocsAddonMermaidPlugin = createPlugin({
45
57
  id: "techdocs-addon-mermaid"
46
58
  });
47
- const Mermaid = techdocsAddonMermaidPlugin.provide(
48
- createTechDocsAddonExtension({
49
- name: "MermaidDiagram",
50
- location: TechDocsAddonLocations.Content,
51
- component: MermaidAddon
52
- })
53
- );
59
+ const Mermaid = techdocsAddonMermaidPlugin.provide(createTechDocsAddonExtension({
60
+ name: "MermaidDiagram",
61
+ location: TechDocsAddonLocations.Content,
62
+ component: MermaidAddon
63
+ }));
54
64
 
55
65
  export { Mermaid, techdocsAddonMermaidPlugin };
56
66
  //# sourceMappingURL=index.esm.js.map
@@ -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 = /^(\\s*)(graph|flowchart|sequenceDiagram|classDiagram|stateDiagram|erDiagram|journey|gantt|pie|requirementDiagram|gitGraph)/;\n\nexport const isMermaidCode = (code: string): boolean => {\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 { useShadowRootElements } from '@backstage/plugin-techdocs-react';\nimport mermaid from 'mermaid'\nimport { isMermaidCode } from './hooks';\nimport { MermaidProps } from './props';\n\n\n/**\n * Show report issue button when text is highlighted\n */\n\nlet diagramId = 0\n\nexport const MermaidAddon = (properties: MermaidProps) => {\n const highlightTables = useShadowRootElements<HTMLDivElement>(['.highlighttable']);\n\n useEffect(() => {\n highlightTables.forEach(highlightTable => {\n // Mermaid is currently classified as \"text\"\n // https://github.com/backstage/backstage/pull/11447 which adds this class isn't\n // shipped yet.\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.innerText\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 highlightTable.style.display = 'none'\n\n const diagramElement = document.createElement('div')\n diagramElement.className = \"mermaid\"\n\n highlightTable.parentNode?.insertBefore(diagramElement, highlightTable.nextSibling);\n\n const id = `mermaid-${diagramId++}`\n\n if (properties?.config) {\n mermaid.initialize(properties?.config);\n }\n mermaid.render(id, diagramText, (svgGraph: string) => {\n diagramElement.innerHTML = svgGraph\n });\n });\n }, [highlightTables, properties]);\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":";;;;;AAAA,MAAM,YAAY,GAAG,2HAA2H,CAAC;AAC1I,MAAM,aAAa,GAAG,CAAC,IAAI,KAAK;AACvC,EAAE,OAAO,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,KAAK,IAAI,CAAC;AAC3C,CAAC;;ACCD,IAAI,SAAS,GAAG,CAAC,CAAC;AACX,MAAM,YAAY,GAAG,CAAC,UAAU,KAAK;AAC5C,EAAE,MAAM,eAAe,GAAG,qBAAqB,CAAC,CAAC,iBAAiB,CAAC,CAAC,CAAC;AACrE,EAAE,SAAS,CAAC,MAAM;AAClB,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KAAK,CAAC,CAAC;AACP,GAAG,EAAE,CAAC,eAAe,EAAE,UAAU,CAAC,CAAC,CAAC;AACpC,EAAE,OAAO,IAAI,CAAC;AACd,CAAC;;AC7BW,MAAC,0BAA0B,GAAG,YAAY,CAAC;AACvD,EAAE,EAAE,EAAE,wBAAwB;AAC9B,CAAC,EAAE;AACS,MAAC,OAAO,GAAG,0BAA0B,CAAC,OAAO;AACzD,EAAE,4BAA4B,CAAC;AAC/B,IAAI,IAAI,EAAE,gBAAgB;AAC1B,IAAI,QAAQ,EAAE,sBAAsB,CAAC,OAAO;AAC5C,IAAI,SAAS,EAAE,YAAY;AAC3B,GAAG,CAAC;AACJ;;;;"}
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 = /^(\\s*)(graph|flowchart|sequenceDiagram|classDiagram|stateDiagram|erDiagram|journey|gantt|pie|requirementDiagram|gitGraph)/;\n\nexport const isMermaidCode = (code: string): boolean => {\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 properties.darkConfig || {};\n}\n\n/**\n * Show report issue button when text is highlighted\n */\n\nlet diagramId = 0\n\nexport const MermaidAddon = (properties: MermaidProps) => {\n const highlightTables = useShadowRootElements<HTMLDivElement>(['.highlighttable']);\n const theme = useTheme<BackstageTheme>();\n\n useEffect(() => {\n highlightTables.forEach(highlightTable => {\n // Mermaid is currently classified as \"text\"\n // https://github.com/backstage/backstage/pull/11447 which adds this class isn't\n // shipped yet.\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.innerText\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 highlightTable.style.display = 'none'\n\n const diagramElement = document.createElement('div')\n diagramElement.className = \"mermaid\"\n\n highlightTable.parentNode?.insertBefore(diagramElement, highlightTable.nextSibling);\n\n const id = `mermaid-${diagramId++}`\n\n const config = selectConfig(theme.palette.type, properties);\n if(config) {\n mermaid.initialize(config);\n }\n\n mermaid.render(id, diagramText, (svgGraph: string) => {\n diagramElement.innerHTML = svgGraph\n });\n });\n }, [highlightTables, 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":";;;;;;AAAA,MAAM,YAAY,GAAG,2HAA2H,CAAC;AAC1I,MAAM,aAAa,GAAG,CAAC,IAAI,KAAK;AACvC,EAAE,OAAO,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,KAAK,IAAI,CAAC;AAC3C,CAAC;;ACEM,SAAS,YAAY,CAAC,gBAAgB,EAAE,UAAU,EAAE;AAC3D,EAAE,IAAI,UAAU,CAAC,MAAM,EAAE;AACzB,IAAI,OAAO,UAAU,CAAC,MAAM,CAAC;AAC7B,GAAG;AACH,EAAE,IAAI,gBAAgB,KAAK,OAAO,EAAE;AACpC,IAAI,OAAO,UAAU,CAAC,WAAW,IAAI,EAAE,CAAC;AACxC,GAAG;AACH,EAAE,OAAO,UAAU,CAAC,UAAU,IAAI,EAAE,CAAC;AACrC,CAAC;AACD,IAAI,SAAS,GAAG,CAAC,CAAC;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C,MAAM,CAAC,CAAC;AACnC,OAAO;AACP,MAAM,OAAO,CAAC,MAAM,CAAC,EAAE,EAAE,WAAW,EAAE,CAAC,QAAQ,KAAK;AACpD,QAAQ,cAAc,CAAC,SAAS,GAAG,QAAQ,CAAC;AAC5C,OAAO,CAAC,CAAC;AACT,KAAK,CAAC,CAAC;AACP,GAAG,EAAE,CAAC,eAAe,EAAE,UAAU,EAAE,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;AACxD,EAAE,OAAO,IAAI,CAAC;AACd,CAAC;;ACzCW,MAAC,0BAA0B,GAAG,YAAY,CAAC;AACvD,EAAE,EAAE,EAAE,wBAAwB;AAC9B,CAAC,EAAE;AACS,MAAC,OAAO,GAAG,0BAA0B,CAAC,OAAO,CAAC,4BAA4B,CAAC;AACvF,EAAE,IAAI,EAAE,gBAAgB;AACxB,EAAE,QAAQ,EAAE,sBAAsB,CAAC,OAAO;AAC1C,EAAE,SAAS,EAAE,YAAY;AACzB,CAAC,CAAC;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backstage-plugin-techdocs-addon-mermaid",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "main": "dist/index.esm.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "license": "MIT",
package/src/index.ts ADDED
@@ -0,0 +1,16 @@
1
+ /*
2
+ * Copyright 2022 The Backstage Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ export { techdocsAddonMermaidPlugin, Mermaid } from './plugin';