@mdfriday/foundry
Version:
The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.
97 lines (94 loc) • 4.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const pageparser_1 = require("./pageparser");
const item_1 = require("./item");
const pageparser_2 = require("./pageparser");
const util_1 = require("util");
describe('pageparser', () => {
describe('ParseBytes', () => {
it('should parse content with front matter and shortcodes', () => {
const start = `
---
title: "Front Matters"
description: "It really does"
---
This is some summary. This is some summary. This is some summary. This is some summary.
<!--more-->
`;
const input = new util_1.TextEncoder().encode(start +
Array(10).fill(Array(30).fill('this is text').join('') +
'{{< myshortcode >}}This is some inner content.{{< /myshortcode >}}').join(''));
const cfg = {};
const [items, err] = (0, pageparser_1.ParseBytes)(input, cfg);
expect(err).toBeNull();
expect(items.length).toBeGreaterThan(0);
expect(items[0].Type).toBe(item_1.ItemType.TypeFrontMatterYAML);
});
});
describe('FormatFromFrontMatterType', () => {
const tests = [
{ typ: item_1.ItemType.TypeFrontMatterJSON, expect: pageparser_2.Format.JSON },
{ typ: item_1.ItemType.TypeFrontMatterTOML, expect: pageparser_2.Format.TOML },
{ typ: item_1.ItemType.TypeFrontMatterYAML, expect: pageparser_2.Format.YAML },
{ typ: item_1.ItemType.TypeFrontMatterORG, expect: pageparser_2.Format.ORG },
{ typ: item_1.ItemType.TypeIgnore, expect: pageparser_2.Format.UNKNOWN },
];
test.each(tests)('should return $expect for type $typ', ({ typ, expect: expected }) => {
expect((0, pageparser_1.FormatFromFrontMatterType)(typ)).toBe(expected);
});
});
describe('IsProbablySourceOfItems', () => {
it('should correctly identify source of items', () => {
const input = ' {{< foo >}} ';
const [items, err] = (0, pageparser_1.collectStringMain)(input);
expect(err).toBeNull();
console.log('Input:', input);
console.log('Items:', items.map(item => ({
Type: item.Type,
Pos: item.Pos(),
firstByte: item['firstByte'],
Val: new TextDecoder().decode(item.Val(new util_1.TextEncoder().encode(input)))
})));
expect((0, pageparser_1.IsProbablySourceOfItems)(new util_1.TextEncoder().encode(input), items)).toBe(true);
expect((0, pageparser_1.IsProbablySourceOfItems)(new util_1.TextEncoder().encode(' '.repeat(input.length)), items)).toBe(false);
expect((0, pageparser_1.IsProbablySourceOfItems)(new util_1.TextEncoder().encode('{{< foo >}} '), items)).toBe(false);
expect((0, pageparser_1.IsProbablySourceOfItems)(new util_1.TextEncoder().encode(''), items)).toBe(false);
});
});
describe('HasShortcode', () => {
const tests = [
{ input: '{{< foo >}}', expected: true },
{ input: 'aSDasd SDasd aSD\n\nasdfadf{{% foo %}}\nasdf', expected: true },
{ input: '{{</* foo */>}}', expected: false },
{ input: '{{%/* foo */%}}', expected: false },
];
test.each(tests)('should return $expected for "$input"', ({ input, expected }) => {
expect((0, pageparser_1.HasShortcode)(input)).toBe(expected);
});
describe('performance', () => {
const withShortcode = Array(30).fill('this is text').join('') +
'{{< myshortcode >}}This is some inner content.{{< /myshortcode >}}' +
Array(30).fill('this is text').join('');
const withoutShortcode = Array(30).fill('this is text').join('') +
'This is some inner content.' +
Array(30).fill('this is text').join('');
it('should quickly identify shortcode when present', () => {
const start = performance.now();
for (let i = 0; i < 1000; i++) {
(0, pageparser_1.HasShortcode)(withShortcode);
}
const duration = performance.now() - start;
expect(duration).toBeLessThan(1000); // should complete 1000 iterations in less than 1 second
});
it('should quickly identify when no shortcode present', () => {
const start = performance.now();
for (let i = 0; i < 1000; i++) {
(0, pageparser_1.HasShortcode)(withoutShortcode);
}
const duration = performance.now() - start;
expect(duration).toBeLessThan(1000); // should complete 1000 iterations in less than 1 second
});
});
});
});
//# sourceMappingURL=pageparser.test.js.map