marked 5.1.2 → 7.0.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.
@@ -1,44 +1,50 @@
1
- import { Renderer } from './Renderer.js';
2
- import { TextRenderer } from './TextRenderer.js';
3
- import { Slugger } from './Slugger.js';
4
- import { defaults } from './defaults.js';
1
+ import { _Renderer } from './Renderer.ts';
2
+ import { _TextRenderer } from './TextRenderer.ts';
3
+ import { _Slugger } from './Slugger.ts';
4
+ import { _defaults } from './defaults.ts';
5
5
  import {
6
6
  unescape
7
- } from './helpers.js';
7
+ } from './helpers.ts';
8
+ import type { Token, Tokens } from './Tokens.ts';
9
+ import type { MarkedOptions } from './MarkedOptions.ts';
8
10
 
9
11
  /**
10
12
  * Parsing & Compiling
11
13
  */
12
- export class Parser {
13
- constructor(options) {
14
- this.options = options || defaults;
15
- this.options.renderer = this.options.renderer || new Renderer();
14
+ export class _Parser {
15
+ options: MarkedOptions;
16
+ renderer: _Renderer;
17
+ textRenderer: _TextRenderer;
18
+ slugger: _Slugger;
19
+ constructor(options?: MarkedOptions) {
20
+ this.options = options || _defaults;
21
+ this.options.renderer = this.options.renderer || new _Renderer();
16
22
  this.renderer = this.options.renderer;
17
23
  this.renderer.options = this.options;
18
- this.textRenderer = new TextRenderer();
19
- this.slugger = new Slugger();
24
+ this.textRenderer = new _TextRenderer();
25
+ this.slugger = new _Slugger();
20
26
  }
21
27
 
22
28
  /**
23
29
  * Static Parse Method
24
30
  */
25
- static parse(tokens, options) {
26
- const parser = new Parser(options);
31
+ static parse(tokens: Token[], options?: MarkedOptions) {
32
+ const parser = new _Parser(options);
27
33
  return parser.parse(tokens);
28
34
  }
29
35
 
30
36
  /**
31
37
  * Static Parse Inline Method
32
38
  */
33
- static parseInline(tokens, options) {
34
- const parser = new Parser(options);
39
+ static parseInline(tokens: Token[], options?: MarkedOptions) {
40
+ const parser = new _Parser(options);
35
41
  return parser.parseInline(tokens);
36
42
  }
37
43
 
38
44
  /**
39
45
  * Parse Loop
40
46
  */
41
- parse(tokens, top = true) {
47
+ parse(tokens: Token[], top = true): string {
42
48
  let out = '',
43
49
  i,
44
50
  j,
@@ -83,16 +89,16 @@ export class Parser {
83
89
  }
84
90
  case 'heading': {
85
91
  out += this.renderer.heading(
86
- this.parseInline(token.tokens),
92
+ this.parseInline(token.tokens) as string,
87
93
  token.depth,
88
- unescape(this.parseInline(token.tokens, this.textRenderer)),
94
+ unescape(this.parseInline(token.tokens, this.textRenderer) as string),
89
95
  this.slugger);
90
96
  continue;
91
97
  }
92
98
  case 'code': {
93
99
  out += this.renderer.code(token.text,
94
100
  token.lang,
95
- token.escaped);
101
+ !!token.escaped);
96
102
  continue;
97
103
  }
98
104
  case 'table': {
@@ -103,7 +109,7 @@ export class Parser {
103
109
  l2 = token.header.length;
104
110
  for (j = 0; j < l2; j++) {
105
111
  cell += this.renderer.tablecell(
106
- this.parseInline(token.header[j].tokens),
112
+ this.parseInline(token.header[j].tokens)!,
107
113
  { header: true, align: token.align[j] }
108
114
  );
109
115
  }
@@ -118,7 +124,7 @@ export class Parser {
118
124
  l3 = row.length;
119
125
  for (k = 0; k < l3; k++) {
120
126
  cell += this.renderer.tablecell(
121
- this.parseInline(row[k].tokens),
127
+ this.parseInline(row[k].tokens)!,
122
128
  { header: false, align: token.align[k] }
123
129
  );
124
130
  }
@@ -129,7 +135,7 @@ export class Parser {
129
135
  continue;
130
136
  }
131
137
  case 'blockquote': {
132
- body = this.parse(token.tokens);
138
+ body = this.parse(token.tokens)!;
133
139
  out += this.renderer.blockquote(body);
134
140
  continue;
135
141
  }
@@ -147,7 +153,7 @@ export class Parser {
147
153
 
148
154
  itemBody = '';
149
155
  if (item.task) {
150
- checkbox = this.renderer.checkbox(checked);
156
+ checkbox = this.renderer.checkbox(!!checked);
151
157
  if (loose) {
152
158
  if (item.tokens.length > 0 && item.tokens[0].type === 'paragraph') {
153
159
  item.tokens[0].text = checkbox + ' ' + item.tokens[0].text;
@@ -158,7 +164,7 @@ export class Parser {
158
164
  item.tokens.unshift({
159
165
  type: 'text',
160
166
  text: checkbox
161
- });
167
+ } as Tokens.Text);
162
168
  }
163
169
  } else {
164
170
  itemBody += checkbox;
@@ -166,7 +172,7 @@ export class Parser {
166
172
  }
167
173
 
168
174
  itemBody += this.parse(item.tokens, loose);
169
- body += this.renderer.listitem(itemBody, task, checked);
175
+ body += this.renderer.listitem(itemBody, task, !!checked);
170
176
  }
171
177
 
172
178
  out += this.renderer.list(body, ordered, start);
@@ -177,7 +183,7 @@ export class Parser {
177
183
  continue;
178
184
  }
179
185
  case 'paragraph': {
180
- out += this.renderer.paragraph(this.parseInline(token.tokens));
186
+ out += this.renderer.paragraph(this.parseInline(token.tokens)!);
181
187
  continue;
182
188
  }
183
189
  case 'text': {
@@ -186,7 +192,7 @@ export class Parser {
186
192
  token = tokens[++i];
187
193
  body += '\n' + (token.tokens ? this.parseInline(token.tokens) : token.text);
188
194
  }
189
- out += top ? this.renderer.paragraph(body) : body;
195
+ out += top ? this.renderer.paragraph(body!) : body;
190
196
  continue;
191
197
  }
192
198
 
@@ -194,7 +200,7 @@ export class Parser {
194
200
  const errMsg = 'Token with "' + token.type + '" type was not found.';
195
201
  if (this.options.silent) {
196
202
  console.error(errMsg);
197
- return;
203
+ return '';
198
204
  } else {
199
205
  throw new Error(errMsg);
200
206
  }
@@ -208,7 +214,7 @@ export class Parser {
208
214
  /**
209
215
  * Parse Inline Tokens
210
216
  */
211
- parseInline(tokens, renderer) {
217
+ parseInline(tokens: Token[], renderer?: _Renderer | _TextRenderer): string {
212
218
  renderer = renderer || this.renderer;
213
219
  let out = '',
214
220
  i,
@@ -238,7 +244,7 @@ export class Parser {
238
244
  break;
239
245
  }
240
246
  case 'link': {
241
- out += renderer.link(token.href, token.title, this.parseInline(token.tokens, renderer));
247
+ out += renderer.link(token.href, token.title, this.parseInline(token.tokens, renderer)!);
242
248
  break;
243
249
  }
244
250
  case 'image': {
@@ -246,11 +252,11 @@ export class Parser {
246
252
  break;
247
253
  }
248
254
  case 'strong': {
249
- out += renderer.strong(this.parseInline(token.tokens, renderer));
255
+ out += renderer.strong(this.parseInline(token.tokens, renderer)!);
250
256
  break;
251
257
  }
252
258
  case 'em': {
253
- out += renderer.em(this.parseInline(token.tokens, renderer));
259
+ out += renderer.em(this.parseInline(token.tokens, renderer)!);
254
260
  break;
255
261
  }
256
262
  case 'codespan': {
@@ -262,7 +268,7 @@ export class Parser {
262
268
  break;
263
269
  }
264
270
  case 'del': {
265
- out += renderer.del(this.parseInline(token.tokens, renderer));
271
+ out += renderer.del(this.parseInline(token.tokens, renderer)!);
266
272
  break;
267
273
  }
268
274
  case 'text': {
@@ -273,7 +279,7 @@ export class Parser {
273
279
  const errMsg = 'Token with "' + token.type + '" type was not found.';
274
280
  if (this.options.silent) {
275
281
  console.error(errMsg);
276
- return;
282
+ return '';
277
283
  } else {
278
284
  throw new Error(errMsg);
279
285
  }
@@ -1,19 +1,22 @@
1
- import { defaults } from './defaults.js';
1
+ import { _defaults } from './defaults.ts';
2
2
  import {
3
3
  cleanUrl,
4
4
  escape
5
- } from './helpers.js';
5
+ } from './helpers.ts';
6
+ import type { MarkedOptions } from './MarkedOptions.ts';
7
+ import { Slugger } from './marked.ts';
6
8
 
7
9
  /**
8
10
  * Renderer
9
11
  */
10
- export class Renderer {
11
- constructor(options) {
12
- this.options = options || defaults;
12
+ export class _Renderer {
13
+ options: MarkedOptions;
14
+ constructor(options?: MarkedOptions) {
15
+ this.options = options || _defaults;
13
16
  }
14
17
 
15
- code(code, infostring, escaped) {
16
- const lang = (infostring || '').match(/\S*/)[0];
18
+ code(code: string, infostring: string | undefined, escaped: boolean): string {
19
+ const lang = (infostring || '').match(/\S*/)![0];
17
20
  if (this.options.highlight) {
18
21
  const out = this.options.highlight(code, lang);
19
22
  if (out != null && out !== code) {
@@ -38,24 +41,15 @@ export class Renderer {
38
41
  + '</code></pre>\n';
39
42
  }
40
43
 
41
- /**
42
- * @param {string} quote
43
- */
44
- blockquote(quote) {
44
+ blockquote(quote: string): string {
45
45
  return `<blockquote>\n${quote}</blockquote>\n`;
46
46
  }
47
47
 
48
- html(html, block) {
48
+ html(html: string, block?: boolean) : string {
49
49
  return html;
50
50
  }
51
51
 
52
- /**
53
- * @param {string} text
54
- * @param {string} level
55
- * @param {string} raw
56
- * @param {any} slugger
57
- */
58
- heading(text, level, raw, slugger) {
52
+ heading(text: string, level: number, raw: string, slugger: Slugger): string {
59
53
  if (this.options.headerIds) {
60
54
  const id = this.options.headerPrefix + slugger.slug(raw);
61
55
  return `<h${level} id="${id}">${text}</h${level}>\n`;
@@ -65,24 +59,21 @@ export class Renderer {
65
59
  return `<h${level}>${text}</h${level}>\n`;
66
60
  }
67
61
 
68
- hr() {
62
+ hr(): string {
69
63
  return this.options.xhtml ? '<hr/>\n' : '<hr>\n';
70
64
  }
71
65
 
72
- list(body, ordered, start) {
66
+ list(body: string, ordered: boolean, start: number | ''): string {
73
67
  const type = ordered ? 'ol' : 'ul',
74
68
  startatt = (ordered && start !== 1) ? (' start="' + start + '"') : '';
75
69
  return '<' + type + startatt + '>\n' + body + '</' + type + '>\n';
76
70
  }
77
71
 
78
- /**
79
- * @param {string} text
80
- */
81
- listitem(text) {
72
+ listitem(text: string, task: boolean, checked: boolean): string {
82
73
  return `<li>${text}</li>\n`;
83
74
  }
84
75
 
85
- checkbox(checked) {
76
+ checkbox(checked: boolean): string {
86
77
  return '<input '
87
78
  + (checked ? 'checked="" ' : '')
88
79
  + 'disabled="" type="checkbox"'
@@ -90,18 +81,11 @@ export class Renderer {
90
81
  + '> ';
91
82
  }
92
83
 
93
- /**
94
- * @param {string} text
95
- */
96
- paragraph(text) {
84
+ paragraph(text: string): string {
97
85
  return `<p>${text}</p>\n`;
98
86
  }
99
87
 
100
- /**
101
- * @param {string} header
102
- * @param {string} body
103
- */
104
- table(header, body) {
88
+ table(header: string, body: string): string {
105
89
  if (body) body = `<tbody>${body}</tbody>`;
106
90
 
107
91
  return '<table>\n'
@@ -112,14 +96,14 @@ export class Renderer {
112
96
  + '</table>\n';
113
97
  }
114
98
 
115
- /**
116
- * @param {string} content
117
- */
118
- tablerow(content) {
99
+ tablerow(content: string): string {
119
100
  return `<tr>\n${content}</tr>\n`;
120
101
  }
121
102
 
122
- tablecell(content, flags) {
103
+ tablecell(content: string, flags: {
104
+ header: boolean;
105
+ align: 'center' | 'left' | 'right' | null;
106
+ }): string {
123
107
  const type = flags.header ? 'th' : 'td';
124
108
  const tag = flags.align
125
109
  ? `<${type} align="${flags.align}">`
@@ -129,44 +113,29 @@ export class Renderer {
129
113
 
130
114
  /**
131
115
  * span level renderer
132
- * @param {string} text
133
116
  */
134
- strong(text) {
117
+ strong(text: string): string {
135
118
  return `<strong>${text}</strong>`;
136
119
  }
137
120
 
138
- /**
139
- * @param {string} text
140
- */
141
- em(text) {
121
+ em(text: string): string {
142
122
  return `<em>${text}</em>`;
143
123
  }
144
124
 
145
- /**
146
- * @param {string} text
147
- */
148
- codespan(text) {
125
+ codespan(text: string): string {
149
126
  return `<code>${text}</code>`;
150
127
  }
151
128
 
152
- br() {
129
+ br(): string {
153
130
  return this.options.xhtml ? '<br/>' : '<br>';
154
131
  }
155
132
 
156
- /**
157
- * @param {string} text
158
- */
159
- del(text) {
133
+ del(text: string): string {
160
134
  return `<del>${text}</del>`;
161
135
  }
162
136
 
163
- /**
164
- * @param {string} href
165
- * @param {string} title
166
- * @param {string} text
167
- */
168
- link(href, title, text) {
169
- href = cleanUrl(this.options.sanitize, this.options.baseUrl, href);
137
+ link(href: string, title: string | null | undefined, text: string): string {
138
+ href = cleanUrl(this.options.sanitize, this.options.baseUrl, href) as any;
170
139
  if (href === null) {
171
140
  return text;
172
141
  }
@@ -178,13 +147,8 @@ export class Renderer {
178
147
  return out;
179
148
  }
180
149
 
181
- /**
182
- * @param {string} href
183
- * @param {string} title
184
- * @param {string} text
185
- */
186
- image(href, title, text) {
187
- href = cleanUrl(this.options.sanitize, this.options.baseUrl, href);
150
+ image(href: string, title: string | null, text: string): string {
151
+ href = cleanUrl(this.options.sanitize, this.options.baseUrl, href) as any;
188
152
  if (href === null) {
189
153
  return text;
190
154
  }
@@ -197,7 +161,7 @@ export class Renderer {
197
161
  return out;
198
162
  }
199
163
 
200
- text(text) {
164
+ text(text: string) : string {
201
165
  return text;
202
166
  }
203
167
  }
@@ -1,15 +1,16 @@
1
+ import type { SluggerOptions } from './MarkedOptions.ts';
2
+
1
3
  /**
2
4
  * Slugger generates header id
3
5
  */
4
- export class Slugger {
6
+ export class _Slugger {
7
+ seen: { [slugValue: string]: number };
8
+
5
9
  constructor() {
6
10
  this.seen = {};
7
11
  }
8
12
 
9
- /**
10
- * @param {string} value
11
- */
12
- serialize(value) {
13
+ serialize(value: string) {
13
14
  return value
14
15
  .toLowerCase()
15
16
  .trim()
@@ -22,10 +23,8 @@ export class Slugger {
22
23
 
23
24
  /**
24
25
  * Finds the next safe (unique) slug to use
25
- * @param {string} originalSlug
26
- * @param {boolean} isDryRun
27
26
  */
28
- getNextSafeSlug(originalSlug, isDryRun) {
27
+ getNextSafeSlug(originalSlug: string, isDryRun: boolean | undefined) {
29
28
  let slug = originalSlug;
30
29
  let occurenceAccumulator = 0;
31
30
  if (this.seen.hasOwnProperty(slug)) {
@@ -44,11 +43,8 @@ export class Slugger {
44
43
 
45
44
  /**
46
45
  * Convert string to unique id
47
- * @param {object} [options]
48
- * @param {boolean} [options.dryrun] Generates the next unique slug without
49
- * updating the internal accumulator.
50
46
  */
51
- slug(value, options = {}) {
47
+ slug(value: string, options: SluggerOptions = {}) {
52
48
  const slug = this.serialize(value);
53
49
  return this.getNextSafeSlug(slug, options.dryrun);
54
50
  }
@@ -2,37 +2,37 @@
2
2
  * TextRenderer
3
3
  * returns only the textual part of the token
4
4
  */
5
- export class TextRenderer {
5
+ export class _TextRenderer {
6
6
  // no need for block level renderers
7
- strong(text) {
7
+ strong(text: string) {
8
8
  return text;
9
9
  }
10
10
 
11
- em(text) {
11
+ em(text: string) {
12
12
  return text;
13
13
  }
14
14
 
15
- codespan(text) {
15
+ codespan(text: string) {
16
16
  return text;
17
17
  }
18
18
 
19
- del(text) {
19
+ del(text: string) {
20
20
  return text;
21
21
  }
22
22
 
23
- html(text) {
23
+ html(text: string) {
24
24
  return text;
25
25
  }
26
26
 
27
- text(text) {
27
+ text(text: string) {
28
28
  return text;
29
29
  }
30
30
 
31
- link(href, title, text) {
31
+ link(href: string, title: string | null | undefined, text: string) {
32
32
  return '' + text;
33
33
  }
34
34
 
35
- image(href, title, text) {
35
+ image(href: string, title: string | null, text: string) {
36
36
  return '' + text;
37
37
  }
38
38