Add interface for DiagramDb and other minor changes

This commit is contained in:
Mason Malone
2022-11-19 12:48:40 -08:00
parent a11ab3d5ea
commit 3316aa5f4f
2 changed files with 15 additions and 6 deletions

View File

@@ -1,3 +1,4 @@
import { DiagramDb } from './types';
// The "* as yaml" part is necessary for tree-shaking
import * as yaml from 'js-yaml';
@@ -15,11 +16,11 @@ type FrontMatterMetadata = {
/**
* Extract and parse frontmatter from text, if present, and sets appropriate
* properties in the provided db.
* @param text -
* @param db -
* @param text - The text that may have a YAML frontmatter.
* @param db - Diagram database, could be of any diagram.
* @returns text with frontmatter stripped out
*/
export function extractFrontMatter(text: string, db: any): string {
export function extractFrontMatter(text: string, db: DiagramDb): string {
const matches = text.match(frontMatterRegex);
if (matches) {
const parsed: FrontMatterMetadata = yaml.load(matches[1], {
@@ -28,8 +29,8 @@ export function extractFrontMatter(text: string, db: any): string {
schema: yaml.FAILSAFE_SCHEMA,
}) as FrontMatterMetadata;
if (parsed && parsed.title) {
db?.setDiagramTitle(parsed.title);
if (parsed?.title) {
db.setDiagramTitle?.(parsed.title);
}
return text.slice(matches[0].length);

View File

@@ -8,8 +8,16 @@ export interface InjectUtils {
_setupGraphViewbox: any;
}
/**
* Generic Diagram DB that may apply to any diagram type.
*/
export interface DiagramDb {
clear?: () => void;
setDiagramTitle?: (title: string) => void;
}
export interface DiagramDefinition {
db: any;
db: DiagramDb;
renderer: any;
parser: any;
styles: any;