Skip to content

Commit

Permalink
feat: Add getConfigValue
Browse files Browse the repository at this point in the history
  • Loading branch information
sidharthv96 committed Jan 30, 2024
1 parent 80c20a7 commit 8b0399e
Show file tree
Hide file tree
Showing 6 changed files with 326 additions and 196 deletions.
53 changes: 42 additions & 11 deletions docs/config/setup/modules/config.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/mermaid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
"remark-gfm": "^3.0.1",
"rimraf": "^5.0.0",
"start-server-and-test": "^2.0.0",
"type-fest": "^4.1.0",
"type-fest": "^4.10.1",
"typedoc": "^0.25.0",
"typedoc-plugin-markdown": "^3.15.2",
"typescript": "^5.0.4",
Expand Down
20 changes: 19 additions & 1 deletion packages/mermaid/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import theme from './themes/index.js';
import config from './defaultConfig.js';
import type { MermaidConfig } from './config.type.js';
import { sanitizeDirective } from './utils/sanitizeDirective.js';
import type { RequiredDeep } from 'type-fest';
import lodashGet from 'lodash-es/get.js';
import type { RequiredDeep, Get, Paths } from 'type-fest';

export const defaultConfig: RequiredDeep<MermaidConfig> = Object.freeze(config);

Expand Down Expand Up @@ -246,3 +247,20 @@ const checkConfig = (config: MermaidConfig) => {
issueWarning('LAZY_LOAD_DEPRECATED');
}
};

/**
* Get a value from the provided config, or the default config if it doesn't exist
* @param config - Mermaid Config
* @param path - Path of the value to get
* @returns Value from provided config if it exists, otherwise from default config
*/
export const getConfigValue = <Path extends Paths<RequiredDeep<MermaidConfig>>>(
config: MermaidConfig,
path: Path
): Get<RequiredDeep<MermaidConfig>, Path> => {
let value = lodashGet(config, path) as Get<RequiredDeep<MermaidConfig>, Path>;
if (!value) {
value = lodashGet(defaultConfig, path) as Get<RequiredDeep<MermaidConfig>, Path>;
}
return value;
};

Check warning on line 266 in packages/mermaid/src/config.ts

View check run for this annotation

Codecov / codecov/patch

packages/mermaid/src/config.ts#L258-L266

Added lines #L258 - L266 were not covered by tests
6 changes: 3 additions & 3 deletions packages/mermaid/src/diagrams/mindmap/mindmapDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { D3Element } from '../../mermaidAPI.js';
import { sanitizeText } from '../../diagrams/common/common.js';
import { log } from '../../logger.js';
import type { MindmapNode } from './mindmapTypes.js';
import { defaultConfig } from '../../config.js';
import { getConfigValue } from '../../config.js';

let nodes: MindmapNode[] = [];
let cnt = 0;
Expand Down Expand Up @@ -32,7 +32,7 @@ const getMindmap = () => {
const addNode = (level: number, id: string, descr: string, type: number) => {
log.info('addNode', level, id, descr, type);
const conf = getConfig();
let padding: number = conf.mindmap?.padding ?? defaultConfig.mindmap.padding;
let padding: number = getConfigValue(conf, 'mindmap.padding');
switch (type) {
case nodeType.ROUNDED_RECT:
case nodeType.RECT:
Expand All @@ -47,7 +47,7 @@ const addNode = (level: number, id: string, descr: string, type: number) => {
descr: sanitizeText(descr, conf),
type,
children: [],
width: conf.mindmap?.maxNodeWidth ?? defaultConfig.mindmap.maxNodeWidth,
width: getConfigValue(conf, 'mindmap.maxNodeWidth'),
padding,
} satisfies MindmapNode;

Expand Down
7 changes: 3 additions & 4 deletions packages/mermaid/src/diagrams/mindmap/mindmapRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import cytoscape from 'cytoscape';
import coseBilkent from 'cytoscape-cose-bilkent';
import { select } from 'd3';
import type { MermaidConfig } from '../../config.type.js';
import { getConfig } from '../../diagram-api/diagramAPI.js';
import type { DrawDefinition } from '../../diagram-api/types.js';
import { log } from '../../logger.js';
import type { D3Element } from '../../mermaidAPI.js';
import { selectSvgElement } from '../../rendering-util/selectSvgElement.js';
import { setupGraphViewbox } from '../../setupGraphViewbox.js';
import type { FilledMindMapNode, MindmapDB, MindmapNode } from './mindmapTypes.js';
import { drawNode, positionNode } from './svgDraw.js';
import { defaultConfig } from '../../config.js';
import { getConfig, getConfigValue } from '../../config.js';

// Inject the layout algorithm into cytoscape
cytoscape.use(coseBilkent);
Expand Down Expand Up @@ -191,8 +190,8 @@ export const draw: DrawDefinition = async (text, id, _version, diagObj) => {
setupGraphViewbox(
undefined,
svg,
conf.mindmap?.padding ?? defaultConfig.mindmap.padding,
conf.mindmap?.useMaxWidth ?? defaultConfig.mindmap.useMaxWidth
getConfigValue(conf, 'mindmap.padding'),
getConfigValue(conf, 'mindmap.useMaxWidth')
);
};

Expand Down
Loading

0 comments on commit 8b0399e

Please sign in to comment.