Skip to content

Commit c9742e2

Browse files
committed
fix(Changelogs): storyId as string, fix parse last line
1 parent 95d9ad7 commit c9742e2

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

src/transform/plugins/changelog/collect.ts

+19-14
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import imsize from '../imsize';
66
import {MarkdownItPluginOpts} from '../typings';
77

88
const BLOCK_START = '{% changelog %}';
9-
const BLOCK_END = '{% endchangelog %}\n';
9+
const BLOCK_END = '{% endchangelog %}';
1010

1111
function parseChangelogs(str: string, path?: string) {
1212
const {parse, compile, env} = initMarkdownit({
@@ -28,33 +28,38 @@ type Options = Pick<MarkdownItPluginOpts, 'path' | 'log'> & {
2828
const collect = (input: string, {path: filepath, log, changelogs, extractChangelogs}: Options) => {
2929
let result = input;
3030
let lastPos = 0;
31-
const rawChanges = [];
31+
const rawChangelogs = [];
3232

3333
// eslint-disable-next-line no-constant-condition
3434
while (true) {
35-
const pos = result.indexOf(BLOCK_START, lastPos);
36-
lastPos = pos;
37-
if (pos === -1) {
35+
const startPos = result.indexOf(BLOCK_START, lastPos);
36+
lastPos = startPos;
37+
if (startPos === -1) {
3838
break;
3939
}
40-
const endPos = result.indexOf(BLOCK_END, pos + BLOCK_START.length);
41-
if (endPos === -1) {
40+
41+
const endBlockPos = result.indexOf(BLOCK_END, startPos + BLOCK_START.length);
42+
if (endBlockPos === -1) {
4243
log.error(`Changelog block must be closed${filepath ? ` in ${bold(filepath)}` : ''}`);
4344
break;
4445
}
46+
let endPos = endBlockPos + BLOCK_END.length;
47+
if (result[endPos + 1] === '\n') {
48+
endPos += 1;
49+
}
4550

46-
const change = result.slice(pos, endPos + BLOCK_END.length);
51+
const changelog = result.slice(startPos, endPos);
4752

48-
rawChanges.push(change);
53+
rawChangelogs.push(changelog);
4954

50-
result = result.slice(0, pos) + result.slice(endPos + BLOCK_END.length);
55+
result = result.slice(0, startPos) + result.slice(endPos);
5156
}
5257

53-
if (rawChanges.length && changelogs && extractChangelogs) {
54-
const parsedChangelogs = parseChangelogs(rawChanges.join('\n\n'), filepath);
55-
if (parsedChangelogs.length !== rawChanges.length) {
58+
if (rawChangelogs.length && changelogs && extractChangelogs) {
59+
const parsedChangelogs = parseChangelogs(rawChangelogs.join('\n\n'), filepath);
60+
if (parsedChangelogs.length !== rawChangelogs.length) {
5661
log.error(
57-
`Parsed cahngelogs less than expected${filepath ? ` in ${bold(filepath)}` : ''}`,
62+
`Parsed changelogs less than expected${filepath ? ` in ${bold(filepath)}` : ''}`,
5863
);
5964
}
6065
changelogs.push(...parsedChangelogs);

src/transform/plugins/changelog/index.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ function parseBody(tokens: Token[], state: StateCore) {
5555
throw new Error('Metadata tag not found');
5656
}
5757

58-
let metadata: object = {};
58+
let metadata: Record<string, unknown> = {};
5959
const rawMetadata = yaml.load(metadataToken.content, {
6060
schema: yaml.JSON_SCHEMA,
61-
});
61+
}) as Record<string, unknown>;
6262
if (rawMetadata && typeof rawMetadata === 'object') {
6363
metadata = rawMetadata;
6464
}
@@ -93,6 +93,10 @@ function parseBody(tokens: Token[], state: StateCore) {
9393

9494
const description = md.renderer.render(tokens, md.options, env);
9595

96+
if (typeof metadata.storyId === "number") {
97+
metadata.storyId = String(metadata.storyId);
98+
}
99+
96100
return {
97101
...metadata,
98102
title,
@@ -135,13 +139,13 @@ const changelog: MarkdownItPluginCb<Options> = function (md, {extractChangelogs,
135139
content.splice(-3);
136140

137141
try {
138-
const change = parseBody(content, state);
142+
const changelogLocal = parseBody(content, state);
139143

140144
if (!env.changelogs) {
141145
env.changelogs = [];
142146
}
143147

144-
env.changelogs.push(change);
148+
env.changelogs.push(changelogLocal);
145149
} catch (err) {
146150
log.error(`Changelog error: ${(err as Error).message} in ${bold(path)}`);
147151
continue;

test/changelog.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('Changelog', () => {
1212
return {
1313
date,
1414
index,
15-
storyId: 123321,
15+
storyId: '123321',
1616
title: 'Change log title',
1717
image: {
1818
alt: 'My image',

0 commit comments

Comments
 (0)