mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-10-13 19:19:37 +02:00
feat!: integrate info
parser into mermaid
package
BREAKING CHANGE: remove `showInfo` from `infoDb`.
This commit is contained in:
@@ -71,6 +71,7 @@
|
|||||||
"khroma": "^2.0.0",
|
"khroma": "^2.0.0",
|
||||||
"lodash-es": "^4.17.21",
|
"lodash-es": "^4.17.21",
|
||||||
"mdast-util-from-markdown": "^1.3.0",
|
"mdast-util-from-markdown": "^1.3.0",
|
||||||
|
"mermaid-parser": "workspace:*",
|
||||||
"stylis": "^4.1.3",
|
"stylis": "^4.1.3",
|
||||||
"ts-dedent": "^2.2.0",
|
"ts-dedent": "^2.2.0",
|
||||||
"uuid": "^9.0.0"
|
"uuid": "^9.0.0"
|
||||||
|
@@ -16,11 +16,16 @@ export interface InjectUtils {
|
|||||||
* Generic Diagram DB that may apply to any diagram type.
|
* Generic Diagram DB that may apply to any diagram type.
|
||||||
*/
|
*/
|
||||||
export interface DiagramDB {
|
export interface DiagramDB {
|
||||||
|
// db
|
||||||
clear?: () => void;
|
clear?: () => void;
|
||||||
setDiagramTitle?: (title: string) => void;
|
setDiagramTitle?: (title: string) => void;
|
||||||
setDisplayMode?: (title: string) => void;
|
getDiagramTitle?: () => string;
|
||||||
|
setAccTitle?: (title: string) => void;
|
||||||
getAccTitle?: () => string;
|
getAccTitle?: () => string;
|
||||||
|
setAccDescription?: (describetion: string) => void;
|
||||||
getAccDescription?: () => string;
|
getAccDescription?: () => string;
|
||||||
|
|
||||||
|
setDisplayMode?: (title: string) => void;
|
||||||
bindFunctions?: (element: Element) => void;
|
bindFunctions?: (element: Element) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
9
packages/mermaid/src/diagrams/common/populateCommonDb.ts
Normal file
9
packages/mermaid/src/diagrams/common/populateCommonDb.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import type { DiagramAST } from 'mermaid-parser';
|
||||||
|
|
||||||
|
import type { DiagramDB } from '../../diagram-api/types.js';
|
||||||
|
|
||||||
|
export function populateCommonDb(ast: DiagramAST, db: DiagramDB) {
|
||||||
|
ast.accDescr && db.setAccDescription?.(ast.accDescr);
|
||||||
|
ast.accTitle && db.setAccTitle?.(ast.accTitle);
|
||||||
|
ast.title && db.setDiagramTitle?.(ast.title);
|
||||||
|
}
|
@@ -1,24 +1,31 @@
|
|||||||
// @ts-ignore - jison doesn't export types
|
import { parser } from './infoParser.js';
|
||||||
import { parser } from './parser/info.jison';
|
|
||||||
import { db } from './infoDb.js';
|
|
||||||
|
|
||||||
describe('info diagram', () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
parser.yy = db;
|
|
||||||
parser.yy.clear();
|
|
||||||
});
|
|
||||||
|
|
||||||
|
describe('info', () => {
|
||||||
it('should handle an info definition', () => {
|
it('should handle an info definition', () => {
|
||||||
const str = `info`;
|
const str = `info`;
|
||||||
parser.parse(str);
|
expect(() => {
|
||||||
|
parser.parse(str);
|
||||||
expect(db.getInfo()).toBeFalsy();
|
}).not.toThrow();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle an info definition with showInfo', () => {
|
it('should handle an info definition with showInfo', () => {
|
||||||
const str = `info showInfo`;
|
const str = `info showInfo`;
|
||||||
parser.parse(str);
|
expect(() => {
|
||||||
|
parser.parse(str);
|
||||||
|
}).not.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
expect(db.getInfo()).toBeTruthy();
|
it('should throw because of unsupported info grammar', () => {
|
||||||
|
const str = `info unsupported`;
|
||||||
|
expect(() => {
|
||||||
|
parser.parse(str);
|
||||||
|
}).toThrow('Parsing failed: unexpected character: ->u<- at offset: 5, skipped 11 characters.');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw because of unsupported info grammar', () => {
|
||||||
|
const str = `info unsupported`;
|
||||||
|
expect(() => {
|
||||||
|
parser.parse(str);
|
||||||
|
}).toThrow('Parsing failed: unexpected character: ->u<- at offset: 5, skipped 11 characters.');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@@ -1,23 +1,10 @@
|
|||||||
import type { InfoFields, InfoDB } from './infoTypes.js';
|
import type { InfoFields, InfoDB } from './infoTypes.js';
|
||||||
|
import { version } from '../../../package.json';
|
||||||
|
|
||||||
export const DEFAULT_INFO_DB: InfoFields = {
|
export const DEFAULT_INFO_DB: InfoFields = { version } as const;
|
||||||
info: false,
|
|
||||||
} as const;
|
|
||||||
|
|
||||||
let info: boolean = DEFAULT_INFO_DB.info;
|
export const getVersion = (): string => DEFAULT_INFO_DB.version;
|
||||||
|
|
||||||
export const setInfo = (toggle: boolean): void => {
|
|
||||||
info = toggle;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getInfo = (): boolean => info;
|
|
||||||
|
|
||||||
const clear = (): void => {
|
|
||||||
info = DEFAULT_INFO_DB.info;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const db: InfoDB = {
|
export const db: InfoDB = {
|
||||||
clear,
|
getVersion,
|
||||||
setInfo,
|
|
||||||
getInfo,
|
|
||||||
};
|
};
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
import type { DiagramDefinition } from '../../diagram-api/types.js';
|
import type { DiagramDefinition } from '../../diagram-api/types.js';
|
||||||
// @ts-ignore - jison doesn't export types
|
import { parser } from './infoParser.js';
|
||||||
import parser from './parser/info.jison';
|
|
||||||
import { db } from './infoDb.js';
|
import { db } from './infoDb.js';
|
||||||
import { renderer } from './infoRenderer.js';
|
import { renderer } from './infoRenderer.js';
|
||||||
|
|
||||||
|
12
packages/mermaid/src/diagrams/info/infoParser.ts
Normal file
12
packages/mermaid/src/diagrams/info/infoParser.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import type { Info } from 'mermaid-parser';
|
||||||
|
import { parse } from 'mermaid-parser';
|
||||||
|
|
||||||
|
import { log } from '../../logger.js';
|
||||||
|
import type { ParserDefinition } from '../../diagram-api/types.js';
|
||||||
|
|
||||||
|
export const parser: ParserDefinition = {
|
||||||
|
parse: (input: string): void => {
|
||||||
|
const ast: Info = parse('info', input);
|
||||||
|
log.debug(ast);
|
||||||
|
},
|
||||||
|
};
|
@@ -1,11 +1,9 @@
|
|||||||
import type { DiagramDB } from '../../diagram-api/types.js';
|
import type { DiagramDB } from '../../diagram-api/types.js';
|
||||||
|
|
||||||
export interface InfoFields {
|
export interface InfoFields {
|
||||||
info: boolean;
|
version: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface InfoDB extends DiagramDB {
|
export interface InfoDB extends DiagramDB {
|
||||||
clear: () => void;
|
getVersion: () => string;
|
||||||
setInfo: (info: boolean) => void;
|
|
||||||
getInfo: () => boolean;
|
|
||||||
}
|
}
|
||||||
|
@@ -1,48 +0,0 @@
|
|||||||
/** mermaid
|
|
||||||
* https://knsv.github.io/mermaid
|
|
||||||
* (c) 2015 Knut Sveidqvist
|
|
||||||
* MIT license.
|
|
||||||
*/
|
|
||||||
%lex
|
|
||||||
|
|
||||||
%options case-insensitive
|
|
||||||
|
|
||||||
%{
|
|
||||||
// Pre-lexer code can go here
|
|
||||||
%}
|
|
||||||
|
|
||||||
%%
|
|
||||||
|
|
||||||
"info" return 'info' ;
|
|
||||||
[\s\n\r]+ return 'NL' ;
|
|
||||||
[\s]+ return 'space';
|
|
||||||
"showInfo" return 'showInfo';
|
|
||||||
<<EOF>> return 'EOF' ;
|
|
||||||
. return 'TXT' ;
|
|
||||||
|
|
||||||
/lex
|
|
||||||
|
|
||||||
%start start
|
|
||||||
|
|
||||||
%% /* language grammar */
|
|
||||||
|
|
||||||
start
|
|
||||||
// %{ : info document 'EOF' { return yy; } }
|
|
||||||
: info document 'EOF' { return yy; }
|
|
||||||
;
|
|
||||||
|
|
||||||
document
|
|
||||||
: /* empty */
|
|
||||||
| document line
|
|
||||||
;
|
|
||||||
|
|
||||||
line
|
|
||||||
: statement { }
|
|
||||||
| 'NL'
|
|
||||||
;
|
|
||||||
|
|
||||||
statement
|
|
||||||
: showInfo { yy.setInfo(true); }
|
|
||||||
;
|
|
||||||
|
|
||||||
%%
|
|
3
pnpm-lock.yaml
generated
3
pnpm-lock.yaml
generated
@@ -233,6 +233,9 @@ importers:
|
|||||||
mdast-util-from-markdown:
|
mdast-util-from-markdown:
|
||||||
specifier: ^1.3.0
|
specifier: ^1.3.0
|
||||||
version: 1.3.0
|
version: 1.3.0
|
||||||
|
mermaid-parser:
|
||||||
|
specifier: workspace:*
|
||||||
|
version: link:../parser
|
||||||
stylis:
|
stylis:
|
||||||
specifier: ^4.1.3
|
specifier: ^4.1.3
|
||||||
version: 4.1.3
|
version: 4.1.3
|
||||||
|
Reference in New Issue
Block a user