UNPKG

@mdfriday/foundry

Version:

The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.

156 lines 7.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const item_1 = require("./item"); const pagelexer_1 = require("./pagelexer"); function nti(tp, text) { return { typ: tp, text }; } async function collect(input, skipFrontMatter, stateStart) { const [items, err] = (0, pagelexer_1.ParseBytes)(input, {}); if (err) { throw err; } return items; } const tstJSON = '{ "a": { "b": "\\"Hugo\\"}" } }'; const tstFrontMatterTOML = nti(item_1.ItemType.TypeFrontMatterTOML, "foo = \"bar\"\n"); const tstFrontMatterYAML = nti(item_1.ItemType.TypeFrontMatterYAML, "foo: \"bar\"\n"); const tstFrontMatterYAMLCRLF = nti(item_1.ItemType.TypeFrontMatterYAML, "foo: \"bar\"\r\n"); const tstFrontMatterJSON = nti(item_1.ItemType.TypeFrontMatterJSON, tstJSON + "\r\n"); const tstSomeText = nti(item_1.ItemType.tText, "\nSome text.\n"); const tstSummaryDivider = nti(item_1.ItemType.TypeLeadSummaryDivider, "<!--more-->\n"); const tstNewline = nti(item_1.ItemType.tText, "\n"); const tstEOF = nti(item_1.ItemType.tEOF, ""); // Create UTF-8 BOM as a single Unicode character (U+FEFF) const bomStr = String.fromCharCode(0xFEFF); console.log('[Test] BOM test case:', { test: { name: "Byte order mark", input: bomStr + "Some text.", expected: "7: 22: $Some text.$1: " }, bomStr: Array.from(new TextEncoder().encode(bomStr)).map(b => b.toString(16)) }); const tstORG = ` #+TITLE: T1 #+AUTHOR: A1 #+DESCRIPTION: D1 `; const tstFrontMatterORG = nti(item_1.ItemType.TypeFrontMatterORG, tstORG); const crLfReplacer = (s) => s.replace(/\r/g, '#').replace(/\n/g, '$'); const frontMatterTests = [ { name: "empty", input: "", items: [tstEOF], err: null }, { name: "Byte order mark", input: bomStr + "Some text.\n", items: [nti(item_1.ItemType.TypeIgnore, bomStr), nti(item_1.ItemType.tText, "Some text.\n"), tstEOF], err: null }, { name: "No front matter", input: "\nSome text.\n", items: [tstSomeText, tstEOF], err: null }, { name: "YAML front matter", input: "---\nfoo: \"bar\"\n---\n\nSome text.\n", items: [tstFrontMatterYAML, tstSomeText, tstEOF], err: null }, { name: "YAML empty front matter", input: "---\n---\n\nSome text.\n", items: [nti(item_1.ItemType.TypeFrontMatterYAML, ""), tstSomeText, tstEOF], err: null }, { name: "YAML front matter CRLF", input: "---\r\nfoo: \"bar\"\r\n---\n\nSome text.\n", items: [tstFrontMatterYAMLCRLF, tstSomeText, tstEOF], err: null }, { name: "TOML front matter", input: "+++\nfoo = \"bar\"\n+++\n\nSome text.\n", items: [tstFrontMatterTOML, tstSomeText, tstEOF], err: null }, { name: "JSON front matter", input: tstJSON + "\r\n\nSome text.\n", items: [tstFrontMatterJSON, tstSomeText, tstEOF], err: null }, { name: "ORG front matter", input: tstORG + "\nSome text.\n", items: [tstFrontMatterORG, tstSomeText, tstEOF], err: null }, { name: "Summary divider ORG", input: tstORG + "\nSome text.\n# more\nSome text.\n", items: [tstFrontMatterORG, tstSomeText, nti(item_1.ItemType.TypeLeadSummaryDivider, "# more\n"), nti(item_1.ItemType.tText, "Some text.\n"), tstEOF], err: null }, { name: "Summary divider", input: "+++\nfoo = \"bar\"\n+++\n\nSome text.\n<!--more-->\nSome text.\n", items: [tstFrontMatterTOML, tstSomeText, tstSummaryDivider, nti(item_1.ItemType.tText, "Some text.\n"), tstEOF], err: null }, { name: "Summary divider same line", input: "+++\nfoo = \"bar\"\n+++\n\nSome text.<!--more-->Some text.\n", items: [tstFrontMatterTOML, nti(item_1.ItemType.tText, "\nSome text."), nti(item_1.ItemType.TypeLeadSummaryDivider, "<!--more-->"), nti(item_1.ItemType.tText, "Some text.\n"), tstEOF], err: null }, { name: "Summary and shortcode, no space", input: "+++\nfoo = \"bar\"\n+++\n\nSome text.\n<!--more-->{{< sc1 >}}\nSome text.\n", items: [tstFrontMatterTOML, tstSomeText, nti(item_1.ItemType.TypeLeadSummaryDivider, "<!--more-->"), nti(item_1.ItemType.tLeftDelimScNoMarkup, "{{<"), nti(item_1.ItemType.tScName, "sc1"), nti(item_1.ItemType.tRightDelimScNoMarkup, ">}}"), tstSomeText, tstEOF], err: null }, { name: "Summary and shortcode only", input: "+++\nfoo = \"bar\"\n+++\n{{< sc1 >}}\n<!--more-->\n{{< sc2 >}}", items: [tstFrontMatterTOML, nti(item_1.ItemType.tLeftDelimScNoMarkup, "{{<"), nti(item_1.ItemType.tScName, "sc1"), nti(item_1.ItemType.tRightDelimScNoMarkup, ">}}"), tstNewline, tstSummaryDivider, nti(item_1.ItemType.tLeftDelimScNoMarkup, "{{<"), nti(item_1.ItemType.tScName, "sc2"), nti(item_1.ItemType.tRightDelimScNoMarkup, ">}}"), tstEOF], err: null }, ]; describe('pageparser intro', () => { describe('FrontMatter', () => { frontMatterTests.forEach((test) => { it(test.name, async () => { if (test.name === "Byte order mark") { console.log('[Test] BOM test details:', { bomStr, bomBytes: Array.from(new TextEncoder().encode(bomStr)).map(b => b.toString(16)) }); } const items = await collect(new TextEncoder().encode(test.input), false, pagelexer_1.lexIntroSection); if (test.err) { expect(items[0].Err).toEqual(test.err); } else { const got = itemsToString(items, new TextEncoder().encode(test.input)); const expectedStr = testItemsToString(test.items); expect(got).toEqual(expectedStr); } }); }); }); }); function itemsToString(items, source) { const parts = []; for (let i = 0; i < items.length; i++) { const item = items[i]; let s; if (item.Err) { s = item.Err.message; } else if (item.Type === item_1.ItemType.TypeIgnore && item.firstByte === 0xEF) { s = ""; } else { s = new TextDecoder().decode(item.Val(source)); } parts.push(`${item.Type}: ${s}`); } return parts.join('\n'); } function testItemsToString(items) { const parts = []; for (let i = 0; i < items.length; i++) { const item = items[i]; let s = item.text; if (item.typ === item_1.ItemType.TypeIgnore && item.text === String.fromCharCode(0xFEFF)) { s = ""; } parts.push(`${item.typ}: ${s}`); } return parts.join('\n'); } function equal(source, got, expect) { if (got.length !== expect.length) { console.log('Length mismatch:', { got: got.length, expected: expect.length }); return false; } const sourceb = new TextEncoder().encode(source); for (let k = 0; k < got.length; k++) { const g = got[k]; const e = expect[k]; if (g.Type !== e.typ) { console.log('Type mismatch at position', k, { got: g.Type, expected: e.typ }); return false; } let s; if (g.Err) { s = g.Err.message; } else { s = new TextDecoder().decode(g.Val(sourceb)); } if (s !== e.text) { console.log('Text mismatch at position', k, { got: { text: s, length: s.length, charCodes: Array.from(s).map(c => c.charCodeAt(0)), hex: Array.from(s).map(c => c.charCodeAt(0).toString(16)) }, expected: { text: e.text, length: e.text.length, charCodes: Array.from(e.text).map(c => c.charCodeAt(0)), hex: Array.from(e.text).map(c => c.charCodeAt(0).toString(16)) } }); return false; } } return true; } //# sourceMappingURL=pageparser_intro.test.js.map