From 4ae6c3c1530c8654545cfc0de1a1acb6a7193015 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=CC=88zu=CC=88m=20Eldog=CC=86an?= Date: Thu, 25 Feb 2021 18:40:14 +0300 Subject: [PATCH] feat: add mermaid converter --- README.md | 94 +++++++++++++++++++++----------- package-lock.json | 11 ++++ package.json | 1 + src/converters/index.ts | 1 + src/converters/mermaid-to-svg.ts | 67 +++++++++++++++++++++++ src/index.ts | 4 ++ 6 files changed, 145 insertions(+), 33 deletions(-) create mode 100644 src/converters/index.ts create mode 100644 src/converters/mermaid-to-svg.ts diff --git a/README.md b/README.md index 1b7ceba..264fd68 100644 --- a/README.md +++ b/README.md @@ -34,8 +34,11 @@ Template based scaffolding tool for PostgreSQL. - [log](#log) - [outDir](#outdir) - [Modules](#modules) -- [Namespace: filterFunctions](#namespace-filterfunctions) +- [Namespace: converters](#namespace-converters) - [Functions](#functions-1) + - [mermaidToSVG](#mermaidtosvg) +- [Namespace: filterFunctions](#namespace-filterfunctions) + - [Functions](#functions-2) - [camelCase](#camelcase) - [classCase](#classcase) - [clearDefault](#cleardefault) @@ -82,6 +85,7 @@ Template based scaffolding tool for PostgreSQL. ### Namespaces +- [converters](#modulesconvertersmd) - [filterFunctions](#modulesfilterfunctionsmd) ### Classes @@ -107,7 +111,7 @@ Context provided to render function. | `o` | Db \| DbObject | | `x` | _Record_ | -Defined in: [types/index.ts:53](https://github.com/ozum/pg-generator/blob/0f1e464/src/types/index.ts#L53) +Defined in: [types/index.ts:53](https://github.com/ozum/pg-generator/blob/1b8e16d/src/types/index.ts#L53) --- @@ -115,7 +119,7 @@ Defined in: [types/index.ts:53](https://github.com/ozum/pg-generator/blob/0f1e46 Ƭ **Options**: [_GeneratorOptions_](#interfacesgeneratoroptionsmd) & ClientOptions -Defined in: [types/index.ts:50](https://github.com/ozum/pg-generator/blob/0f1e464/src/types/index.ts#L50) +Defined in: [types/index.ts:50](https://github.com/ozum/pg-generator/blob/1b8e16d/src/types/index.ts#L50) ## Functions @@ -141,7 +145,7 @@ generate(require.resolve("./local-generator"), options); **Returns:** _Promise_ -Defined in: [generate.ts:15](https://github.com/ozum/pg-generator/blob/0f1e464/src/generate.ts#L15) +Defined in: [generate.ts:15](https://github.com/ozum/pg-generator/blob/1b8e16d/src/generate.ts#L15) ▸ **generate**(`generator`: _string_, `subGenerator?`: _string_, `options?`: [_Options_](#options)): _Promise_ @@ -164,7 +168,7 @@ generate(require.resolve("./local-generator"), "sub-generator", options); **Returns:** _Promise_ -Defined in: [generate.ts:27](https://github.com/ozum/pg-generator/blob/0f1e464/src/generate.ts#L27) +Defined in: [generate.ts:27](https://github.com/ozum/pg-generator/blob/1b8e16d/src/generate.ts#L27) # Classes @@ -205,7 +209,7 @@ Creates an instance of PgGenerator. **Returns:** [_PgGenerator_](#classespggeneratormd) -Defined in: [pg-generator.ts:23](https://github.com/ozum/pg-generator/blob/0f1e464/src/pg-generator.ts#L23) +Defined in: [pg-generator.ts:23](https://github.com/ozum/pg-generator/blob/1b8e16d/src/pg-generator.ts#L23) ## Methods @@ -223,7 +227,7 @@ Additionally copies all files in `[rootDir]/files` to the output directory. **Returns:** _Promise_ -Defined in: [pg-generator.ts:47](https://github.com/ozum/pg-generator/blob/0f1e464/src/pg-generator.ts#L47) +Defined in: [pg-generator.ts:47](https://github.com/ozum/pg-generator/blob/1b8e16d/src/pg-generator.ts#L47) # Interfaces @@ -243,7 +247,7 @@ Options for generation and reverse engineering process. Options extends [pg-stru Whether to clear the destination directory before creating files. -Defined in: [types/index.ts:7](https://github.com/ozum/pg-generator/blob/0f1e464/src/types/index.ts#L7) +Defined in: [types/index.ts:7](https://github.com/ozum/pg-generator/blob/1b8e16d/src/types/index.ts#L7) --- @@ -253,7 +257,7 @@ Defined in: [types/index.ts:7](https://github.com/ozum/pg-generator/blob/0f1e464 Extra context data. This data is merged with and overridden by data from context file. -Defined in: [types/index.ts:13](https://github.com/ozum/pg-generator/blob/0f1e464/src/types/index.ts#L13) +Defined in: [types/index.ts:13](https://github.com/ozum/pg-generator/blob/1b8e16d/src/types/index.ts#L13) --- @@ -263,7 +267,7 @@ Defined in: [types/index.ts:13](https://github.com/ozum/pg-generator/blob/0f1e46 Path to a JSON or JS file providing extra context for templates. -Defined in: [types/index.ts:11](https://github.com/ozum/pg-generator/blob/0f1e464/src/types/index.ts#L11) +Defined in: [types/index.ts:11](https://github.com/ozum/pg-generator/blob/1b8e16d/src/types/index.ts#L11) --- @@ -273,7 +277,7 @@ Defined in: [types/index.ts:11](https://github.com/ozum/pg-generator/blob/0f1e46 Whether to log output to console. -Defined in: [types/index.ts:15](https://github.com/ozum/pg-generator/blob/0f1e464/src/types/index.ts#L15) +Defined in: [types/index.ts:15](https://github.com/ozum/pg-generator/blob/1b8e16d/src/types/index.ts#L15) --- @@ -283,10 +287,34 @@ Defined in: [types/index.ts:15](https://github.com/ozum/pg-generator/blob/0f1e46 Path of the output directory. -Defined in: [types/index.ts:9](https://github.com/ozum/pg-generator/blob/0f1e464/src/types/index.ts#L9) +Defined in: [types/index.ts:9](https://github.com/ozum/pg-generator/blob/1b8e16d/src/types/index.ts#L9) # Modules + + +[pg-generator](#readmemd) / converters + +# Namespace: converters + +## Functions + +### mermaidToSVG + +▸ **mermaidToSVG**(`data`: _string_, `options?`: { `link`: }): _string_ + +#### Parameters: + +| Name | Type | +| :------------- | :------- | +| `data` | _string_ | +| `options` | _object_ | +| `options.link` | - | + +**Returns:** _string_ + +Defined in: converters/mermaid-to-svg.ts:8 + [pg-generator](#readmemd) / filterFunctions @@ -317,7 +345,7 @@ camelCase("user-name"); // userName string as camel case. -Defined in: [utils/filter-functions.ts:48](https://github.com/ozum/pg-generator/blob/0f1e464/src/utils/filter-functions.ts#L48) +Defined in: [utils/filter-functions.ts:48](https://github.com/ozum/pg-generator/blob/1b8e16d/src/utils/filter-functions.ts#L48) --- @@ -343,7 +371,7 @@ classCase("user-name"); // UserName string as class case. -Defined in: [utils/filter-functions.ts:74](https://github.com/ozum/pg-generator/blob/0f1e464/src/utils/filter-functions.ts#L74) +Defined in: [utils/filter-functions.ts:74](https://github.com/ozum/pg-generator/blob/1b8e16d/src/utils/filter-functions.ts#L74) --- @@ -373,7 +401,7 @@ clearDefaultValue("'No ''value'' given'"); // "No value 'given'" default value to be used in a template. -Defined in: [utils/filter-functions.ts:26](https://github.com/ozum/pg-generator/blob/0f1e464/src/utils/filter-functions.ts#L26) +Defined in: [utils/filter-functions.ts:26](https://github.com/ozum/pg-generator/blob/1b8e16d/src/utils/filter-functions.ts#L26) --- @@ -399,7 +427,7 @@ dashCase("User Name"); // user-name string as dash case. -Defined in: [utils/filter-functions.ts:100](https://github.com/ozum/pg-generator/blob/0f1e464/src/utils/filter-functions.ts#L100) +Defined in: [utils/filter-functions.ts:100](https://github.com/ozum/pg-generator/blob/1b8e16d/src/utils/filter-functions.ts#L100) --- @@ -418,7 +446,7 @@ Returns given the given database object name as a class name. **Returns:** _string_ -Defined in: [utils/filter-functions.ts:319](https://github.com/ozum/pg-generator/blob/0f1e464/src/utils/filter-functions.ts#L319) +Defined in: [utils/filter-functions.ts:319](https://github.com/ozum/pg-generator/blob/1b8e16d/src/utils/filter-functions.ts#L319) --- @@ -445,7 +473,7 @@ columnTypeModifier(name); // (20) modifier string. -Defined in: [utils/filter-functions.ts:333](https://github.com/ozum/pg-generator/blob/0f1e464/src/utils/filter-functions.ts#L333) +Defined in: [utils/filter-functions.ts:333](https://github.com/ozum/pg-generator/blob/1b8e16d/src/utils/filter-functions.ts#L333) --- @@ -471,7 +499,7 @@ plural("User_name"); // User_name string with lower first case. -Defined in: [utils/filter-functions.ts:139](https://github.com/ozum/pg-generator/blob/0f1e464/src/utils/filter-functions.ts#L139) +Defined in: [utils/filter-functions.ts:139](https://github.com/ozum/pg-generator/blob/1b8e16d/src/utils/filter-functions.ts#L139) --- @@ -503,7 +531,7 @@ Text line 2 **Returns:** _string_ -Defined in: [utils/filter-functions.ts:237](https://github.com/ozum/pg-generator/blob/0f1e464/src/utils/filter-functions.ts#L237) +Defined in: [utils/filter-functions.ts:237](https://github.com/ozum/pg-generator/blob/1b8e16d/src/utils/filter-functions.ts#L237) --- @@ -525,7 +553,7 @@ Pads given string's end with given padding string to complete its length to coun the string padded with padding string. -Defined in: [utils/filter-functions.ts:216](https://github.com/ozum/pg-generator/blob/0f1e464/src/utils/filter-functions.ts#L216) +Defined in: [utils/filter-functions.ts:216](https://github.com/ozum/pg-generator/blob/1b8e16d/src/utils/filter-functions.ts#L216) --- @@ -551,7 +579,7 @@ pascalCase("user-name"); // UserName string as pascal case. -Defined in: [utils/filter-functions.ts:61](https://github.com/ozum/pg-generator/blob/0f1e464/src/utils/filter-functions.ts#L61) +Defined in: [utils/filter-functions.ts:61](https://github.com/ozum/pg-generator/blob/1b8e16d/src/utils/filter-functions.ts#L61) --- @@ -577,7 +605,7 @@ plural("user_name"); // user_names string in plural form. -Defined in: [utils/filter-functions.ts:126](https://github.com/ozum/pg-generator/blob/0f1e464/src/utils/filter-functions.ts#L126) +Defined in: [utils/filter-functions.ts:126](https://github.com/ozum/pg-generator/blob/1b8e16d/src/utils/filter-functions.ts#L126) --- @@ -603,7 +631,7 @@ plural("user_name"); // "user_name" string with quotes. -Defined in: [utils/filter-functions.ts:152](https://github.com/ozum/pg-generator/blob/0f1e464/src/utils/filter-functions.ts#L152) +Defined in: [utils/filter-functions.ts:152](https://github.com/ozum/pg-generator/blob/1b8e16d/src/utils/filter-functions.ts#L152) --- @@ -623,7 +651,7 @@ If given data is a multi line string replcaes new lines with escape characters. the string with escape characters. -Defined in: [utils/filter-functions.ts:293](https://github.com/ozum/pg-generator/blob/0f1e464/src/utils/filter-functions.ts#L293) +Defined in: [utils/filter-functions.ts:293](https://github.com/ozum/pg-generator/blob/1b8e16d/src/utils/filter-functions.ts#L293) --- @@ -649,7 +677,7 @@ plural("Some 'example' text"); // 'some \'example\' text' string with quotes. -Defined in: [utils/filter-functions.ts:165](https://github.com/ozum/pg-generator/blob/0f1e464/src/utils/filter-functions.ts#L165) +Defined in: [utils/filter-functions.ts:165](https://github.com/ozum/pg-generator/blob/1b8e16d/src/utils/filter-functions.ts#L165) --- @@ -675,7 +703,7 @@ singular("user_names"); // user_name string in singular form. -Defined in: [utils/filter-functions.ts:113](https://github.com/ozum/pg-generator/blob/0f1e464/src/utils/filter-functions.ts#L113) +Defined in: [utils/filter-functions.ts:113](https://github.com/ozum/pg-generator/blob/1b8e16d/src/utils/filter-functions.ts#L113) --- @@ -701,7 +729,7 @@ snakeCase("userName"); // user_name string as snake case. -Defined in: [utils/filter-functions.ts:87](https://github.com/ozum/pg-generator/blob/0f1e464/src/utils/filter-functions.ts#L87) +Defined in: [utils/filter-functions.ts:87](https://github.com/ozum/pg-generator/blob/1b8e16d/src/utils/filter-functions.ts#L87) --- @@ -727,7 +755,7 @@ If given data is object or array, converts it to string. converted value. -Defined in: [utils/filter-functions.ts:270](https://github.com/ozum/pg-generator/blob/0f1e464/src/utils/filter-functions.ts#L270) +Defined in: [utils/filter-functions.ts:270](https://github.com/ozum/pg-generator/blob/1b8e16d/src/utils/filter-functions.ts#L270) --- @@ -748,7 +776,7 @@ Vairadic function which strips all of the given strings or database object's nam converted string. -Defined in: [utils/filter-functions.ts:202](https://github.com/ozum/pg-generator/blob/0f1e464/src/utils/filter-functions.ts#L202) +Defined in: [utils/filter-functions.ts:202](https://github.com/ozum/pg-generator/blob/1b8e16d/src/utils/filter-functions.ts#L202) --- @@ -769,7 +797,7 @@ Vairadic function which strips all of the given strings or database object's nam converted string. -Defined in: [utils/filter-functions.ts:176](https://github.com/ozum/pg-generator/blob/0f1e464/src/utils/filter-functions.ts#L176) +Defined in: [utils/filter-functions.ts:176](https://github.com/ozum/pg-generator/blob/1b8e16d/src/utils/filter-functions.ts#L176) --- @@ -790,7 +818,7 @@ Vairadic function which strips all of the given strings or database object's nam converted string. -Defined in: [utils/filter-functions.ts:189](https://github.com/ozum/pg-generator/blob/0f1e464/src/utils/filter-functions.ts#L189) +Defined in: [utils/filter-functions.ts:189](https://github.com/ozum/pg-generator/blob/1b8e16d/src/utils/filter-functions.ts#L189) --- @@ -816,4 +844,4 @@ Returns given array with unique elements by eliminating duplicate values. the array with unique values. -Defined in: [utils/filter-functions.ts:303](https://github.com/ozum/pg-generator/blob/0f1e464/src/utils/filter-functions.ts#L303) +Defined in: [utils/filter-functions.ts:303](https://github.com/ozum/pg-generator/blob/1b8e16d/src/utils/filter-functions.ts#L303) diff --git a/package-lock.json b/package-lock.json index 5e32577..8b52216 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "fdir": "^5.0.0", "ignor": "^1.2.0", "inflection": "^1.12.0", + "js-base64": "^3.6.0", "json5": "^2.2.0", "junk": "^3.1.0", "lodash.get": "^4.4.2", @@ -13276,6 +13277,11 @@ "node": ">= 10.13.0" } }, + "node_modules/js-base64": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/js-base64/-/js-base64-3.6.0.tgz", + "integrity": "sha512-wVdUBYQeY2gY73RIlPrysvpYx+2vheGo8Y1SNQv/BzHToWpAZzJU7Z6uheKMAe+GLSBig5/Ps2nxg/8tRB73xg==" + }, "node_modules/js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", @@ -41217,6 +41223,11 @@ "supports-color": "^7.0.0" } }, + "js-base64": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/js-base64/-/js-base64-3.6.0.tgz", + "integrity": "sha512-wVdUBYQeY2gY73RIlPrysvpYx+2vheGo8Y1SNQv/BzHToWpAZzJU7Z6uheKMAe+GLSBig5/Ps2nxg/8tRB73xg==" + }, "js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", diff --git a/package.json b/package.json index 55c7b74..49efe1d 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,7 @@ "fdir": "^5.0.0", "ignor": "^1.2.0", "inflection": "^1.12.0", + "js-base64": "^3.6.0", "json5": "^2.2.0", "junk": "^3.1.0", "lodash.get": "^4.4.2", diff --git a/src/converters/index.ts b/src/converters/index.ts new file mode 100644 index 0000000..106c572 --- /dev/null +++ b/src/converters/index.ts @@ -0,0 +1 @@ +export * from "./mermaid-to-svg"; diff --git a/src/converters/mermaid-to-svg.ts b/src/converters/mermaid-to-svg.ts new file mode 100644 index 0000000..9826d96 --- /dev/null +++ b/src/converters/mermaid-to-svg.ts @@ -0,0 +1,67 @@ +/** + * This converter is from: https://github.com/superj80820/mermaid-js-converter + */ + +/* eslint-disable no-param-reassign */ +import Base64 from "js-base64"; + +export function mermaidToSVG(data: string, { link = false } = {}): string { + const matchData = data.match(/```mermaid(.|\n)*?```/gm); + if (matchData === null) return data; + + const jsonStrings = matchData + .map((item: string) => item.replace("```mermaid", "").replace("```", "")) + // Workaround for classdiagram + .map((item: string) => + item.startsWith("\nclass") || item.startsWith("\ngantt") || item.startsWith("\nerDiagram") || item.startsWith("\njourney") + ? item.substr(1, item.length - 1) + : item + ) + .map((item: any) => + item.replace(/\s*?\w+?\s+/, "").length === 0 + ? undefined + : JSON.stringify({ + code: item, + mermaid: { + theme: "default", + }, + }) + ) + .map((item: any) => { + if (item === undefined) return ""; + const jsonString = Base64.encodeURI(item); + return link + ? `[![](https://mermaid.ink/svg/${jsonString})](https://mermaid-js.github.io/mermaid-live-editor/#/edit/${jsonString})` + : `![](https://mermaid.ink/svg/${jsonString})`; + }); + + let changeMd = data; + matchData.forEach((item: any, index: string | number) => { + changeMd = changeMd.replace(item, jsonStrings[index as any] as string); + }); + + return changeMd; +} + +// export function SVGToMermaid(data: string): string { +// const matchData = data.match(/\[?!\[\]\(https:\/\/mermaid\.ink\/svg\/(.|\n)*?\)\n/gm); +// if (matchData === null) return data; +// const encodedURIs = matchData.map((item: string) => { +// item = item.replace("[![](https://mermaid.ink/img/", ""); +// // return item.substr(0, item.indexOf(")](https://mermaid-js.github.io/mermaid-live-editor/#/")); +// return item.substr(0, item.indexOf(")]")); +// }); + +// let originMd = data; + +// matchData.forEach((item: any, index: string | number) => { +// // Workaround for classdiagram about assignment let +// let { code } = JSON.parse(Base64.decode(encodedURIs[index as any])); +// // Workaround for classdiagram +// if (code.startsWith("class") || code.startsWith("gantt") || code.startsWith("erDiagram") || code.startsWith("journey")) +// code = `\n${code}`; +// originMd = originMd.replace(item, `\`\`\`mermaid${code}\`\`\`\n`); +// }); + +// return originMd; +// } diff --git a/src/index.ts b/src/index.ts index 86fdfbd..b047e8e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,9 @@ export { PgGenerator } from "./pg-generator"; export { generate } from "./generate"; + +// Generator Helpers export * as filterFunctions from "./utils/filter-functions"; +export * as converters from "./converters"; +// Types export type { Context, GeneratorOptions, Options } from "./types/index";