@diplodoc/transform 4.8.1 → 4.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,7 +6,7 @@ import imsize from '../imsize';
6
6
  import {MarkdownItPluginOpts} from '../typings';
7
7
 
8
8
  const BLOCK_START = '{% changelog %}';
9
- const BLOCK_END = '{% endchangelog %}\n';
9
+ const BLOCK_END = '{% endchangelog %}';
10
10
 
11
11
  function parseChangelogs(str: string, path?: string) {
12
12
  const {parse, compile, env} = initMarkdownit({
@@ -28,33 +28,38 @@ type Options = Pick<MarkdownItPluginOpts, 'path' | 'log'> & {
28
28
  const collect = (input: string, {path: filepath, log, changelogs, extractChangelogs}: Options) => {
29
29
  let result = input;
30
30
  let lastPos = 0;
31
- const rawChanges = [];
31
+ const rawChangelogs = [];
32
32
 
33
33
  // eslint-disable-next-line no-constant-condition
34
34
  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) {
38
38
  break;
39
39
  }
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) {
42
43
  log.error(`Changelog block must be closed${filepath ? ` in ${bold(filepath)}` : ''}`);
43
44
  break;
44
45
  }
46
+ let endPos = endBlockPos + BLOCK_END.length;
47
+ if (result[endPos + 1] === '\n') {
48
+ endPos += 1;
49
+ }
45
50
 
46
- const change = result.slice(pos, endPos + BLOCK_END.length);
51
+ const changelog = result.slice(startPos, endPos);
47
52
 
48
- rawChanges.push(change);
53
+ rawChangelogs.push(changelog);
49
54
 
50
- result = result.slice(0, pos) + result.slice(endPos + BLOCK_END.length);
55
+ result = result.slice(0, startPos) + result.slice(endPos);
51
56
  }
52
57
 
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) {
56
61
  log.error(
57
- `Parsed cahngelogs less than expected${filepath ? ` in ${bold(filepath)}` : ''}`,
62
+ `Parsed changelogs less than expected${filepath ? ` in ${bold(filepath)}` : ''}`,
58
63
  );
59
64
  }
60
65
  changelogs.push(...parsedChangelogs);
@@ -55,10 +55,10 @@ function parseBody(tokens: Token[], state: StateCore) {
55
55
  throw new Error('Metadata tag not found');
56
56
  }
57
57
 
58
- let metadata: object = {};
58
+ let metadata: Record<string, unknown> = {};
59
59
  const rawMetadata = yaml.load(metadataToken.content, {
60
60
  schema: yaml.JSON_SCHEMA,
61
- });
61
+ }) as Record<string, unknown>;
62
62
  if (rawMetadata && typeof rawMetadata === 'object') {
63
63
  metadata = rawMetadata;
64
64
  }
@@ -93,6 +93,10 @@ function parseBody(tokens: Token[], state: StateCore) {
93
93
 
94
94
  const description = md.renderer.render(tokens, md.options, env);
95
95
 
96
+ if (typeof metadata.storyId === 'number') {
97
+ metadata.storyId = String(metadata.storyId);
98
+ }
99
+
96
100
  return {
97
101
  ...metadata,
98
102
  title,
@@ -135,13 +139,13 @@ const changelog: MarkdownItPluginCb<Options> = function (md, {extractChangelogs,
135
139
  content.splice(-3);
136
140
 
137
141
  try {
138
- const change = parseBody(content, state);
142
+ const changelogLocal = parseBody(content, state);
139
143
 
140
144
  if (!env.changelogs) {
141
145
  env.changelogs = [];
142
146
  }
143
147
 
144
- env.changelogs.push(change);
148
+ env.changelogs.push(changelogLocal);
145
149
  } catch (err) {
146
150
  log.error(`Changelog error: ${(err as Error).message} in ${bold(path)}`);
147
151
  continue;
@@ -58,7 +58,10 @@ function matchWrongNotes(tokens: Token[], i: number) {
58
58
 
59
59
  const findCloseTokenIdx = closeTokenFactory('Note', matchOpenToken, matchCloseToken);
60
60
 
61
- const notes: MarkdownItPluginCb = (md, {lang, path: optPath, log}) => {
61
+ // @ts-ignore
62
+ const notes: MarkdownItPluginCb = (md, {lang, notesAutotitle, path: optPath, log}) => {
63
+ notesAutotitle = typeof notesAutotitle === 'boolean' ? notesAutotitle : true;
64
+
62
65
  const plugin = (state: StateCore) => {
63
66
  const {tokens, env} = state;
64
67
  const path = env.path || optPath;
@@ -99,7 +102,9 @@ const notes: MarkdownItPluginCb = (md, {lang, path: optPath, log}) => {
99
102
  titleOpen.block = true;
100
103
  titleClose.block = true;
101
104
 
102
- titleInline.content = match[2] === undefined ? getTitle(type, lang) : match[2];
105
+ const autotitle = notesAutotitle ? getTitle(type, lang) : '';
106
+
107
+ titleInline.content = match[2] === undefined ? autotitle : match[2];
103
108
  titleInline.children = [];
104
109
 
105
110
  const contentOpen = new state.Token('yfm_note_content_open', 'div', 1);