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

feat: Add getConfigValue #5251

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 @@
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work for values that valid but falsy, false, 0, or "".

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'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also not a big fan of this syntax, since it makes things more complicated.

Ideally, we'd instead change getConfig() to return RequiredDeep<MermaidConfig>.

The only reason I haven't yet is that some of the config values don't have default values, because undefined is a valid value for them. Ideally, I'd go through the config and change every value that currently accepts undefined to instead use null, that way we can safely do RequiredDeep<MermaidConfig> and still keep the null types.

Then we could just do: 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
Loading