Disallow leading whitespace before delimiter

This commit is contained in:
Mason Malone 2022-11-19 12:48:17 -08:00
parent 70f024735b
commit a11ab3d5ea
13 changed files with 58 additions and 52 deletions

View File

@ -41,6 +41,13 @@ describe('extractFrontmatter', () => {
expect(db.setDiagramTitle).toHaveBeenCalledWith('foo---bar'); expect(db.setDiagramTitle).toHaveBeenCalledWith('foo---bar');
}); });
it('handles frontmatter with multi-line string and multiple delimiters', () => {
const db = dbMock();
const text = `---\ntitle: |\n multi-line string\n ---\n---\ndiagram`;
expect(extractFrontMatter(text, db)).toEqual('diagram');
expect(db.setDiagramTitle).toHaveBeenCalledWith('multi-line string\n---\n');
});
it('handles frontmatter with title', () => { it('handles frontmatter with title', () => {
const db = dbMock(); const db = dbMock();
const text = `---\ntitle: foo\n---\ndiagram`; const text = `---\ntitle: foo\n---\ndiagram`;

View File

@ -6,7 +6,7 @@ import * as yaml from 'js-yaml';
// Note that JS doesn't support the "\A" anchor, which means we can't use // Note that JS doesn't support the "\A" anchor, which means we can't use
// multiline mode. // multiline mode.
// Relevant YAML spec: https://yaml.org/spec/1.2.2/#914-explicit-documents // Relevant YAML spec: https://yaml.org/spec/1.2.2/#914-explicit-documents
export const frontMatterRegex = /^(?:\s*---\s*[\r\n])(.*?)(?:[\r\n]\s*---\s*[\r\n]+)/s; export const frontMatterRegex = /^(?:---\s*[\r\n])(.*?)(?:[\r\n]---\s*[\r\n]+)/s;
type FrontMatterMetadata = { type FrontMatterMetadata = {
title?: string; title?: string;

View File

@ -238,10 +238,9 @@ Alice->Bob: hi`;
const type = detectType(str); const type = detectType(str);
expect(type).toBe('gitGraph'); expect(type).toBe('gitGraph');
}); });
it('should handle frontmatter with leading spaces', function () { it('should not allow frontmatter with leading spaces', function () {
const str = ' ---\ntitle: foo\n---\n gitGraph TB:\nbfs1:queue'; const str = ' ---\ntitle: foo\n---\n gitGraph TB:\nbfs1:queue';
const type = detectType(str); expect(() => detectType(str)).toThrow('No diagram type detected for text');
expect(type).toBe('gitGraph');
}); });
}); });
describe('when finding substring in array ', function () { describe('when finding substring in array ', function () {