Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: clean up code in mermaidAPI render() and write specs/tests #3684

Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
d248952
render: constants
weedySeaDragon Oct 15, 2022
8aaa7b1
specs: encodeEntities, decodeEntities
weedySeaDragon Oct 15, 2022
978bf4e
render: define const iFrameId, enclosingDivID and _selector to use in…
weedySeaDragon Oct 15, 2022
2e86031
comment the main steps (prepare to break into functions that can be t…
weedySeaDragon Oct 15, 2022
5aff154
more meaningful var names; move related lines together; const idSelector
weedySeaDragon Oct 15, 2022
a26673c
const isSandboxed, isLooseSecurityLevel, fontFamily; a few more CONSTs
weedySeaDragon Oct 15, 2022
d106d3d
add MockedD3.ts
weedySeaDragon Oct 16, 2022
a3b8c30
functions and specs: createCssStyles, appendDivSvgG,cleanUpSvgCode, p…
weedySeaDragon Oct 16, 2022
166dca5
functions and specs: createUserstyles; minor changes
weedySeaDragon Oct 16, 2022
fcba29f
functions and specs: removeExistingElements
weedySeaDragon Oct 16, 2022
ea86ef3
change spec descriptions to active voice (= shorter b/c 'should' isn'…
weedySeaDragon Oct 16, 2022
377b22e
fix: Type of DiagramStyleClassDef, general cleanup
sidharthv96 Oct 19, 2022
f4fa680
Merge branch 'develop' into pr/weedySeaDragon/3684
sidharthv96 Oct 19, 2022
3e76edc
chore: Fix cspell
sidharthv96 Oct 19, 2022
c413119
merge MERMAID/develop into branch; fix ts/es lint errors
weedySeaDragon Oct 25, 2022
6563c02
merge upstream/develop into branch
weedySeaDragon Nov 1, 2022
b206026
use lodash isEmpty instead of method defined in utils
weedySeaDragon Nov 1, 2022
469d0f8
merge fix: get classDefs only if diagram is in CLASSDEF_DIAGRAMS
weedySeaDragon Nov 1, 2022
46ee4e4
fix typo
weedySeaDragon Nov 1, 2022
6d2441d
only call getClasses if the diagram renderer supports it
weedySeaDragon Nov 1, 2022
794b642
merge develop into branch. update cSpell.json; run pnpm install
weedySeaDragon Nov 3, 2022
607fe88
Merge remote-tracking branch 'MERMAID/develop' into chore/3648-mermai…
weedySeaDragon Nov 3, 2022
750029f
merge MERMAID/develop
weedySeaDragon Nov 9, 2022
791dbe0
fix: `sourceLinkTemplate` in typedoc
sidharthv96 Nov 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion cSpell.json
Expand Up @@ -79,7 +79,10 @@
"edgechromium",
"statediagram",
"Brolin",
"Greywolf"
"Greywolf",
"stylis",
"svgXlink",
"xlink"
],
"patterns": [
{
Expand Down
22 changes: 11 additions & 11 deletions docs/config/setup/modules/config.md
Expand Up @@ -14,7 +14,7 @@

#### Defined in

[config.ts:7](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.ts#L7)
[config.ts:7](https://github.com/weedySeaDragon/mermaid/blob/master/packages/mermaid/src/config.ts#L7)

## Functions

Expand All @@ -36,7 +36,7 @@ Pushes in a directive to the configuration

#### Defined in

[config.ts:191](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.ts#L191)
[config.ts:191](https://github.com/weedySeaDragon/mermaid/blob/master/packages/mermaid/src/config.ts#L191)

---

Expand All @@ -60,7 +60,7 @@ The currentConfig

#### Defined in

[config.ts:136](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.ts#L136)
[config.ts:136](https://github.com/weedySeaDragon/mermaid/blob/master/packages/mermaid/src/config.ts#L136)

---

Expand All @@ -84,7 +84,7 @@ The siteConfig

#### Defined in

[config.ts:96](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.ts#L96)
[config.ts:96](https://github.com/weedySeaDragon/mermaid/blob/master/packages/mermaid/src/config.ts#L96)

---

Expand Down Expand Up @@ -118,7 +118,7 @@ The siteConfig

#### Defined in

[config.ts:223](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.ts#L223)
[config.ts:223](https://github.com/weedySeaDragon/mermaid/blob/master/packages/mermaid/src/config.ts#L223)

---

Expand Down Expand Up @@ -147,7 +147,7 @@ options in-place

#### Defined in

[config.ts:151](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.ts#L151)
[config.ts:151](https://github.com/weedySeaDragon/mermaid/blob/master/packages/mermaid/src/config.ts#L151)

---

Expand All @@ -167,7 +167,7 @@ options in-place

#### Defined in

[config.ts:75](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.ts#L75)
[config.ts:75](https://github.com/weedySeaDragon/mermaid/blob/master/packages/mermaid/src/config.ts#L75)

---

Expand Down Expand Up @@ -199,7 +199,7 @@ The currentConfig merged with the sanitized conf

#### Defined in

[config.ts:113](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.ts#L113)
[config.ts:113](https://github.com/weedySeaDragon/mermaid/blob/master/packages/mermaid/src/config.ts#L113)

---

Expand Down Expand Up @@ -232,7 +232,7 @@ The new siteConfig

#### Defined in

[config.ts:61](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.ts#L61)
[config.ts:61](https://github.com/weedySeaDragon/mermaid/blob/master/packages/mermaid/src/config.ts#L61)

---

Expand All @@ -253,7 +253,7 @@ The new siteConfig

#### Defined in

[config.ts:14](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.ts#L14)
[config.ts:14](https://github.com/weedySeaDragon/mermaid/blob/master/packages/mermaid/src/config.ts#L14)

---

Expand All @@ -273,4 +273,4 @@ The new siteConfig

#### Defined in

[config.ts:79](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.ts#L79)
[config.ts:79](https://github.com/weedySeaDragon/mermaid/blob/master/packages/mermaid/src/config.ts#L79)
4 changes: 2 additions & 2 deletions docs/config/setup/modules/defaultConfig.md
Expand Up @@ -14,7 +14,7 @@

#### Defined in

[defaultConfig.ts:1882](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts#L1882)
[defaultConfig.ts:1882](https://github.com/weedySeaDragon/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts#L1882)

---

Expand Down Expand Up @@ -53,4 +53,4 @@ A description of each option follows below.

#### Defined in

[defaultConfig.ts:33](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts#L33)
[defaultConfig.ts:33](https://github.com/weedySeaDragon/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts#L33)
205 changes: 195 additions & 10 deletions docs/config/setup/modules/mermaidAPI.md
Expand Up @@ -16,7 +16,7 @@ Renames and re-exports [mermaidAPI](mermaidAPI.md#mermaidapi)

### mermaidAPI

• `Const` **mermaidAPI**: `Readonly`<{ `defaultConfig`: `MermaidConfig` = configApi.defaultConfig; `getConfig`: () => `MermaidConfig` = configApi.getConfig; `getSiteConfig`: () => `MermaidConfig` = configApi.getSiteConfig; `globalReset`: () => `void` ; `initialize`: (`options`: `MermaidConfig`) => `Promise`<`void`> ; `parse`: (`text`: `string`, `parseError?`: `ParseErrorFunction`) => `boolean` ; `parseDirective`: (`p`: `any`, `statement`: `string`, `context`: `string`, `type`: `string`) => `void` ; `render`: (`id`: `string`, `text`: `string`, `cb`: (`svgCode`: `string`, `bindFunctions?`: (`element`: `Element`) => `void`) => `void`, `container?`: `Element`) => `Promise`<`void`> ; `reset`: () => `void` ; `setConfig`: (`conf`: `MermaidConfig`) => `MermaidConfig` = configApi.setConfig; `updateSiteConfig`: (`conf`: `MermaidConfig`) => `MermaidConfig` = configApi.updateSiteConfig }>
• `Const` **mermaidAPI**: `Readonly`<{ `defaultConfig`: `MermaidConfig` = configApi.defaultConfig; `getConfig`: () => `MermaidConfig` = configApi.getConfig; `getSiteConfig`: () => `MermaidConfig` = configApi.getSiteConfig; `globalReset`: () => `void` ; `initialize`: (`options`: `MermaidConfig`) => `Promise`<`void`> ; `parse`: (`text`: `string`, `parseError?`: `ParseErrorFunction`) => `boolean` ; `parseDirective`: (`p`: `any`, `statement`: `string`, `context`: `string`, `type`: `string`) => `void` ; `render`: (`id`: `string`, `text`: `string`, `cb`: (`svgCode`: `string`, `bindFunctions?`: (`element`: `Element`) => `void`) => `void`, `svgContainingElement?`: `Element`) => `Promise`<`void`> ; `reset`: () => `void` ; `setConfig`: (`conf`: `MermaidConfig`) => `MermaidConfig` = configApi.setConfig; `updateSiteConfig`: (`conf`: `MermaidConfig`) => `MermaidConfig` = configApi.updateSiteConfig }>

## mermaidAPI configuration defaults

Expand Down Expand Up @@ -80,27 +80,160 @@ mermaid.initialize(config);

#### Defined in

[mermaidAPI.ts:546](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L546)
[mermaidAPI.ts:736](https://github.com/weedySeaDragon/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L736)

## Functions

### appendDivSvgG

▸ **appendDivSvgG**(`parentRoot`, `id`, `enclosingDivId`, `divStyle?`, `svgXlink?`): `any`

Append an enclosing div, then svg, then g (group) to the d3 parentRoot. Set attributes.
Only set the style attribute on the enclosing div if divStyle is given.
Only set the xmlns:xlink attribute on svg if svgXlink is given.
Return the last node appended

#### Parameters

| Name | Type | Description |
| :--------------- | :------- | :----------------------------------------------- |
| `parentRoot` | `any` | the d3 node to append things to |
| `id` | `string` | the value to set the id attr to |
| `enclosingDivId` | `string` | the id to set the enclosing div to |
| `divStyle?` | `string` | if given, the style to set the enclosing div to |
| `svgXlink?` | `string` | if given, the link to set the new svg element to |

#### Returns

`any`

- returns the parentRoot that had nodes appended

#### Defined in

[mermaidAPI.ts:283](https://github.com/weedySeaDragon/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L283)

---

### cleanUpSvgCode

▸ **cleanUpSvgCode**(`svgCode?`, `inSandboxMode`, `useArrowMarkerUrls`): `string`

Clean up svgCode. Do replacements needed

#### Parameters

| Name | Type | Default value | Description |
| :------------------- | :-------- | :------------ | :---------------------------------------------------------- |
| `svgCode` | `string` | `''` | the code to clean up |
| `inSandboxMode` | `boolean` | `undefined` | security level |
| `useArrowMarkerUrls` | `boolean` | `undefined` | should arrow marker's use full urls? (vs. just the anchors) |

#### Returns

`string`

the cleaned up svgCode

#### Defined in

[mermaidAPI.ts:234](https://github.com/weedySeaDragon/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L234)

---

### createCssStyles

▸ **createCssStyles**(`config`, `graphType`, `classDefs?`): `string`

Create the user styles

#### Parameters

| Name | Type | Description |
| :---------- | :-------------- | :----------------------------------------------------- | ------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------- |
| `config` | `MermaidConfig` | configuration that has style and theme settings to use |
| `graphType` | `string` | used for checking if classDefs should be applied |
| `classDefs` | `undefined` | `null` | `Record`<`string`, `DiagramStyleClassDef`> | the classDefs in the diagram text. Might be null if none were defined. Usually is the result of a call to getClasses(...) |

#### Returns

`string`

the string with all the user styles

#### Defined in

[mermaidAPI.ts:161](https://github.com/weedySeaDragon/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L161)

---

### createUserStyles

▸ **createUserStyles**(`config`, `graphType`, `classDefs`, `svgId`): `string`

#### Parameters

| Name | Type |
| :---------- | :----------------------------------------- |
| `config` | `MermaidConfig` |
| `graphType` | `string` |
| `classDefs` | `Record`<`string`, `DiagramStyleClassDef`> |
| `svgId` | `string` |

#### Returns

`string`

#### Defined in

[mermaidAPI.ts:211](https://github.com/weedySeaDragon/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L211)

---

### cssImportantStyles

▸ **cssImportantStyles**(`cssClass`, `element`, `cssClasses?`): `string`

Create a CSS style that starts with the given class name, then the element,
with an enclosing block that has each of the cssClasses followed by !important;

#### Parameters

| Name | Type | Default value | Description |
| :----------- | :---------- | :------------ | :--------------------------------------------- |
| `cssClass` | `string` | `undefined` | CSS class name |
| `element` | `string` | `undefined` | CSS element |
| `cssClasses` | `string`\[] | `[]` | list of CSS styles to append after the element |

#### Returns

`string`

- the constructed string

#### Defined in

[mermaidAPI.ts:145](https://github.com/weedySeaDragon/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L145)

---

### decodeEntities

▸ **decodeEntities**(`text`): `string`

#### Parameters

| Name | Type |
| :----- | :------- |
| `text` | `string` |
| Name | Type | Description |
| :----- | :------- | :----------------- |
| `text` | `string` | text to be decoded |

#### Returns

`string`

#### Defined in

[mermaidAPI.ts:72](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L72)
[mermaidAPI.ts:119](https://github.com/weedySeaDragon/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L119)

---

Expand All @@ -110,14 +243,66 @@ mermaid.initialize(config);

#### Parameters

| Name | Type |
| :----- | :------- |
| `text` | `string` |
| Name | Type | Description |
| :----- | :------- | :----------------- |
| `text` | `string` | text to be encoded |

#### Returns

`string`

#### Defined in

[mermaidAPI.ts:46](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L46)
[mermaidAPI.ts:90](https://github.com/weedySeaDragon/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L90)

---

### putIntoIFrame

▸ **putIntoIFrame**(`svgCode?`, `svgElement?`): `string`

Put the svgCode into an iFrame. Return the iFrame code

#### Parameters

| Name | Type | Default value | Description |
| :------------ | :------- | :------------ | :--------------------------------------------------------------------------- |
| `svgCode` | `string` | `''` | the svg code to put inside the iFrame |
| `svgElement?` | `any` | `undefined` | the d3 node that has the current svgElement so we can get the height from it |

#### Returns

`string`

- the code with the iFrame that now contains the svgCode
TODO replace btoa(). Replace with buf.toString('base64')?

#### Defined in

[mermaidAPI.ts:262](https://github.com/weedySeaDragon/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L262)

---

### removeExistingElements

▸ **removeExistingElements**(`doc`, `isSandboxed`, `id`, `divSelector`, `iFrameSelector`): `void`

Remove any existing elements from the given document

#### Parameters

| Name | Type | Description |
| :--------------- | :--------- | :---------------------------------------------- |
| `doc` | `Document` | the document to removed elements from |
| `isSandboxed` | `boolean` | whether or not we are in sandboxed mode |
| `id` | `string` | id for any existing SVG element |
| `divSelector` | `string` | selector for any existing enclosing div element |
| `iFrameSelector` | `string` | selector for any existing iFrame element |

#### Returns

`void`

#### Defined in

[mermaidAPI.ts:334](https://github.com/weedySeaDragon/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L334)
2 changes: 1 addition & 1 deletion packages/mermaid/src/diagrams/flowchart/flowDb.js
Expand Up @@ -17,7 +17,7 @@ let vertexCounter = 0;
let config = configApi.getConfig();
let vertices = {};
let edges = [];
let classes = [];
let classes = {};
let subGraphs = [];
let subGraphLookup = {};
let tooltips = {};
Expand Down
3 changes: 2 additions & 1 deletion packages/mermaid/src/diagrams/flowchart/flowRenderer.js
Expand Up @@ -279,7 +279,8 @@ export const getClasses = function (text, diagObj) {
diagObj.parse(text);
return diagObj.db.getClasses();
} catch (e) {
return;
log.error(e);
return {};
}
};

Expand Down