Skip to content

Commit

Permalink
feat(frontmatter): implement getFrontmatterBlock method (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
billiegoose authored Mar 4, 2020
1 parent b0785b6 commit e4aee86
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
Empty file removed src/consts.ts
Empty file.
43 changes: 43 additions & 0 deletions src/frontmatter/__tests__/fronmatter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as fs from 'fs';
import { join } from 'path';
import * as Unist from 'unist';

import { parse } from '../../parse';
import { parseWithPointers } from '../../parseWithPointers';
import { stringify } from '../../stringify';
import { Frontmatter } from '../frontmatter';
Expand Down Expand Up @@ -38,6 +39,10 @@ describe('Frontmatter', () => {
expect(instance.get('test')).toBeUndefined();
});

it('getFrontmatterBlock should return undefined', () => {
expect(Frontmatter.getFrontmatterBlock(fixture)).toBeUndefined();
});

describe('set', () => {
it('should create frontmatter block', () => {
const instance = new Frontmatter(fixture);
Expand Down Expand Up @@ -67,6 +72,13 @@ foo: 123
expect(instance.getAll()).toEqual({});
});

it('getFrontmatterBlock should return block', () => {
expect(Frontmatter.getFrontmatterBlock(invalid)).toEqual(`---
tti
tags: [Hubs]
---`);
});

describe('set', () => {
it('should update frontmatter block correctly', () => {
const instance = new Frontmatter(invalid);
Expand Down Expand Up @@ -108,6 +120,13 @@ tags: [test]
});
});

it('getFrontmatterBlock should return block', () => {
expect(Frontmatter.getFrontmatterBlock(tags)).toEqual(`---
title: Graphite Introduction
tags: ['introductions', 'guides']
---`);
});

describe('#get', () => {
it('should return value for single item', () => {
const instance = new Frontmatter(tags);
Expand Down Expand Up @@ -285,4 +304,28 @@ Coolio.
expect(instance.stringify()).toEqual(tags);
});
});

describe('getFrontmatterBlock method', () => {
it.each(['---', '------', '---\nfoo', '--\n---', '---\na--', '\na\n---'])(
'should return undefined for incomplete block `%s`',
document => {
const block = Frontmatter.getFrontmatterBlock(document);
expect(block).toBeUndefined();
expect(parse(document)).toEqual(
expect.objectContaining({
type: 'root',
children: expect.arrayContaining([expect.not.objectContaining({ type: 'yaml' })]),
}),
);
},
);

it.each(['---\n---', '---\nfoo\n---'])('should produce the same result for `%s`', () => {
const document = `---\n---`;
const block = Frontmatter.getFrontmatterBlock(document)!;

expect(block).toEqual(document);
expect(stringify(parse(block))).toEqual(stringify(parse(document)));
});
});
});
7 changes: 7 additions & 0 deletions src/frontmatter/frontmatter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Optional } from '@stoplight/types';
import * as yaml from 'js-yaml';
import { get, pullAt, set, toPath, unset } from 'lodash';
import * as Unist from 'unist';
Expand Down Expand Up @@ -94,6 +95,12 @@ export class Frontmatter<T extends object = any> implements IFrontmatter<T> {
return stringify(this.document);
}

// based on https://github.com/remarkjs/remark-frontmatter/blob/3c18752b01af683d94641e47bd79581690a995b7/lib/parse.js
public static getFrontmatterBlock(value: string): Optional<string> {
const match = value.match(/^(\s*\n)?---.*?\n---/s);
return match === null ? void 0 : match[0];
}

private updateDocument() {
const children = this.document.children as Unist.Parent['children'] | undefined;
if (!children) return;
Expand Down

0 comments on commit e4aee86

Please sign in to comment.