mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-02 23:26:44 +02:00
Updated code as per suggestions on-behalf-of: @MermaidChart <hello@mermaidchart.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import { log } from '../../logger.js';
|
|||||||
import { populateCommonDb } from '../common/populateCommonDb.js';
|
import { populateCommonDb } from '../common/populateCommonDb.js';
|
||||||
import type { TreemapNode, TreemapAst, TreemapDB } from './types.js';
|
import type { TreemapNode, TreemapAst, TreemapDB } from './types.js';
|
||||||
import { buildHierarchy } from './utils.js';
|
import { buildHierarchy } from './utils.js';
|
||||||
|
import { TreeMapDB } from './db.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Populates the database with data from the Treemap AST
|
* Populates the database with data from the Treemap AST
|
||||||
@@ -83,7 +84,7 @@ const getItemName = (item: { name?: string | number }): string => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const parser: ParserDefinition = {
|
export const parser: ParserDefinition = {
|
||||||
parser: { yy: {} as TreemapDB },
|
parser: { yy: new TreeMapDB() },
|
||||||
parse: async (text: string): Promise<void> => {
|
parse: async (text: string): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
// Use a generic parse that accepts any diagram type
|
// 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 parseFunc = parse as (diagramType: string, text: string) => Promise<TreemapAst>;
|
||||||
const ast = await parseFunc('treemap', text);
|
const ast = await parseFunc('treemap', text);
|
||||||
log.debug('Treemap AST:', ast);
|
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) {
|
} catch (error) {
|
||||||
log.error('Error parsing treemap:', error);
|
log.error('Error parsing treemap:', error);
|
||||||
throw error;
|
throw error;
|
||||||
|
Reference in New Issue
Block a user