Updated code as per suggestions on-behalf-of: @MermaidChart <hello@mermaidchart.com>

This commit is contained in:
shubham-mermaid
2025-07-11 15:16:49 +05:30
parent 9dfbf1166d
commit c12aea588c

View File

@@ -4,6 +4,7 @@ import { log } from '../../logger.js';
import { populateCommonDb } from '../common/populateCommonDb.js';
import type { TreemapNode, TreemapAst, TreemapDB } from './types.js';
import { buildHierarchy } from './utils.js';
import { TreeMapDB } from './db.js';
/**
* Populates the database with data from the Treemap AST
@@ -83,7 +84,7 @@ const getItemName = (item: { name?: string | number }): string => {
};
export const parser: ParserDefinition = {
parser: { yy: {} as TreemapDB },
parser: { yy: new TreeMapDB() },
parse: async (text: string): Promise<void> => {
try {
// Use a generic parse that accepts any diagram type
@@ -91,7 +92,13 @@ export const parser: ParserDefinition = {
const parseFunc = parse as (diagramType: string, text: string) => Promise<TreemapAst>;
const ast = await parseFunc('treemap', text);
log.debug('Treemap AST:', ast);
populate(ast, parser.parser?.yy as TreemapDB);
const db = parser.parser?.yy;
if (!(db instanceof TreeMapDB)) {
throw new Error(
'parser.parser?.yy was not a TreemapDB. This is due to a bug within Mermaid, please report this issue at https://github.com/mermaid-js/mermaid/issues.'
);
}
populate(ast, db);
} catch (error) {
log.error('Error parsing treemap:', error);
throw error;