• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mermaid-js / mermaid / 4608236643

pending completion
4608236643

push

github

Knut Sveidqvist
Merge branch 'release/10.1.0'

1643 of 1996 branches covered (82.31%)

Branch coverage included in aggregate %.

801 of 801 new or added lines in 37 files covered. (100.0%)

16190 of 33430 relevant lines covered (48.43%)

403.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.74
/packages/mermaid/src/diagram-api/frontmatter.ts
1
import { DiagramDb } from './types';
1✔
2
// The "* as yaml" part is necessary for tree-shaking
1✔
3
import * as yaml from 'js-yaml';
1✔
4

1✔
5
// Match Jekyll-style front matter blocks (https://jekyllrb.com/docs/front-matter/).
1✔
6
// Based on regex used by Jekyll: https://github.com/jekyll/jekyll/blob/6dd3cc21c40b98054851846425af06c64f9fb466/lib/jekyll/document.rb#L10
1✔
7
// Note that JS doesn't support the "\A" anchor, which means we can't use
1✔
8
// multiline mode.
1✔
9
// Relevant YAML spec: https://yaml.org/spec/1.2.2/#914-explicit-documents
1✔
10
export const frontMatterRegex = /^-{3}\s*[\n\r](.*?)[\n\r]-{3}\s*[\n\r]+/s;
1✔
11

1✔
12
type FrontMatterMetadata = {
1✔
13
  title?: string;
1✔
14
  // Allows custom display modes. Currently used for compact mode in gantt charts.
1✔
15
  displayMode?: string;
1✔
16
};
1✔
17

1✔
18
/**
1✔
19
 * Extract and parse frontmatter from text, if present, and sets appropriate
1✔
20
 * properties in the provided db.
1✔
21
 * @param text - The text that may have a YAML frontmatter.
1✔
22
 * @param db - Diagram database, could be of any diagram.
1✔
23
 * @returns text with frontmatter stripped out
1✔
24
 */
1✔
25
export function extractFrontMatter(text: string, db: DiagramDb): string {
1✔
26
  const matches = text.match(frontMatterRegex);
28,639✔
27
  if (matches) {
28,639✔
28
    const parsed: FrontMatterMetadata = yaml.load(matches[1], {
8✔
29
      // To keep things simple, only allow strings, arrays, and plain objects.
8✔
30
      // https://www.yaml.org/spec/1.2/spec.html#id2802346
8✔
31
      schema: yaml.FAILSAFE_SCHEMA,
8✔
32
    }) as FrontMatterMetadata;
8✔
33

8✔
34
    if (parsed?.title) {
8✔
35
      db.setDiagramTitle?.(parsed.title);
5✔
36
    }
5✔
37

7✔
38
    if (parsed?.displayMode) {
8!
39
      db.setDisplayMode?.(parsed.displayMode);
×
40
    }
✔
41

7✔
42
    return text.slice(matches[0].length);
7✔
43
  } else {
28,639✔
44
    return text;
28,631✔
45
  }
28,631✔
46
}
28,639✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc