diff --git a/src/transform/plugins/changelog/collect.ts b/src/transform/plugins/changelog/collect.ts index 0f073835..1d26a36e 100644 --- a/src/transform/plugins/changelog/collect.ts +++ b/src/transform/plugins/changelog/collect.ts @@ -51,7 +51,13 @@ const collect = (input: string, {path: filepath, log, changelogs, extractChangel } if (rawChanges.length && changelogs && extractChangelogs) { - changelogs.push(...parseChangelogs(rawChanges.join('\n\n'), filepath)); + const parsedChangelogs = parseChangelogs(rawChanges.join('\n\n'), filepath); + if (parsedChangelogs.length !== rawChanges.length) { + log.error( + `Parsed cahngelogs less than expected${filepath ? ` in ${bold(filepath)}` : ''}`, + ); + } + changelogs.push(...parsedChangelogs); } return result; diff --git a/src/transform/plugins/changelog/index.ts b/src/transform/plugins/changelog/index.ts index e99774e8..b353a7c4 100644 --- a/src/transform/plugins/changelog/index.ts +++ b/src/transform/plugins/changelog/index.ts @@ -5,10 +5,6 @@ import {bold} from 'chalk'; import yaml from 'js-yaml'; import StateCore from 'markdown-it/lib/rules_core/state_core'; -interface Metadata { - date: string; -} - interface Options { extractChangelogs?: boolean; } @@ -58,11 +54,14 @@ function parseBody(tokens: Token[], state: StateCore) { if (metadataToken?.type !== 'fence') { throw new Error('Metadata tag not found'); } - const rawMetadata = yaml.load(metadataToken.content) as Metadata; - const metadata = { - ...rawMetadata, - date: new Date(rawMetadata.date).toISOString(), - }; + + let metadata: object = {}; + const rawMetadata = yaml.load(metadataToken.content, { + schema: yaml.JSON_SCHEMA, + }); + if (rawMetadata && typeof rawMetadata === 'object') { + metadata = rawMetadata; + } if (!isTitle(tokens)) { throw new Error('Title tag not found'); diff --git a/src/transform/plugins/changelog/types.ts b/src/transform/plugins/changelog/types.ts index e85ead75..22ff1ffe 100644 --- a/src/transform/plugins/changelog/types.ts +++ b/src/transform/plugins/changelog/types.ts @@ -6,6 +6,5 @@ export interface ChangelogItem { ratio?: string; }; description: string; - date: string; [x: string]: unknown; } diff --git a/test/changelog.test.ts b/test/changelog.test.ts index 1a1871d5..a82cd96c 100644 --- a/test/changelog.test.ts +++ b/test/changelog.test.ts @@ -8,10 +8,28 @@ import {Logger} from '../src/transform/log'; import {ChangelogItem} from '../src/transform/plugins/changelog/types'; describe('Changelog', () => { + function getItem(date: string | undefined, index: number | undefined) { + return { + date, + index, + storyId: 123321, + title: 'Change log title', + image: { + alt: 'My image', + ratio: 0.5625, + src: '../src/asd.png', + }, + description: '

Change log payload

\n', + }; + } + test('Should cut changelog', async () => { expect.assertions(2); - const data = await fs.promises.readFile(path.join(__dirname, 'data/changelog.md'), 'utf8'); + const data = await fs.promises.readFile( + path.join(__dirname, 'data/changelog/changelog.md'), + 'utf8', + ); const { result: {html, changelogs: logs}, @@ -24,10 +42,33 @@ describe('Changelog', () => { expect(logs).toBe(undefined); }); - test('Should cut changelog and write it in env', async () => { + test('Should cut changelog with date and write it in env', async () => { + expect.assertions(2); + + const data = await fs.promises.readFile( + path.join(__dirname, 'data/changelog/changelog_date.md'), + 'utf8', + ); + + const { + result: {html, changelogs: logs}, + } = transform(data, { + plugins: [changelogPlugin, imsize], + extractChangelogs: true, + }); + + expect(html).toBe(`

Some changelog

\n

After changelog

\n`); + + expect(logs).toEqual(new Array(3).fill(getItem('2023-05-10', undefined))); + }); + + test('Should cut changelog with index and write it in env', async () => { expect.assertions(2); - const data = await fs.promises.readFile(path.join(__dirname, 'data/changelog.md'), 'utf8'); + const data = await fs.promises.readFile( + path.join(__dirname, 'data/changelog/changelog_index.md'), + 'utf8', + ); const { result: {html, changelogs: logs}, @@ -39,24 +80,39 @@ describe('Changelog', () => { expect(html).toBe(`

Some changelog

\n

After changelog

\n`); expect(logs).toEqual( - new Array(3).fill({ - date: '2023-05-10T00:00:00.000Z', - storyId: 123321, - title: 'Change log title', - image: { - alt: 'My image', - ratio: 0.5625, - src: '../src/asd.png', - }, - description: '

Change log payload

\n', + new Array(3).fill(getItem(undefined, 0)).map((item, index) => { + return {...item, index: 3 - index}; }), ); }); + test('Should cut changelog and write it in env', async () => { + expect.assertions(2); + + const data = await fs.promises.readFile( + path.join(__dirname, 'data/changelog/changelog.md'), + 'utf8', + ); + + const { + result: {html, changelogs: logs}, + } = transform(data, { + plugins: [changelogPlugin, imsize], + extractChangelogs: true, + }); + + expect(html).toBe(`

Some changelog

\n

After changelog

\n`); + + expect(logs).toEqual(new Array(3).fill(getItem(undefined, undefined))); + }); + test('Should cut changelog and write it in variable', async () => { expect.assertions(2); - const data = await fs.promises.readFile(path.join(__dirname, 'data/changelog.md'), 'utf8'); + const data = await fs.promises.readFile( + path.join(__dirname, 'data/changelog/changelog.md'), + 'utf8', + ); const changelogs: ChangelogItem[] = []; const html = changelogCollect(data, { diff --git a/test/data/changelog/changelog.md b/test/data/changelog/changelog.md new file mode 100644 index 00000000..a72e27f2 --- /dev/null +++ b/test/data/changelog/changelog.md @@ -0,0 +1,37 @@ +# Some changelog + +{% changelog %} +``` +storyId: 123321 +``` +# Change log title +![My image](../src/asd.png =16x9) + +Change log payload + +{% endchangelog %} + +{% changelog %} +``` +storyId: 123321 +``` +# Change log title +![My image](../src/asd.png =16x9) + +Change log payload + +{% endchangelog %} + + +{% changelog %} +``` +storyId: 123321 +``` +# Change log title +![My image](../src/asd.png =16x9) + +Change log payload + +{% endchangelog %} + +After changelog diff --git a/test/data/changelog.md b/test/data/changelog/changelog_date.md similarity index 100% rename from test/data/changelog.md rename to test/data/changelog/changelog_date.md diff --git a/test/data/changelog/changelog_index.md b/test/data/changelog/changelog_index.md new file mode 100644 index 00000000..f352f5b5 --- /dev/null +++ b/test/data/changelog/changelog_index.md @@ -0,0 +1,40 @@ +# Some changelog + +{% changelog %} +``` +index: 3 +storyId: 123321 +``` +# Change log title +![My image](../src/asd.png =16x9) + +Change log payload + +{% endchangelog %} + +{% changelog %} +``` +index: 2 +storyId: 123321 +``` +# Change log title +![My image](../src/asd.png =16x9) + +Change log payload + +{% endchangelog %} + + +{% changelog %} +``` +index: 1 +storyId: 123321 +``` +# Change log title +![My image](../src/asd.png =16x9) + +Change log payload + +{% endchangelog %} + +After changelog