2024-12-05 09:50:29 -05:00
|
|
|
import assert from 'node:assert/strict';
|
|
|
|
import { describe, it } from 'node:test';
|
|
|
|
import { extractFrontmatter, parseFrontmatter } from '../dist/index.js';
|
|
|
|
|
2024-12-06 05:08:39 -05:00
|
|
|
const bom = '\uFEFF';
|
|
|
|
|
2024-12-05 09:50:29 -05:00
|
|
|
describe('extractFrontmatter', () => {
|
|
|
|
it('works', () => {
|
|
|
|
const yaml = `\nfoo: bar\n`;
|
|
|
|
assert.equal(extractFrontmatter(`---${yaml}---`), yaml);
|
2024-12-06 05:08:39 -05:00
|
|
|
assert.equal(extractFrontmatter(`${bom}---${yaml}---`), yaml);
|
2024-12-05 09:50:29 -05:00
|
|
|
assert.equal(extractFrontmatter(`\n---${yaml}---`), yaml);
|
|
|
|
assert.equal(extractFrontmatter(`\n \n---${yaml}---`), yaml);
|
|
|
|
assert.equal(extractFrontmatter(`---${yaml}---\ncontent`), yaml);
|
2024-12-06 05:08:39 -05:00
|
|
|
assert.equal(extractFrontmatter(`${bom}---${yaml}---\ncontent`), yaml);
|
2024-12-05 09:50:29 -05:00
|
|
|
assert.equal(extractFrontmatter(`\n\n---${yaml}---\n\ncontent`), yaml);
|
|
|
|
assert.equal(extractFrontmatter(`\n \n---${yaml}---\n\ncontent`), yaml);
|
2024-12-06 05:08:39 -05:00
|
|
|
assert.equal(extractFrontmatter(` ---${yaml}---`), undefined);
|
|
|
|
assert.equal(extractFrontmatter(`---${yaml} ---`), undefined);
|
2024-12-05 09:50:29 -05:00
|
|
|
assert.equal(extractFrontmatter(`text\n---${yaml}---\n\ncontent`), undefined);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('parseFrontmatter', () => {
|
|
|
|
it('works', () => {
|
|
|
|
const yaml = `\nfoo: bar\n`;
|
|
|
|
assert.deepEqual(parseFrontmatter(`---${yaml}---`), {
|
|
|
|
frontmatter: { foo: 'bar' },
|
|
|
|
rawFrontmatter: yaml,
|
|
|
|
content: '',
|
|
|
|
});
|
2024-12-06 05:08:39 -05:00
|
|
|
assert.deepEqual(parseFrontmatter(`${bom}---${yaml}---`), {
|
2024-12-05 09:50:29 -05:00
|
|
|
frontmatter: { foo: 'bar' },
|
|
|
|
rawFrontmatter: yaml,
|
2024-12-06 05:08:39 -05:00
|
|
|
content: bom,
|
2024-12-05 09:50:29 -05:00
|
|
|
});
|
|
|
|
assert.deepEqual(parseFrontmatter(`\n---${yaml}---`), {
|
|
|
|
frontmatter: { foo: 'bar' },
|
|
|
|
rawFrontmatter: yaml,
|
|
|
|
content: '\n',
|
|
|
|
});
|
|
|
|
assert.deepEqual(parseFrontmatter(`\n \n---${yaml}---`), {
|
|
|
|
frontmatter: { foo: 'bar' },
|
|
|
|
rawFrontmatter: yaml,
|
|
|
|
content: '\n \n',
|
|
|
|
});
|
|
|
|
assert.deepEqual(parseFrontmatter(`---${yaml}---\ncontent`), {
|
|
|
|
frontmatter: { foo: 'bar' },
|
|
|
|
rawFrontmatter: yaml,
|
|
|
|
content: '\ncontent',
|
|
|
|
});
|
2024-12-06 05:08:39 -05:00
|
|
|
assert.deepEqual(parseFrontmatter(`${bom}---${yaml}---\ncontent`), {
|
|
|
|
frontmatter: { foo: 'bar' },
|
|
|
|
rawFrontmatter: yaml,
|
|
|
|
content: `${bom}\ncontent`,
|
|
|
|
});
|
2024-12-05 09:50:29 -05:00
|
|
|
assert.deepEqual(parseFrontmatter(`\n\n---${yaml}---\n\ncontent`), {
|
|
|
|
frontmatter: { foo: 'bar' },
|
|
|
|
rawFrontmatter: yaml,
|
|
|
|
content: '\n\n\n\ncontent',
|
|
|
|
});
|
|
|
|
assert.deepEqual(parseFrontmatter(`\n \n---${yaml}---\n\ncontent`), {
|
|
|
|
frontmatter: { foo: 'bar' },
|
|
|
|
rawFrontmatter: yaml,
|
|
|
|
content: '\n \n\n\ncontent',
|
|
|
|
});
|
2024-12-06 05:08:39 -05:00
|
|
|
assert.deepEqual(parseFrontmatter(` ---${yaml}---`), {
|
|
|
|
frontmatter: {},
|
|
|
|
rawFrontmatter: '',
|
|
|
|
content: ` ---${yaml}---`,
|
|
|
|
});
|
|
|
|
assert.deepEqual(parseFrontmatter(`---${yaml} ---`), {
|
|
|
|
frontmatter: {},
|
|
|
|
rawFrontmatter: '',
|
|
|
|
content: `---${yaml} ---`,
|
|
|
|
});
|
2024-12-05 09:50:29 -05:00
|
|
|
assert.deepEqual(parseFrontmatter(`text\n---${yaml}---\n\ncontent`), {
|
|
|
|
frontmatter: {},
|
|
|
|
rawFrontmatter: '',
|
|
|
|
content: `text\n---${yaml}---\n\ncontent`,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('frontmatter style', () => {
|
|
|
|
const yaml = `\nfoo: bar\n`;
|
|
|
|
const parse1 = (style) => parseFrontmatter(`---${yaml}---`, { frontmatter: style }).content;
|
|
|
|
assert.deepEqual(parse1('preserve'), `---${yaml}---`);
|
|
|
|
assert.deepEqual(parse1('remove'), '');
|
|
|
|
assert.deepEqual(parse1('empty-with-spaces'), ` \n \n `);
|
|
|
|
assert.deepEqual(parse1('empty-with-lines'), `\n\n`);
|
|
|
|
|
|
|
|
const parse2 = (style) =>
|
|
|
|
parseFrontmatter(`\n \n---${yaml}---\n\ncontent`, { frontmatter: style }).content;
|
|
|
|
assert.deepEqual(parse2('preserve'), `\n \n---${yaml}---\n\ncontent`);
|
|
|
|
assert.deepEqual(parse2('remove'), '\n \n\n\ncontent');
|
|
|
|
assert.deepEqual(parse2('empty-with-spaces'), `\n \n \n \n \n\ncontent`);
|
|
|
|
assert.deepEqual(parse2('empty-with-lines'), `\n \n\n\n\n\ncontent`);
|
|
|
|
});
|
|
|
|
});
|