feat: Add title to parseResult

This commit is contained in:
Sidharth Vinod
2024-09-13 19:49:24 +05:30
parent 71889fd135
commit 19e79fda73
2 changed files with 17 additions and 6 deletions

View File

@@ -68,17 +68,23 @@ function processAndSetConfigs(text: string) {
async function parse(
text: string,
parseOptions: ParseOptions & { suppressErrors: true }
): Promise<ParseResult | false>;
): Promise<ParseResult & { error?: unknown }>;
async function parse(text: string, parseOptions?: ParseOptions): Promise<ParseResult>;
async function parse(text: string, parseOptions?: ParseOptions): Promise<ParseResult | false> {
async function parse(
text: string,
parseOptions?: ParseOptions
): Promise<ParseResult & { error?: unknown }> {
addDiagrams();
let code = '';
let title = undefined;
let config: MermaidConfig = {};
try {
const { code, config } = processAndSetConfigs(text);
({ code, config, title } = processAndSetConfigs(text));
const diagram = await getDiagramFromText(code);
return { diagram, config };
return { diagram, code, config, title, success: true };
} catch (error) {
if (parseOptions?.suppressErrors) {
return false;
return { code, config, title, success: false, error };
}
throw error;
}

View File

@@ -58,16 +58,21 @@ export interface ParseOptions {
}
export interface ParseResult {
success: boolean;
code: string;
/**
* The config passed as YAML frontmatter or directives
*/
config: MermaidConfig;
title?: string;
/**
* The diagram AST
*
*/
diagram: Diagram;
diagram?: Diagram;
}
// This makes it clear that we're working with a d3 selected element of some kind, even though it's hard to specify the exact type.