@diplodoc/mermaid-extension 0.0.1

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/.babelrc ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "presets": [
3
+ "env"
4
+ ]
5
+ }
package/lib/index.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ declare module "hooks/useMermaid" {
2
+ import type { MermaidConfig } from 'mermaid';
3
+ export function useMermaid(): (config: MermaidConfig) => Promise<void>;
4
+ }
5
+ declare module "hooks/index" {
6
+ export { useMermaid } from "hooks/useMermaid";
7
+ }
8
+ declare module "plugin/transform" {
9
+ import type { MarkdownItPluginCb } from '@doc-tools/transform/lib/plugins/typings';
10
+ export function transform({ runtime, bundle }?: {
11
+ runtime?: string | undefined;
12
+ bundle?: boolean | undefined;
13
+ }): MarkdownItPluginCb;
14
+ }
15
+ declare module "plugin/index" {
16
+ export { transform } from "plugin/transform";
17
+ }
18
+ declare module "runtime/index" { }
package/lib/index.js ADDED
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/plugin/index.ts
21
+ var plugin_exports = {};
22
+ __export(plugin_exports, {
23
+ transform: () => transform
24
+ });
25
+ module.exports = __toCommonJS(plugin_exports);
26
+
27
+ // src/plugin/transform.ts
28
+ function transform({ runtime = "_assets/mermaid-plugin.js", bundle = true } = {}) {
29
+ const replaceFence = function replaceFence2({ tokens, env }) {
30
+ tokens.forEach((token) => {
31
+ if (token.type !== "fence" || !token.info.match(/^\s*mermaid(\s*|$)/)) {
32
+ return;
33
+ }
34
+ token.type = "mermaid";
35
+ token.attrSet("class", `mermaid yfm-mermaid`);
36
+ console.log(env);
37
+ if (!env.meta) {
38
+ env.meta = {};
39
+ }
40
+ env.meta.script = env.meta.script || [];
41
+ if (env.meta.script.indexOf(runtime) === -1) {
42
+ env.meta.script.push(runtime);
43
+ if (bundle) {
44
+ env.meta.bundle = env.meta.bundle || [];
45
+ env.meta.bundle.push({
46
+ from: "@diplodoc/mermaid-plugin/dist/runtime.js",
47
+ to: runtime
48
+ });
49
+ }
50
+ }
51
+ });
52
+ };
53
+ return function(md) {
54
+ try {
55
+ md.core.ruler.after("fence", "mermaid", replaceFence);
56
+ } catch (e) {
57
+ md.core.ruler.push("mermaid", replaceFence);
58
+ }
59
+ md.renderer.rules.mermaid = (tokens, idx) => {
60
+ const token = tokens[idx];
61
+ const code = encodeURIComponent(token.content.trim());
62
+ return `<div class="mermaid" data-content="${code}"></div>`;
63
+ };
64
+ };
65
+ }
66
+ // Annotate the CommonJS export names for ESM import in node:
67
+ 0 && (module.exports = {
68
+ transform
69
+ });
70
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/plugin/index.ts", "../src/plugin/transform.ts"],
4
+ "sourcesContent": ["export { transform } from './transform';", "import type {MarkdownIt} from '@doc-tools/transform/lib/typings';\nimport type {MarkdownItPluginCb} from '@doc-tools/transform/lib/plugins/typings';\nimport type ParserCore from 'markdown-it/lib/parser_core';\n\nexport function transform({ runtime = '_assets/mermaid-plugin.js', bundle = true } = {}) {\n const replaceFence: ParserCore.RuleCore = function replaceFence({tokens, env}) {\n tokens.forEach((token) => {\n if (token.type !== 'fence' || !token.info.match(/^\\s*mermaid(\\s*|$)/)) {\n return;\n }\n\n token.type = 'mermaid';\n token.attrSet('class', `mermaid yfm-mermaid`);\n\n console.log(env)\n if (!env.meta) {\n env.meta = {};\n }\n\n env.meta.script = env.meta.script || [];\n\n if (env.meta.script.indexOf(runtime) === -1) {\n env.meta.script.push(runtime);\n\n if (bundle) {\n env.meta.bundle = env.meta.bundle || [];\n env.meta.bundle.push({\n from: '@diplodoc/mermaid-plugin/dist/runtime.js',\n to: runtime\n });\n }\n }\n });\n };\n\n return function(md: MarkdownIt) {\n try {\n md.core.ruler.after('fence', 'mermaid', replaceFence);\n } catch (e) {\n md.core.ruler.push('mermaid', replaceFence);\n }\n\n md.renderer.rules.mermaid = (tokens, idx) => {\n const token = tokens[idx];\n const code = encodeURIComponent(token.content.trim());\n\n return `<div class=\"mermaid\" data-content=\"${code}\"></div>`;\n };\n } as MarkdownItPluginCb;\n}"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACIO,SAAS,UAAU,EAAE,UAAU,6BAA6B,SAAS,KAAK,IAAI,CAAC,GAAG;AACrF,QAAM,eAAoC,SAASA,cAAa,EAAC,QAAQ,IAAG,GAAG;AAC3E,WAAO,QAAQ,CAAC,UAAW;AACvB,UAAI,MAAM,SAAS,WAAW,CAAC,MAAM,KAAK,MAAM,oBAAoB,GAAG;AACnE;AAAA,MACJ;AAEA,YAAM,OAAO;AACb,YAAM,QAAQ,SAAS,qBAAqB;AAE5C,cAAQ,IAAI,GAAG;AACf,UAAI,CAAC,IAAI,MAAM;AACX,YAAI,OAAO,CAAC;AAAA,MAChB;AAEA,UAAI,KAAK,SAAS,IAAI,KAAK,UAAU,CAAC;AAEtC,UAAI,IAAI,KAAK,OAAO,QAAQ,OAAO,MAAM,IAAI;AACzC,YAAI,KAAK,OAAO,KAAK,OAAO;AAE5B,YAAI,QAAQ;AACR,cAAI,KAAK,SAAS,IAAI,KAAK,UAAU,CAAC;AACtC,cAAI,KAAK,OAAO,KAAK;AAAA,YACjB,MAAM;AAAA,YACN,IAAI;AAAA,UACR,CAAC;AAAA,QACL;AAAA,MACJ;AAAA,IACJ,CAAC;AAAA,EACL;AAEA,SAAO,SAAS,IAAgB;AAC5B,QAAI;AACA,SAAG,KAAK,MAAM,MAAM,SAAS,WAAW,YAAY;AAAA,IACxD,SAAS,GAAP;AACE,SAAG,KAAK,MAAM,KAAK,WAAW,YAAY;AAAA,IAC9C;AAEA,OAAG,SAAS,MAAM,UAAU,CAAC,QAAQ,QAAQ;AACzC,YAAM,QAAQ,OAAO,GAAG;AACxB,YAAM,OAAO,mBAAmB,MAAM,QAAQ,KAAK,CAAC;AAEpD,aAAO,sCAAsC;AAAA,IACjD;AAAA,EACJ;AACJ;",
6
+ "names": ["replaceFence"]
7
+ }
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@diplodoc/mermaid-extension",
3
+ "version": "0.0.1",
4
+ "description": "Mermaid plugin for Diplodoc transformer and builder",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
7
+ "scripts": {
8
+ "build": "run-p build:*",
9
+ "build:lib": "esbuild src/plugin/index.ts --bundle --platform=node --outfile=lib/index.js --sourcemap",
10
+ "build:hooks": "esbuild src/hooks/index.ts --bundle --platform=neutral --target=es6 --outfile=hooks/index.js --external:react --sourcemap",
11
+ "build:runtime": "esbuild src/runtime/index.ts --bundle --minify --sourcemap --outfile=dist/runtime.js",
12
+ "build:declarations": "tsc --emitDeclarationOnly --outFile lib/index.d.ts",
13
+ "test": "jest"
14
+ },
15
+ "keywords": [
16
+ "mermaid",
17
+ "markdown-it"
18
+ ],
19
+ "author": "",
20
+ "license": "ISC",
21
+ "optionalDependencies": {
22
+ "react": "^18.2.0"
23
+ },
24
+ "devDependencies": {
25
+ "@doc-tools/transform": "^2.16.4",
26
+ "@types/markdown-it": "^12.2.3",
27
+ "@types/react": "^18.0.35",
28
+ "esbuild": "^0.17.12",
29
+ "jest": "^29.5.0",
30
+ "markdown-it": "^13.0.1",
31
+ "npm-run-all": "^4.1.5",
32
+ "react": "^18.2.0",
33
+ "typescript": "^5.0.2"
34
+ },
35
+ "dependencies": {
36
+ "mermaid": "^10.0.2",
37
+ "ts-dedent": "^2.2.0"
38
+ }
39
+ }
@@ -0,0 +1,12 @@
1
+ import type mermaid from 'mermaid';
2
+
3
+ type Callback = (exposed: {
4
+ run: typeof mermaid.run;
5
+ initialize: typeof mermaid.initialize;
6
+ }) => void;
7
+
8
+ declare global {
9
+ interface Window {
10
+ mermaidJsonp: Callback[];
11
+ }
12
+ }
@@ -0,0 +1 @@
1
+ export { transform } from './transform';
@@ -0,0 +1,46 @@
1
+ import type {MarkdownIt} from '@doc-tools/transform/lib/typings';
2
+ import type {MarkdownItPluginCb} from '@doc-tools/transform/lib/plugins/typings';
3
+ import type ParserCore from 'markdown-it/lib/parser_core';
4
+
5
+ export function transform({ runtime = '_assets/mermaid-extension.js', bundle = true } = {}) {
6
+ const replaceFence: ParserCore.RuleCore = function replaceFence({tokens, env}) {
7
+ tokens.forEach((token) => {
8
+ if (token.type !== 'fence' || !token.info.match(/^\s*mermaid(\s*|$)/)) {
9
+ return;
10
+ }
11
+
12
+ token.type = 'mermaid';
13
+ token.attrSet('class', `mermaid yfm-mermaid`);
14
+
15
+ env.meta = env.meta || {};
16
+ env.meta.script = env.meta.script || [];
17
+
18
+ if (env.meta.script.indexOf(runtime) === -1) {
19
+ env.meta.script.push(runtime);
20
+
21
+ if (bundle) {
22
+ env.meta.bundle = env.meta.bundle || [];
23
+ env.meta.bundle.push({
24
+ from: '@diplodoc/mermaid-extension/dist/runtime.js',
25
+ to: runtime
26
+ });
27
+ }
28
+ }
29
+ });
30
+ };
31
+
32
+ return function(md: MarkdownIt) {
33
+ try {
34
+ md.core.ruler.after('fence', 'mermaid', replaceFence);
35
+ } catch (e) {
36
+ md.core.ruler.push('mermaid', replaceFence);
37
+ }
38
+
39
+ md.renderer.rules.mermaid = (tokens, idx) => {
40
+ const token = tokens[idx];
41
+ const code = encodeURIComponent(token.content.trim());
42
+
43
+ return `<div class="mermaid" data-content="${code}"></div>`;
44
+ };
45
+ } as MarkdownItPluginCb;
46
+ }
@@ -0,0 +1,67 @@
1
+ import mermaid from 'mermaid';
2
+ import dedent from 'ts-dedent';
3
+
4
+ mermaid.initialize({
5
+ startOnLoad: false,
6
+ theme: 'forest'
7
+ });
8
+
9
+ const jsonp = window.mermaidJsonp = window.mermaidJsonp || [];
10
+ const queue = jsonp.splice(0, jsonp.length);
11
+
12
+ jsonp.push = function(...args) {
13
+ args.forEach((callback) => {
14
+ queue.push(callback);
15
+ unqueue();
16
+ });
17
+
18
+ return queue.length;
19
+ };
20
+
21
+ let processing = false;
22
+
23
+ function unqueue() {
24
+ if (!processing) {
25
+ next();
26
+ }
27
+ }
28
+
29
+ async function next(): Promise<void> {
30
+ processing = true;
31
+
32
+ const callback = queue.shift();
33
+ if (callback) {
34
+ await callback({
35
+ run: async () => {
36
+ const nodes = document.querySelectorAll('.mermaid');
37
+
38
+ for (const element of Array.from(nodes)) {
39
+ const id = `mermaid-${ Date.now() }`;
40
+ const content = element.getAttribute('data-content') || '';
41
+ const text = dedent(decodeURIComponent(content))
42
+ .trim()
43
+ .replace(/<br\s*\/?>/gi, '<br/>');
44
+
45
+ const { svg, bindFunctions } = await mermaid.render(id, text, element);
46
+ element.innerHTML = svg;
47
+
48
+ if (bindFunctions) {
49
+ bindFunctions(element);
50
+ }
51
+ }
52
+ },
53
+ initialize: (config) => {
54
+ mermaid.initialize({
55
+ startOnLoad: false,
56
+ ...config
57
+ });
58
+ },
59
+ });
60
+
61
+ return next();
62
+ }
63
+
64
+ processing = false;
65
+ }
66
+
67
+ unqueue();
package/tsconfig.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2022",
4
+ "module": "es2022",
5
+ "moduleResolution": "node",
6
+ "esModuleInterop": true,
7
+ "forceConsistentCasingInFileNames": true,
8
+ "strict": true,
9
+ "skipLibCheck": true,
10
+ "pretty": true,
11
+ "newLine": "lf",
12
+ "stripInternal": true,
13
+ "noImplicitReturns": true,
14
+ "noFallthroughCasesInSwitch": true,
15
+ "noEmitOnError": true,
16
+ "experimentalDecorators": true,
17
+ "allowJs": true,
18
+ "declaration": true
19
+ },
20
+ "include": [
21
+ "src"
22
+ ],
23
+ "ts-node": {
24
+ "esm": true,
25
+ "transpileOnly": true
26
+ }
27
+ }