Skip to content

Commit

Permalink
fix(Changelogs): storyId as string, fix parse last line (#361)
Browse files Browse the repository at this point in the history
  • Loading branch information
Feverqwe authored Jan 18, 2024
1 parent 95d9ad7 commit e4aa941
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 19 deletions.
33 changes: 19 additions & 14 deletions src/transform/plugins/changelog/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -28,33 +28,38 @@ type Options = Pick<MarkdownItPluginOpts, 'path' | 'log'> & {
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);
Expand Down
12 changes: 8 additions & 4 deletions src/transform/plugins/changelog/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ function parseBody(tokens: Token[], state: StateCore) {
throw new Error('Metadata tag not found');
}

let metadata: object = {};
let metadata: Record<string, unknown> = {};
const rawMetadata = yaml.load(metadataToken.content, {
schema: yaml.JSON_SCHEMA,
});
}) as Record<string, unknown>;
if (rawMetadata && typeof rawMetadata === 'object') {
metadata = rawMetadata;
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -135,13 +139,13 @@ const changelog: MarkdownItPluginCb<Options> = 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;
Expand Down
25 changes: 24 additions & 1 deletion test/changelog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('Changelog', () => {
return {
date,
index,
storyId: 123321,
storyId: '123321',
title: 'Change log title',
image: {
alt: 'My image',
Expand Down Expand Up @@ -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);
});
});
10 changes: 10 additions & 0 deletions test/data/changelog/changelog_edge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% changelog %}
```
storyId: 123321
```
# Change log title
![My image](../src/asd.png =16x9)

Change log payload

{% endchangelog %}

0 comments on commit e4aa941

Please sign in to comment.