From e4aa941357e41232a7a0c8beb96632a50b258823 Mon Sep 17 00:00:00 2001 From: Anton Date: Thu, 18 Jan 2024 16:23:55 +0500 Subject: [PATCH] fix(Changelogs): storyId as string, fix parse last line (#361) --- src/transform/plugins/changelog/collect.ts | 33 +++++++++++++--------- src/transform/plugins/changelog/index.ts | 12 +++++--- test/changelog.test.ts | 25 +++++++++++++++- test/data/changelog/changelog_edge.md | 10 +++++++ 4 files changed, 61 insertions(+), 19 deletions(-) create mode 100644 test/data/changelog/changelog_edge.md diff --git a/src/transform/plugins/changelog/collect.ts b/src/transform/plugins/changelog/collect.ts index 1d26a36e..b11a286e 100644 --- a/src/transform/plugins/changelog/collect.ts +++ b/src/transform/plugins/changelog/collect.ts @@ -6,7 +6,7 @@ import imsize from '../imsize'; import {MarkdownItPluginOpts} from '../typings'; const BLOCK_START = '{% changelog %}'; -const BLOCK_END = '{% endchangelog %}\n'; +const BLOCK_END = '{% endchangelog %}'; function parseChangelogs(str: string, path?: string) { const {parse, compile, env} = initMarkdownit({ @@ -28,33 +28,38 @@ type Options = Pick & { const collect = (input: string, {path: filepath, log, changelogs, extractChangelogs}: Options) => { let result = input; let lastPos = 0; - const rawChanges = []; + const rawChangelogs = []; // eslint-disable-next-line no-constant-condition while (true) { - const pos = result.indexOf(BLOCK_START, lastPos); - lastPos = pos; - if (pos === -1) { + const startPos = result.indexOf(BLOCK_START, lastPos); + lastPos = startPos; + if (startPos === -1) { break; } - const endPos = result.indexOf(BLOCK_END, pos + BLOCK_START.length); - if (endPos === -1) { + + const endBlockPos = result.indexOf(BLOCK_END, startPos + BLOCK_START.length); + if (endBlockPos === -1) { log.error(`Changelog block must be closed${filepath ? ` in ${bold(filepath)}` : ''}`); break; } + let endPos = endBlockPos + BLOCK_END.length; + if (result[endPos + 1] === '\n') { + endPos += 1; + } - const change = result.slice(pos, endPos + BLOCK_END.length); + const changelog = result.slice(startPos, endPos); - rawChanges.push(change); + rawChangelogs.push(changelog); - result = result.slice(0, pos) + result.slice(endPos + BLOCK_END.length); + result = result.slice(0, startPos) + result.slice(endPos); } - if (rawChanges.length && changelogs && extractChangelogs) { - const parsedChangelogs = parseChangelogs(rawChanges.join('\n\n'), filepath); - if (parsedChangelogs.length !== rawChanges.length) { + if (rawChangelogs.length && changelogs && extractChangelogs) { + const parsedChangelogs = parseChangelogs(rawChangelogs.join('\n\n'), filepath); + if (parsedChangelogs.length !== rawChangelogs.length) { log.error( - `Parsed cahngelogs less than expected${filepath ? ` in ${bold(filepath)}` : ''}`, + `Parsed changelogs less than expected${filepath ? ` in ${bold(filepath)}` : ''}`, ); } changelogs.push(...parsedChangelogs); diff --git a/src/transform/plugins/changelog/index.ts b/src/transform/plugins/changelog/index.ts index b353a7c4..1f933a7b 100644 --- a/src/transform/plugins/changelog/index.ts +++ b/src/transform/plugins/changelog/index.ts @@ -55,10 +55,10 @@ function parseBody(tokens: Token[], state: StateCore) { throw new Error('Metadata tag not found'); } - let metadata: object = {}; + let metadata: Record = {}; const rawMetadata = yaml.load(metadataToken.content, { schema: yaml.JSON_SCHEMA, - }); + }) as Record; if (rawMetadata && typeof rawMetadata === 'object') { metadata = rawMetadata; } @@ -93,6 +93,10 @@ function parseBody(tokens: Token[], state: StateCore) { const description = md.renderer.render(tokens, md.options, env); + if (typeof metadata.storyId === 'number') { + metadata.storyId = String(metadata.storyId); + } + return { ...metadata, title, @@ -135,13 +139,13 @@ const changelog: MarkdownItPluginCb = function (md, {extractChangelogs, content.splice(-3); try { - const change = parseBody(content, state); + const changelogLocal = parseBody(content, state); if (!env.changelogs) { env.changelogs = []; } - env.changelogs.push(change); + env.changelogs.push(changelogLocal); } catch (err) { log.error(`Changelog error: ${(err as Error).message} in ${bold(path)}`); continue; diff --git a/test/changelog.test.ts b/test/changelog.test.ts index a82cd96c..dbef522c 100644 --- a/test/changelog.test.ts +++ b/test/changelog.test.ts @@ -12,7 +12,7 @@ describe('Changelog', () => { return { date, index, - storyId: 123321, + storyId: '123321', title: 'Change log title', image: { alt: 'My image', @@ -133,4 +133,27 @@ After changelog expect(changelogs.length).toBe(3); }); + + test('Should cut changelog on the edge and write it in variable', async () => { + expect.assertions(2); + + const data = ( + await fs.promises.readFile( + path.join(__dirname, 'data/changelog/changelog_edge.md'), + 'utf8', + ) + ).trim(); + + const changelogs: ChangelogItem[] = []; + const html = changelogCollect(data, { + path: '', + changelogs, + log: console as unknown as Logger, + extractChangelogs: true, + }); + + expect(html).toBe(''); + + expect(changelogs.length).toBe(1); + }); }); diff --git a/test/data/changelog/changelog_edge.md b/test/data/changelog/changelog_edge.md new file mode 100644 index 00000000..5d101877 --- /dev/null +++ b/test/data/changelog/changelog_edge.md @@ -0,0 +1,10 @@ +{% changelog %} +``` +storyId: 123321 +``` +# Change log title +![My image](../src/asd.png =16x9) + +Change log payload + +{% endchangelog %}