keep-a-changelog 0.10.4 → 2.0.0-beta

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.
Files changed (96) hide show
  1. package/CHANGELOG.md +5 -196
  2. package/LICENSE +1 -1
  3. package/README.md +93 -63
  4. package/esm/bin.js +113 -0
  5. package/esm/deps/deno_land/std_0.113.0/_util/assert.js +13 -0
  6. package/esm/deps/deno_land/std_0.113.0/_util/os.js +15 -0
  7. package/esm/deps/deno_land/std_0.113.0/flags/mod.js +255 -0
  8. package/esm/deps/deno_land/std_0.113.0/path/_constants.js +45 -0
  9. package/esm/deps/deno_land/std_0.113.0/path/_interface.js +3 -0
  10. package/esm/deps/deno_land/std_0.113.0/path/_util.js +114 -0
  11. package/esm/deps/deno_land/std_0.113.0/path/common.js +36 -0
  12. package/esm/deps/deno_land/std_0.113.0/path/glob.js +361 -0
  13. package/esm/deps/deno_land/std_0.113.0/path/mod.js +14 -0
  14. package/esm/deps/deno_land/std_0.113.0/path/posix.js +493 -0
  15. package/esm/deps/deno_land/std_0.113.0/path/separator.js +5 -0
  16. package/esm/deps/deno_land/std_0.113.0/path/win32.js +963 -0
  17. package/esm/deps/deno_land/std_0.51.0/fs/eol.js +29 -0
  18. package/esm/deps/deno_land/x/ini_v2.1.0/ini.js +226 -0
  19. package/esm/deps/deno_land/x/ini_v2.1.0/mod.js +1 -0
  20. package/esm/deps/deno_land/x/semver_v1.4.0/mod.js +1527 -0
  21. package/esm/mod.js +5 -0
  22. package/esm/package.json +3 -0
  23. package/esm/src/Change.js +48 -0
  24. package/esm/src/Changelog.js +137 -0
  25. package/esm/src/Release.js +204 -0
  26. package/esm/src/deps.js +1 -0
  27. package/esm/src/parser.js +149 -0
  28. package/esm/test/changelog.custom.type.md +24 -0
  29. package/esm/test/changelog.expected.md +169 -0
  30. package/esm/test/changelog.md +197 -0
  31. package/esm/test/empty.expected.md +6 -0
  32. package/package.json +26 -24
  33. package/types/bin.d.ts +2 -0
  34. package/types/deps/deno_land/std_0.113.0/_util/assert.d.ts +5 -0
  35. package/types/deps/deno_land/std_0.113.0/_util/os.d.ts +3 -0
  36. package/types/deps/deno_land/std_0.113.0/flags/mod.d.ts +50 -0
  37. package/types/deps/deno_land/std_0.113.0/path/_constants.d.ts +39 -0
  38. package/types/deps/deno_land/std_0.113.0/path/_interface.d.ts +26 -0
  39. package/types/deps/deno_land/std_0.113.0/path/_util.d.ts +8 -0
  40. package/types/deps/deno_land/std_0.113.0/path/common.d.ts +13 -0
  41. package/types/deps/deno_land/std_0.113.0/path/glob.d.ts +78 -0
  42. package/types/deps/deno_land/std_0.113.0/path/mod.d.ts +9 -0
  43. package/types/deps/deno_land/std_0.113.0/path/posix.d.ts +80 -0
  44. package/types/deps/deno_land/std_0.113.0/path/separator.d.ts +2 -0
  45. package/types/deps/deno_land/std_0.113.0/path/win32.d.ts +87 -0
  46. package/types/deps/deno_land/std_0.115.1/fmt/colors.d.ts +270 -0
  47. package/types/deps/deno_land/std_0.115.1/testing/_diff.d.ts +23 -0
  48. package/types/deps/deno_land/std_0.115.1/testing/asserts.d.ts +151 -0
  49. package/types/deps/deno_land/std_0.51.0/fs/eol.d.ts +12 -0
  50. package/types/deps/deno_land/x/ini_v2.1.0/ini.d.ts +53 -0
  51. package/types/deps/deno_land/x/ini_v2.1.0/mod.d.ts +1 -0
  52. package/types/deps/deno_land/x/semver_v1.4.0/mod.d.ts +116 -0
  53. package/types/mod.d.ts +5 -0
  54. package/types/src/Change.d.ts +8 -0
  55. package/types/src/Changelog.d.ts +18 -0
  56. package/types/src/Release.d.ts +25 -0
  57. package/types/src/deps.d.ts +1 -0
  58. package/types/src/parser.d.ts +11 -0
  59. package/types/test/Change.test.d.ts +1 -0
  60. package/types/test/fixture/CustomRelease.d.ts +7 -0
  61. package/types/test/parser.test.d.ts +1 -0
  62. package/types/test/test.d.ts +1 -0
  63. package/umd/bin.js +143 -0
  64. package/umd/deps/deno_land/std_0.113.0/_util/assert.js +27 -0
  65. package/umd/deps/deno_land/std_0.113.0/_util/os.js +46 -0
  66. package/umd/deps/deno_land/std_0.113.0/flags/mod.js +268 -0
  67. package/umd/deps/deno_land/std_0.113.0/path/_constants.js +57 -0
  68. package/umd/deps/deno_land/std_0.113.0/path/_interface.js +13 -0
  69. package/umd/deps/deno_land/std_0.113.0/path/_util.js +133 -0
  70. package/umd/deps/deno_land/std_0.113.0/path/common.js +49 -0
  71. package/umd/deps/deno_land/std_0.113.0/path/glob.js +396 -0
  72. package/umd/deps/deno_land/std_0.113.0/path/mod.js +50 -0
  73. package/umd/deps/deno_land/std_0.113.0/path/posix.js +537 -0
  74. package/umd/deps/deno_land/std_0.113.0/path/separator.js +17 -0
  75. package/umd/deps/deno_land/std_0.113.0/path/win32.js +1007 -0
  76. package/umd/deps/deno_land/std_0.51.0/fs/eol.js +43 -0
  77. package/umd/deps/deno_land/x/ini_v2.1.0/ini.js +261 -0
  78. package/umd/deps/deno_land/x/ini_v2.1.0/mod.js +22 -0
  79. package/umd/deps/deno_land/x/semver_v1.4.0/mod.js +1577 -0
  80. package/umd/mod.js +23 -0
  81. package/umd/package.json +3 -0
  82. package/umd/src/Change.js +60 -0
  83. package/umd/src/Changelog.js +149 -0
  84. package/umd/src/Release.js +219 -0
  85. package/umd/src/deps.js +18 -0
  86. package/umd/src/parser.js +164 -0
  87. package/umd/test/changelog.custom.type.md +24 -0
  88. package/umd/test/changelog.expected.md +169 -0
  89. package/umd/test/changelog.md +197 -0
  90. package/umd/test/empty.expected.md +6 -0
  91. package/bin.js +0 -128
  92. package/src/Change.js +0 -40
  93. package/src/Changelog.js +0 -121
  94. package/src/Release.js +0 -220
  95. package/src/index.js +0 -6
  96. package/src/parser.js +0 -182
package/umd/mod.js ADDED
@@ -0,0 +1,23 @@
1
+ var __importDefault = (this && this.__importDefault) || function (mod) {
2
+ return (mod && mod.__esModule) ? mod : { "default": mod };
3
+ };
4
+ (function (factory) {
5
+ if (typeof module === "object" && typeof module.exports === "object") {
6
+ var v = factory(require, exports);
7
+ if (v !== undefined) module.exports = v;
8
+ }
9
+ else if (typeof define === "function" && define.amd) {
10
+ define(["require", "exports", "./src/parser.js", "./src/Change.js", "./src/Changelog.js", "./src/Release.js"], factory);
11
+ }
12
+ })(function (require, exports) {
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.Release = exports.parser = exports.Changelog = exports.Change = void 0;
15
+ const parser_js_1 = __importDefault(require("./src/parser.js"));
16
+ exports.parser = parser_js_1.default;
17
+ const Change_js_1 = __importDefault(require("./src/Change.js"));
18
+ exports.Change = Change_js_1.default;
19
+ const Changelog_js_1 = __importDefault(require("./src/Changelog.js"));
20
+ exports.Changelog = Changelog_js_1.default;
21
+ const Release_js_1 = __importDefault(require("./src/Release.js"));
22
+ exports.Release = Release_js_1.default;
23
+ });
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,60 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ class Change {
12
+ constructor(title, description = "") {
13
+ Object.defineProperty(this, "title", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: void 0
18
+ });
19
+ Object.defineProperty(this, "description", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: void 0
24
+ });
25
+ Object.defineProperty(this, "issues", {
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true,
29
+ value: []
30
+ });
31
+ this.title = Change.extractIssues(title, this.issues);
32
+ this.description = Change.extractIssues(description, this.issues);
33
+ }
34
+ static extractIssues(text, issues) {
35
+ return text
36
+ .replace(/(^|[^\\])\[#(\d+)\](?=[^\(]|$)/g, (_, start, index) => {
37
+ if (!issues.includes(index)) {
38
+ issues.push(index);
39
+ }
40
+ return `${start}[#${index}]`;
41
+ })
42
+ .replace(/(^|[\s,])#(\d+)(?=[\s,\.]|$)/g, (_, start, index) => {
43
+ if (!issues.includes(index)) {
44
+ issues.push(index);
45
+ }
46
+ return `${start}[#${index}]`;
47
+ });
48
+ }
49
+ toString() {
50
+ let t = this.title.split("\n").map((line) => ` ${line}`.trimEnd());
51
+ t[0] = "-" + t[0].substr(1);
52
+ if (this.description) {
53
+ t.push("");
54
+ t = t.concat(this.description.split("\n").map((line) => ` ${line}`.trimEnd()));
55
+ }
56
+ return t.join("\n").trim();
57
+ }
58
+ }
59
+ exports.default = Change;
60
+ });
@@ -0,0 +1,149 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports", "./deps.js"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ const deps_js_1 = require("./deps.js");
12
+ class Changelog {
13
+ constructor(title, description = "") {
14
+ Object.defineProperty(this, "flag", {
15
+ enumerable: true,
16
+ configurable: true,
17
+ writable: true,
18
+ value: void 0
19
+ });
20
+ Object.defineProperty(this, "title", {
21
+ enumerable: true,
22
+ configurable: true,
23
+ writable: true,
24
+ value: void 0
25
+ });
26
+ Object.defineProperty(this, "description", {
27
+ enumerable: true,
28
+ configurable: true,
29
+ writable: true,
30
+ value: void 0
31
+ });
32
+ Object.defineProperty(this, "head", {
33
+ enumerable: true,
34
+ configurable: true,
35
+ writable: true,
36
+ value: "HEAD"
37
+ });
38
+ Object.defineProperty(this, "footer", {
39
+ enumerable: true,
40
+ configurable: true,
41
+ writable: true,
42
+ value: void 0
43
+ });
44
+ Object.defineProperty(this, "url", {
45
+ enumerable: true,
46
+ configurable: true,
47
+ writable: true,
48
+ value: void 0
49
+ });
50
+ Object.defineProperty(this, "releases", {
51
+ enumerable: true,
52
+ configurable: true,
53
+ writable: true,
54
+ value: []
55
+ });
56
+ Object.defineProperty(this, "tagNameBuilder", {
57
+ enumerable: true,
58
+ configurable: true,
59
+ writable: true,
60
+ value: void 0
61
+ });
62
+ this.title = title;
63
+ this.description = description;
64
+ }
65
+ addRelease(release) {
66
+ this.releases.push(release);
67
+ this.sortReleases();
68
+ release.changelog = this;
69
+ return this;
70
+ }
71
+ findRelease(version) {
72
+ if (!version) {
73
+ return this.releases.find((release) => !release.version);
74
+ }
75
+ return this.releases.find((release) => release.version && (0, deps_js_1.eq)(release.version, version));
76
+ }
77
+ sortReleases() {
78
+ this.releases.sort((a, b) => a.compare(b));
79
+ }
80
+ tagName(release) {
81
+ if (this.tagNameBuilder) {
82
+ return this.tagNameBuilder(release);
83
+ }
84
+ return `v${release.version}`;
85
+ }
86
+ toString() {
87
+ const t = [];
88
+ if (this.flag) {
89
+ t.push(`<!-- ${this.flag} -->`);
90
+ t.push("");
91
+ }
92
+ t.push(`# ${this.title}`);
93
+ const links = [];
94
+ const compareLinks = [];
95
+ const description = this.description.trim() ||
96
+ `All notable changes to this project will be documented in this file.
97
+
98
+ The format is based on [Keep a Changelog](http://keepachangelog.com/)
99
+ and this project adheres to [Semantic Versioning](http://semver.org/).`;
100
+ if (description) {
101
+ t.push("");
102
+ t.push(description);
103
+ }
104
+ this.releases.forEach((release) => {
105
+ t.push("");
106
+ t.push(release.toString(this));
107
+ release.getLinks(this).forEach((link) => {
108
+ if (!links.includes(link)) {
109
+ links.push(link);
110
+ }
111
+ });
112
+ const compareLink = release.getCompareLink(this);
113
+ if (compareLink) {
114
+ compareLinks.push(compareLink);
115
+ }
116
+ });
117
+ if (links.length) {
118
+ t.push("");
119
+ links.sort(compare);
120
+ links.forEach((link) => t.push(link));
121
+ }
122
+ if (compareLinks.length) {
123
+ t.push("");
124
+ compareLinks.forEach((link) => t.push(link));
125
+ }
126
+ t.push("");
127
+ if (this.footer) {
128
+ t.push("---");
129
+ t.push("");
130
+ t.push(this.footer);
131
+ t.push("");
132
+ }
133
+ return t.join("\n");
134
+ }
135
+ }
136
+ exports.default = Changelog;
137
+ function compare(a, b) {
138
+ if (a === b) {
139
+ return 0;
140
+ }
141
+ const reg = /^\[#(\d+)\]:/;
142
+ const aNumber = a.match(reg);
143
+ const bNumber = b.match(reg);
144
+ if (aNumber && bNumber) {
145
+ return parseInt(aNumber[1]) - parseInt(bNumber[1]);
146
+ }
147
+ return a < b ? -1 : 1;
148
+ }
149
+ });
@@ -0,0 +1,219 @@
1
+ var __importDefault = (this && this.__importDefault) || function (mod) {
2
+ return (mod && mod.__esModule) ? mod : { "default": mod };
3
+ };
4
+ (function (factory) {
5
+ if (typeof module === "object" && typeof module.exports === "object") {
6
+ var v = factory(require, exports);
7
+ if (v !== undefined) module.exports = v;
8
+ }
9
+ else if (typeof define === "function" && define.amd) {
10
+ define(["require", "exports", "./deps.js", "./Change.js"], factory);
11
+ }
12
+ })(function (require, exports) {
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ const deps_js_1 = require("./deps.js");
15
+ const Change_js_1 = __importDefault(require("./Change.js"));
16
+ class Release {
17
+ constructor(version, date, description = "") {
18
+ Object.defineProperty(this, "changelog", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: void 0
23
+ });
24
+ Object.defineProperty(this, "version", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: void 0
29
+ });
30
+ Object.defineProperty(this, "date", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: void 0
35
+ });
36
+ Object.defineProperty(this, "description", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: void 0
41
+ });
42
+ Object.defineProperty(this, "changes", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: void 0
47
+ });
48
+ this.setVersion(version);
49
+ this.setDate(date);
50
+ this.description = description;
51
+ this.changes = new Map([
52
+ ["added", []],
53
+ ["changed", []],
54
+ ["deprecated", []],
55
+ ["removed", []],
56
+ ["fixed", []],
57
+ ["security", []],
58
+ ]);
59
+ }
60
+ compare(release) {
61
+ if (!this.version && release.version) {
62
+ return -1;
63
+ }
64
+ if (!release.version) {
65
+ return 1;
66
+ }
67
+ if (!this.date && release.date) {
68
+ return -1;
69
+ }
70
+ if (!release.date) {
71
+ return 1;
72
+ }
73
+ if (this.version && release.version) {
74
+ return -this.version.compare(release.version);
75
+ }
76
+ return 0;
77
+ }
78
+ isEmpty() {
79
+ if (this.description.trim()) {
80
+ return false;
81
+ }
82
+ return Array.from(this.changes.values()).every((change) => !change.length);
83
+ }
84
+ setVersion(version) {
85
+ if (typeof version === "string") {
86
+ version = new deps_js_1.Semver(version);
87
+ }
88
+ this.version = version;
89
+ //Re-sort the releases of the parent changelog
90
+ if (this.changelog) {
91
+ this.changelog.sortReleases();
92
+ }
93
+ }
94
+ setDate(date) {
95
+ if (typeof date === "string") {
96
+ date = new Date(date);
97
+ }
98
+ this.date = date;
99
+ }
100
+ addChange(type, change) {
101
+ if (!(change instanceof Change_js_1.default)) {
102
+ change = new Change_js_1.default(change);
103
+ }
104
+ if (!this.changes.has(type)) {
105
+ throw new Error("Invalid change type");
106
+ }
107
+ this.changes.get(type).push(change);
108
+ return this;
109
+ }
110
+ added(change) {
111
+ return this.addChange("added", change);
112
+ }
113
+ changed(change) {
114
+ return this.addChange("changed", change);
115
+ }
116
+ deprecated(change) {
117
+ return this.addChange("deprecated", change);
118
+ }
119
+ removed(change) {
120
+ return this.addChange("removed", change);
121
+ }
122
+ fixed(change) {
123
+ return this.addChange("fixed", change);
124
+ }
125
+ security(change) {
126
+ return this.addChange("security", change);
127
+ }
128
+ toString(changelog) {
129
+ let t = [];
130
+ const hasCompareLink = this.getCompareLink(changelog) !== undefined;
131
+ if (this.version) {
132
+ if (hasCompareLink) {
133
+ t.push(`## [${this.version}] - ${formatDate(this.date)}`);
134
+ }
135
+ else {
136
+ t.push(`## ${this.version} - ${formatDate(this.date)}`);
137
+ }
138
+ }
139
+ else {
140
+ if (hasCompareLink) {
141
+ t.push("## [Unreleased]");
142
+ }
143
+ else {
144
+ t.push("## Unreleased");
145
+ }
146
+ }
147
+ if (this.description.trim()) {
148
+ t.push(this.description.trim());
149
+ t.push("");
150
+ }
151
+ this.changes.forEach((changes, type) => {
152
+ if (changes.length) {
153
+ t.push(`### ${type[0].toUpperCase()}${type.substring(1)}`);
154
+ t = t.concat(changes.map((change) => change.toString()));
155
+ t.push("");
156
+ }
157
+ });
158
+ return t.join("\n").trim();
159
+ }
160
+ getCompareLink(changelog) {
161
+ if (!changelog?.url) {
162
+ return;
163
+ }
164
+ const index = changelog.releases.indexOf(this);
165
+ if (index === -1) {
166
+ return;
167
+ }
168
+ let offset = 1;
169
+ let previous = changelog.releases[index + offset];
170
+ while (previous && !previous.date) {
171
+ ++offset;
172
+ previous = changelog.releases[index + offset];
173
+ }
174
+ if (!previous) {
175
+ if (!this.version || !this.date) {
176
+ return;
177
+ }
178
+ return `[${this.version}]: ${changelog.url}/releases/tag/${changelog.tagName(this)}`;
179
+ }
180
+ if (!this.version) {
181
+ return `[Unreleased]: ${changelog.url}/compare/${changelog.tagName(previous)}...${changelog.head}`;
182
+ }
183
+ if (!this.date) {
184
+ return `[${this.version}]: ${changelog.url}/compare/${changelog.tagName(previous)}...${changelog.head}`;
185
+ }
186
+ return `[${this.version}]: ${changelog.url}/compare/${changelog.tagName(previous)}...${changelog.tagName(this)}`;
187
+ }
188
+ getLinks(changelog) {
189
+ const links = [];
190
+ if (!changelog.url) {
191
+ return links;
192
+ }
193
+ this.changes.forEach((changes) => changes.forEach((change) => {
194
+ change.issues.forEach((issue) => {
195
+ if (!links.includes(issue)) {
196
+ links.push(`[#${issue}]: ${changelog.url}/issues/${issue}`);
197
+ }
198
+ });
199
+ }));
200
+ return links;
201
+ }
202
+ }
203
+ exports.default = Release;
204
+ function formatDate(date) {
205
+ if (!date) {
206
+ return "Unreleased";
207
+ }
208
+ const year = date.getUTCFullYear();
209
+ let month = date.getUTCMonth() + 1;
210
+ let day = date.getUTCDate();
211
+ if (month < 10) {
212
+ month = "0" + month;
213
+ }
214
+ if (day < 10) {
215
+ day = "0" + day;
216
+ }
217
+ return `${year}-${month}-${day}`;
218
+ }
219
+ });
@@ -0,0 +1,18 @@
1
+ var __importDefault = (this && this.__importDefault) || function (mod) {
2
+ return (mod && mod.__esModule) ? mod : { "default": mod };
3
+ };
4
+ (function (factory) {
5
+ if (typeof module === "object" && typeof module.exports === "object") {
6
+ var v = factory(require, exports);
7
+ if (v !== undefined) module.exports = v;
8
+ }
9
+ else if (typeof define === "function" && define.amd) {
10
+ define(["require", "exports", "../deps/deno_land/x/semver_v1.4.0/mod.js"], factory);
11
+ }
12
+ })(function (require, exports) {
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.eq = exports.Semver = void 0;
15
+ var mod_js_1 = require("../deps/deno_land/x/semver_v1.4.0/mod.js");
16
+ Object.defineProperty(exports, "Semver", { enumerable: true, get: function () { return __importDefault(mod_js_1).default; } });
17
+ Object.defineProperty(exports, "eq", { enumerable: true, get: function () { return mod_js_1.eq; } });
18
+ });
@@ -0,0 +1,164 @@
1
+ var __importDefault = (this && this.__importDefault) || function (mod) {
2
+ return (mod && mod.__esModule) ? mod : { "default": mod };
3
+ };
4
+ (function (factory) {
5
+ if (typeof module === "object" && typeof module.exports === "object") {
6
+ var v = factory(require, exports);
7
+ if (v !== undefined) module.exports = v;
8
+ }
9
+ else if (typeof define === "function" && define.amd) {
10
+ define(["require", "exports", "./Changelog.js", "./Release.js"], factory);
11
+ }
12
+ })(function (require, exports) {
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ const Changelog_js_1 = __importDefault(require("./Changelog.js"));
15
+ const Release_js_1 = __importDefault(require("./Release.js"));
16
+ const defaultOptions = {
17
+ releaseCreator: (version, date, description) => new Release_js_1.default(version, date, description),
18
+ };
19
+ /** Parse a markdown string */
20
+ function parser(markdown, options) {
21
+ const opts = Object.assign({}, defaultOptions, options);
22
+ const tokens = tokenize(markdown);
23
+ try {
24
+ return processTokens(tokens, opts);
25
+ }
26
+ catch (error) {
27
+ throw new Error(`Parse error in the line ${tokens[0][0]}: ${error.message}`);
28
+ }
29
+ }
30
+ exports.default = parser;
31
+ /** Process an array of tokens to build the Changelog */
32
+ function processTokens(tokens, opts) {
33
+ const changelog = new Changelog_js_1.default("");
34
+ changelog.flag = getContent(tokens, "flag");
35
+ changelog.title = getContent(tokens, "h1", true);
36
+ changelog.description = getContent(tokens, "p");
37
+ //Releases
38
+ let release;
39
+ while ((release = getContent(tokens, "h2").toLowerCase())) {
40
+ const matches = release.match(/\[?([^\]]+)\]?\s*-\s*([\d]{4}-[\d]{1,2}-[\d]{1,2})$/);
41
+ if (matches) {
42
+ release = opts.releaseCreator(matches[1], matches[2]);
43
+ }
44
+ else if (release.includes("unreleased")) {
45
+ const matches = release.match(/\[?([^\]]+)\]?\s*-\s*unreleased$/);
46
+ release = matches
47
+ ? opts.releaseCreator(matches[1])
48
+ : opts.releaseCreator();
49
+ }
50
+ else {
51
+ throw new Error(`Syntax error in the release title`);
52
+ }
53
+ changelog.addRelease(release);
54
+ release.description = getContent(tokens, "p");
55
+ let type;
56
+ while ((type = getContent(tokens, "h3").toLowerCase())) {
57
+ let change;
58
+ while ((change = getContent(tokens, "li"))) {
59
+ release.addChange(type, change);
60
+ }
61
+ }
62
+ }
63
+ //Skip release links
64
+ let link = getContent(tokens, "link");
65
+ while (link) {
66
+ if (!changelog.url) {
67
+ const matches = link.match(/^\[.*\]\:\s*(http.*)\/compare\/.*$/);
68
+ if (matches) {
69
+ changelog.url = matches[1];
70
+ }
71
+ }
72
+ link = getContent(tokens, "link");
73
+ }
74
+ //Footer
75
+ if (getContent(tokens, "hr")) {
76
+ changelog.footer = getContent(tokens, "p");
77
+ }
78
+ if (tokens.length) {
79
+ throw new Error(`Unexpected content ${JSON.stringify(tokens)}`);
80
+ }
81
+ return changelog;
82
+ }
83
+ /** Returns the content of a token */
84
+ function getContent(tokens, type, required = false) {
85
+ if (!tokens[0] || tokens[0][1] !== type) {
86
+ if (required) {
87
+ throw new Error(`Required token missing in: "${tokens[0][0]}"`);
88
+ }
89
+ return "";
90
+ }
91
+ return tokens.shift()[2].join("\n");
92
+ }
93
+ /** Tokenize a markdown string */
94
+ function tokenize(markdown) {
95
+ const tokens = [];
96
+ markdown
97
+ .trim()
98
+ .split("\n")
99
+ .map((line, index) => {
100
+ const lineNumber = index + 1;
101
+ if (line.startsWith("---")) {
102
+ return [lineNumber, "hr", ["-"]];
103
+ }
104
+ if (line.startsWith("# ")) {
105
+ return [lineNumber, "h1", [line.substr(1).trim()]];
106
+ }
107
+ if (line.startsWith("## ")) {
108
+ return [lineNumber, "h2", [line.substr(2).trim()]];
109
+ }
110
+ if (line.startsWith("### ")) {
111
+ return [lineNumber, "h3", [line.substr(3).trim()]];
112
+ }
113
+ if (line.startsWith("-")) {
114
+ return [lineNumber, "li", [line.substr(1).trim()]];
115
+ }
116
+ if (line.startsWith("*")) {
117
+ return [lineNumber, "li", [line.substr(1).trim()]];
118
+ }
119
+ if (line.match(/^\[.*\]\:\s*http.*$/)) {
120
+ return [lineNumber, "link", [line.trim()]];
121
+ }
122
+ const result = line.match(/^<!--(.*)-->$/);
123
+ if (result) {
124
+ return [lineNumber, "flag", [result[1].trim()]];
125
+ }
126
+ return [lineNumber, "p", [line.trimEnd()]];
127
+ })
128
+ .forEach((line, index) => {
129
+ const [lineNumber, type, [content]] = line;
130
+ if (index > 0) {
131
+ const prevType = tokens[0][1];
132
+ if (type === "p") {
133
+ if (prevType === "p") {
134
+ return tokens[0][2].push(content);
135
+ }
136
+ if (prevType === "li") {
137
+ return tokens[0][2].push(content.replace(/^\s\s/, ""));
138
+ }
139
+ }
140
+ }
141
+ tokens.unshift([lineNumber, type, [content]]);
142
+ });
143
+ return tokens
144
+ .filter((token) => !isEmpty(token[2]))
145
+ .map((token) => {
146
+ const content = token[2];
147
+ while (isEmpty(content[content.length - 1])) {
148
+ content.pop();
149
+ }
150
+ while (isEmpty(content[0])) {
151
+ content.shift();
152
+ }
153
+ return token;
154
+ })
155
+ .reverse();
156
+ }
157
+ /** Check if a string or array is empty */
158
+ function isEmpty(val) {
159
+ if (Array.isArray(val)) {
160
+ val = val.join("");
161
+ }
162
+ return !val || val.trim() === "";
163
+ }
164
+ });
@@ -0,0 +1,24 @@
1
+ # Changelog - demo
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
6
+ and this project adheres to
7
+ [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
8
+
9
+ ## [Unreleased]
10
+ ### Maintenance
11
+ - upgrade
12
+ - fix vulnerabilities
13
+
14
+ ## [2.0.0] - 2020-02-07
15
+ ### Added
16
+ - custom release
17
+
18
+ ## [1.0.0] - 2020-02-06
19
+ ### Maintenance
20
+ - fix naming
21
+
22
+ [Unreleased]: https://github.com/olivierlacan/keep-a-changelog/compare/v2.0.0...HEAD
23
+ [2.0.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.0.0...v2.0.0
24
+ [1.0.0]: https://github.com/olivierlacan/keep-a-changelog/releases/tag/v1.0.0