Skip to content

Commit 1f579f7

Browse files
authoredNov 18, 2024··
fix: Remove unused plus typescript tightening (#3527)
* chore: remove unused build file * chore: remove unused package * chore: remove unused function * chore: remove unnecessary | undefineds * core: replace unnecessary &&s with optional chaining * chore: use .at(-x) instead of .length - x property access gives stricter TS typing, is more concise * chore: tighten TS types * chore: sort tokens alphabetically * fix: typeof plus !== null check * chore: type test for .parse, .use * fix: if check
1 parent a46c0d8 commit 1f579f7

17 files changed

+223
-232
lines changed
 

‎bower.json

-23
This file was deleted.

‎docs/demo/demo.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ function setOptions(opts) {
185185
$optionsElem.value = JSON.stringify(
186186
opts,
187187
(key, value) => {
188-
if (value && typeof value === 'object' && Object.getPrototypeOf(value) !== Object.prototype) {
188+
if (value !== null && typeof value === 'object' && Object.getPrototypeOf(value) !== Object.prototype) {
189189
return undefined;
190190
}
191191
return value;

‎package-lock.json

-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
"rollup": "^4.25.0",
7777
"semantic-release": "^24.2.0",
7878
"titleize": "^4.0.0",
79-
"ts-expect": "^1.3.0",
8079
"tslib": "^2.8.1",
8180
"typescript": "5.6.3"
8281
},

‎src/Hooks.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { Token, TokensList } from './Tokens.ts';
66

77
export class _Hooks {
88
options: MarkedOptions;
9-
block: boolean | undefined;
9+
block?: boolean;
1010

1111
constructor(options?: MarkedOptions) {
1212
this.options = options || _defaults;

‎src/Instance.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,11 @@ export class Marked {
265265
type overloadedParse = {
266266
(src: string, options: MarkedOptions & { async: true }): Promise<string>;
267267
(src: string, options: MarkedOptions & { async: false }): string;
268-
(src: string, options?: MarkedOptions | undefined | null): string | Promise<string>;
268+
(src: string, options?: MarkedOptions | null): string | Promise<string>;
269269
};
270270

271271
// eslint-disable-next-line @typescript-eslint/no-explicit-any
272-
const parse: overloadedParse = (src: string, options?: MarkedOptions | undefined | null): any => {
272+
const parse: overloadedParse = (src: string, options?: MarkedOptions | null): any => {
273273
const origOpt = { ...options };
274274
const opt = { ...this.defaults, ...origOpt };
275275

‎src/Lexer.ts

+57-57
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { _Tokenizer } from './Tokenizer.ts';
22
import { _defaults } from './defaults.ts';
33
import { other, block, inline } from './rules.ts';
44
import type { Token, TokensList, Tokens } from './Tokens.ts';
5-
import type { MarkedOptions, TokenizerExtension } from './MarkedOptions.ts';
5+
import type { MarkedOptions } from './MarkedOptions.ts';
66

77
/**
88
* Block Lexer
@@ -85,8 +85,7 @@ export class _Lexer {
8585
* Preprocessing
8686
*/
8787
lex(src: string) {
88-
src = src
89-
.replace(other.carriageReturn, '\n');
88+
src = src.replace(other.carriageReturn, '\n');
9089

9190
this.blockTokens(src, this.tokens);
9291

@@ -109,31 +108,28 @@ export class _Lexer {
109108
src = src.replace(other.tabCharGlobal, ' ').replace(other.spaceLine, '');
110109
}
111110

112-
let token: Tokens.Generic | undefined;
113-
let lastToken;
114-
let cutSrc;
115-
116111
while (src) {
117-
if (this.options.extensions
118-
&& this.options.extensions.block
119-
&& this.options.extensions.block.some((extTokenizer: TokenizerExtension['tokenizer']) => {
120-
if (token = extTokenizer.call({ lexer: this }, src, tokens)) {
121-
src = src.substring(token.raw.length);
122-
tokens.push(token);
123-
return true;
124-
}
125-
return false;
126-
})) {
112+
let token: Tokens.Generic | undefined;
113+
114+
if (this.options.extensions?.block?.some((extTokenizer) => {
115+
if (token = extTokenizer.call({ lexer: this }, src, tokens)) {
116+
src = src.substring(token.raw.length);
117+
tokens.push(token);
118+
return true;
119+
}
120+
return false;
121+
})) {
127122
continue;
128123
}
129124

130125
// newline
131126
if (token = this.tokenizer.space(src)) {
132127
src = src.substring(token.raw.length);
133-
if (token.raw.length === 1 && tokens.length > 0) {
128+
const lastToken = tokens.at(-1);
129+
if (token.raw.length === 1 && lastToken !== undefined) {
134130
// if there's a single \n as a spacer, it's terminating the last line,
135131
// so move it there so that we don't get unnecessary paragraph tags
136-
tokens[tokens.length - 1].raw += '\n';
132+
lastToken.raw += '\n';
137133
} else {
138134
tokens.push(token);
139135
}
@@ -143,12 +139,12 @@ export class _Lexer {
143139
// code
144140
if (token = this.tokenizer.code(src)) {
145141
src = src.substring(token.raw.length);
146-
lastToken = tokens[tokens.length - 1];
142+
const lastToken = tokens.at(-1);
147143
// An indented code block cannot interrupt a paragraph.
148-
if (lastToken && (lastToken.type === 'paragraph' || lastToken.type === 'text')) {
144+
if (lastToken?.type === 'paragraph' || lastToken?.type === 'text') {
149145
lastToken.raw += '\n' + token.raw;
150146
lastToken.text += '\n' + token.text;
151-
this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
147+
this.inlineQueue.at(-1)!.src = lastToken.text;
152148
} else {
153149
tokens.push(token);
154150
}
@@ -200,11 +196,11 @@ export class _Lexer {
200196
// def
201197
if (token = this.tokenizer.def(src)) {
202198
src = src.substring(token.raw.length);
203-
lastToken = tokens[tokens.length - 1];
204-
if (lastToken && (lastToken.type === 'paragraph' || lastToken.type === 'text')) {
199+
const lastToken = tokens.at(-1);
200+
if (lastToken?.type === 'paragraph' || lastToken?.type === 'text') {
205201
lastToken.raw += '\n' + token.raw;
206202
lastToken.text += '\n' + token.raw;
207-
this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
203+
this.inlineQueue.at(-1)!.src = lastToken.text;
208204
} else if (!this.tokens.links[token.tag]) {
209205
this.tokens.links[token.tag] = {
210206
href: token.href,
@@ -230,43 +226,45 @@ export class _Lexer {
230226

231227
// top-level paragraph
232228
// prevent paragraph consuming extensions by clipping 'src' to extension start
233-
cutSrc = src;
234-
if (this.options.extensions && this.options.extensions.startBlock) {
229+
let cutSrc = src;
230+
if (this.options.extensions?.startBlock) {
235231
let startIndex = Infinity;
236232
const tempSrc = src.slice(1);
237233
let tempStart;
238234
this.options.extensions.startBlock.forEach((getStartIndex) => {
239235
tempStart = getStartIndex.call({ lexer: this }, tempSrc);
240-
if (typeof tempStart === 'number' && tempStart >= 0) { startIndex = Math.min(startIndex, tempStart); }
236+
if (typeof tempStart === 'number' && tempStart >= 0) {
237+
startIndex = Math.min(startIndex, tempStart);
238+
}
241239
});
242240
if (startIndex < Infinity && startIndex >= 0) {
243241
cutSrc = src.substring(0, startIndex + 1);
244242
}
245243
}
246244
if (this.state.top && (token = this.tokenizer.paragraph(cutSrc))) {
247-
lastToken = tokens[tokens.length - 1];
245+
const lastToken = tokens.at(-1);
248246
if (lastParagraphClipped && lastToken?.type === 'paragraph') {
249247
lastToken.raw += '\n' + token.raw;
250248
lastToken.text += '\n' + token.text;
251249
this.inlineQueue.pop();
252-
this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
250+
this.inlineQueue.at(-1)!.src = lastToken.text;
253251
} else {
254252
tokens.push(token);
255253
}
256-
lastParagraphClipped = (cutSrc.length !== src.length);
254+
lastParagraphClipped = cutSrc.length !== src.length;
257255
src = src.substring(token.raw.length);
258256
continue;
259257
}
260258

261259
// text
262260
if (token = this.tokenizer.text(src)) {
263261
src = src.substring(token.raw.length);
264-
lastToken = tokens[tokens.length - 1];
265-
if (lastToken && lastToken.type === 'text') {
262+
const lastToken = tokens.at(-1);
263+
if (lastToken?.type === 'text') {
266264
lastToken.raw += '\n' + token.raw;
267265
lastToken.text += '\n' + token.text;
268266
this.inlineQueue.pop();
269-
this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
267+
this.inlineQueue.at(-1)!.src = lastToken.text;
270268
} else {
271269
tokens.push(token);
272270
}
@@ -297,20 +295,19 @@ export class _Lexer {
297295
* Lexing/Compiling
298296
*/
299297
inlineTokens(src: string, tokens: Token[] = []): Token[] {
300-
let token, lastToken, cutSrc;
301-
302298
// String with links masked to avoid interference with em and strong
303299
let maskedSrc = src;
304-
let match;
305-
let keepPrevChar, prevChar;
300+
let match: RegExpExecArray | null = null;
306301

307302
// Mask out reflinks
308303
if (this.tokens.links) {
309304
const links = Object.keys(this.tokens.links);
310305
if (links.length > 0) {
311306
while ((match = this.tokenizer.rules.inline.reflinkSearch.exec(maskedSrc)) != null) {
312307
if (links.includes(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
313-
maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex);
308+
maskedSrc = maskedSrc.slice(0, match.index)
309+
+ '[' + 'a'.repeat(match[0].length - 2) + ']'
310+
+ maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex);
314311
}
315312
}
316313
}
@@ -325,23 +322,25 @@ export class _Lexer {
325322
maskedSrc = maskedSrc.slice(0, match.index) + '++' + maskedSrc.slice(this.tokenizer.rules.inline.anyPunctuation.lastIndex);
326323
}
327324

325+
let keepPrevChar = false;
326+
let prevChar = '';
328327
while (src) {
329328
if (!keepPrevChar) {
330329
prevChar = '';
331330
}
332331
keepPrevChar = false;
333332

333+
let token: Tokens.Generic | undefined;
334+
334335
// extensions
335-
if (this.options.extensions
336-
&& this.options.extensions.inline
337-
&& this.options.extensions.inline.some((extTokenizer) => {
338-
if (token = extTokenizer.call({ lexer: this }, src, tokens)) {
339-
src = src.substring(token.raw.length);
340-
tokens.push(token);
341-
return true;
342-
}
343-
return false;
344-
})) {
336+
if (this.options.extensions?.inline?.some((extTokenizer) => {
337+
if (token = extTokenizer.call({ lexer: this }, src, tokens)) {
338+
src = src.substring(token.raw.length);
339+
tokens.push(token);
340+
return true;
341+
}
342+
return false;
343+
})) {
345344
continue;
346345
}
347346

@@ -355,7 +354,6 @@ export class _Lexer {
355354
// tag
356355
if (token = this.tokenizer.tag(src)) {
357356
src = src.substring(token.raw.length);
358-
lastToken = tokens[tokens.length - 1];
359357
tokens.push(token);
360358
continue;
361359
}
@@ -370,8 +368,8 @@ export class _Lexer {
370368
// reflink, nolink
371369
if (token = this.tokenizer.reflink(src, this.tokens.links)) {
372370
src = src.substring(token.raw.length);
373-
lastToken = tokens[tokens.length - 1];
374-
if (lastToken && token.type === 'text' && lastToken.type === 'text') {
371+
const lastToken = tokens.at(-1);
372+
if (token.type === 'text' && lastToken?.type === 'text') {
375373
lastToken.raw += token.raw;
376374
lastToken.text += token.text;
377375
} else {
@@ -424,14 +422,16 @@ export class _Lexer {
424422

425423
// text
426424
// prevent inlineText consuming extensions by clipping 'src' to extension start
427-
cutSrc = src;
428-
if (this.options.extensions && this.options.extensions.startInline) {
425+
let cutSrc = src;
426+
if (this.options.extensions?.startInline) {
429427
let startIndex = Infinity;
430428
const tempSrc = src.slice(1);
431429
let tempStart;
432430
this.options.extensions.startInline.forEach((getStartIndex) => {
433431
tempStart = getStartIndex.call({ lexer: this }, tempSrc);
434-
if (typeof tempStart === 'number' && tempStart >= 0) { startIndex = Math.min(startIndex, tempStart); }
432+
if (typeof tempStart === 'number' && tempStart >= 0) {
433+
startIndex = Math.min(startIndex, tempStart);
434+
}
435435
});
436436
if (startIndex < Infinity && startIndex >= 0) {
437437
cutSrc = src.substring(0, startIndex + 1);
@@ -443,8 +443,8 @@ export class _Lexer {
443443
prevChar = token.raw.slice(-1);
444444
}
445445
keepPrevChar = true;
446-
lastToken = tokens[tokens.length - 1];
447-
if (lastToken && lastToken.type === 'text') {
446+
const lastToken = tokens.at(-1);
447+
if (lastToken?.type === 'text') {
448448
lastToken.raw += token.raw;
449449
lastToken.text += token.text;
450450
} else {

‎src/MarkedOptions.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ export type TokenizerStartFunction = (this: TokenizerThis, src: string) => numbe
1616
export interface TokenizerExtension {
1717
name: string;
1818
level: 'block' | 'inline';
19-
start?: TokenizerStartFunction | undefined;
19+
start?: TokenizerStartFunction;
2020
tokenizer: TokenizerExtensionFunction;
21-
childTokens?: string[] | undefined;
21+
childTokens?: string[];
2222
}
2323

2424
export interface RendererThis {
@@ -58,19 +58,19 @@ export interface MarkedExtension {
5858
/**
5959
* Enable GFM line breaks. This option requires the gfm option to be true.
6060
*/
61-
breaks?: boolean | undefined;
61+
breaks?: boolean;
6262

6363
/**
6464
* Add tokenizers and renderers to marked
6565
*/
6666
extensions?:
6767
| TokenizerAndRendererExtension[]
68-
| undefined | null;
68+
| null;
6969

7070
/**
7171
* Enable GitHub flavored markdown.
7272
*/
73-
gfm?: boolean | undefined;
73+
gfm?: boolean;
7474

7575
/**
7676
* Hooks are methods that hook into some part of marked.
@@ -80,56 +80,56 @@ export interface MarkedExtension {
8080
* provideLexer is called to provide a function to tokenize markdown.
8181
* provideParser is called to provide a function to parse tokens.
8282
*/
83-
hooks?: HooksObject | undefined | null;
83+
hooks?: HooksObject | null;
8484

8585
/**
8686
* Conform to obscure parts of markdown.pl as much as possible. Don't fix any of the original markdown bugs or poor behavior.
8787
*/
88-
pedantic?: boolean | undefined;
88+
pedantic?: boolean;
8989

9090
/**
9191
* Type: object Default: new Renderer()
9292
*
9393
* An object containing functions to render tokens to HTML.
9494
*/
95-
renderer?: RendererObject | undefined | null;
95+
renderer?: RendererObject | null;
9696

9797
/**
9898
* Shows an HTML error message when rendering fails.
9999
*/
100-
silent?: boolean | undefined;
100+
silent?: boolean;
101101

102102
/**
103103
* The tokenizer defines how to turn markdown text into tokens.
104104
*/
105-
tokenizer?: TokenizerObject | undefined | null;
105+
tokenizer?: TokenizerObject | null;
106106

107107
/**
108108
* The walkTokens function gets called with every token.
109109
* Child tokens are called before moving on to sibling tokens.
110110
* Each token is passed by reference so updates are persisted when passed to the parser.
111111
* The return value of the function is ignored.
112112
*/
113-
walkTokens?: ((token: Token) => void | Promise<void>) | undefined | null;
113+
walkTokens?: ((token: Token) => void | Promise<void>) | null;
114114
}
115115

116116
export interface MarkedOptions extends Omit<MarkedExtension, 'hooks' | 'renderer' | 'tokenizer' | 'extensions' | 'walkTokens'> {
117117
/**
118118
* Hooks are methods that hook into some part of marked.
119119
*/
120-
hooks?: _Hooks | undefined | null;
120+
hooks?: _Hooks | null;
121121

122122
/**
123123
* Type: object Default: new Renderer()
124124
*
125125
* An object containing functions to render tokens to HTML.
126126
*/
127-
renderer?: _Renderer | undefined | null;
127+
renderer?: _Renderer | null;
128128

129129
/**
130130
* The tokenizer defines how to turn markdown text into tokens.
131131
*/
132-
tokenizer?: _Tokenizer | undefined | null;
132+
tokenizer?: _Tokenizer | null;
133133

134134
/**
135135
* Custom extensions

‎src/Parser.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class _Parser {
4646
const anyToken = tokens[i];
4747

4848
// Run any renderer extensions
49-
if (this.options.extensions && this.options.extensions.renderers && this.options.extensions.renderers[anyToken.type]) {
49+
if (this.options.extensions?.renderers?.[anyToken.type]) {
5050
const genericToken = anyToken as Tokens.Generic;
5151
const ret = this.options.extensions.renderers[genericToken.type].call({ parser: this }, genericToken);
5252
if (ret !== false || !['space', 'hr', 'heading', 'code', 'table', 'blockquote', 'list', 'html', 'paragraph', 'text'].includes(genericToken.type)) {
@@ -132,15 +132,14 @@ export class _Parser {
132132
/**
133133
* Parse Inline Tokens
134134
*/
135-
parseInline(tokens: Token[], renderer?: _Renderer | _TextRenderer): string {
136-
renderer = renderer || this.renderer;
135+
parseInline(tokens: Token[], renderer: _Renderer | _TextRenderer = this.renderer): string {
137136
let out = '';
138137

139138
for (let i = 0; i < tokens.length; i++) {
140139
const anyToken = tokens[i];
141140

142141
// Run any renderer extensions
143-
if (this.options.extensions && this.options.extensions.renderers && this.options.extensions.renderers[anyToken.type]) {
142+
if (this.options.extensions?.renderers?.[anyToken.type]) {
144143
const ret = this.options.extensions.renderers[anyToken.type].call({ parser: this }, anyToken);
145144
if (ret !== false || !['escape', 'html', 'link', 'image', 'strong', 'em', 'codespan', 'br', 'del', 'text'].includes(anyToken.type)) {
146145
out += ret || '';

‎src/Renderer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class _Renderer {
7777
if (item.task) {
7878
const checkbox = this.checkbox({ checked: !!item.checked });
7979
if (item.loose) {
80-
if (item.tokens.length > 0 && item.tokens[0].type === 'paragraph') {
80+
if (item.tokens[0]?.type === 'paragraph') {
8181
item.tokens[0].text = checkbox + ' ' + item.tokens[0].text;
8282
if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') {
8383
item.tokens[0].tokens[0].text = checkbox + ' ' + escape(item.tokens[0].tokens[0].text);

‎src/Tokenizer.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export class _Tokenizer {
198198
break;
199199
}
200200

201-
const lastToken = tokens[tokens.length - 1];
201+
const lastToken = tokens.at(-1);
202202

203203
if (lastToken?.type === 'code') {
204204
// blockquote continuation cannot be preceded by a code block
@@ -222,7 +222,7 @@ export class _Tokenizer {
222222

223223
raw = raw.substring(0, raw.length - lastToken.raw.length) + newToken.raw;
224224
text = text.substring(0, text.length - oldToken.raw.length) + newToken.raw;
225-
lines = newText.substring(tokens[tokens.length - 1].raw.length).split('\n');
225+
lines = newText.substring(tokens.at(-1)!.raw.length).split('\n');
226226
continue;
227227
}
228228
}
@@ -414,8 +414,11 @@ export class _Tokenizer {
414414
}
415415

416416
// Do not consume newlines at end of final item. Alternatively, make itemRegex *start* with any newlines to simplify/speed up endsWithBlankLine logic
417-
list.items[list.items.length - 1].raw = list.items[list.items.length - 1].raw.trimEnd();
418-
list.items[list.items.length - 1].text = list.items[list.items.length - 1].text.trimEnd();
417+
const lastItem = list.items.at(-1);
418+
if (lastItem) {
419+
lastItem.raw = lastItem.raw.trimEnd();
420+
lastItem.text = lastItem.text.trimEnd();
421+
}
419422
list.raw = list.raw.trimEnd();
420423

421424
// Item child tokens handled here at end because we needed to have the final item to trim it first
@@ -486,7 +489,7 @@ export class _Tokenizer {
486489

487490
const headers = splitCells(cap[1]);
488491
const aligns = cap[2].replace(this.rules.other.tableAlignChars, '').split('|');
489-
const rows = cap[3] && cap[3].trim() ? cap[3].replace(this.rules.other.tableRowBlankLine, '').split('\n') : [];
492+
const rows = cap[3]?.trim() ? cap[3].replace(this.rules.other.tableRowBlankLine, '').split('\n') : [];
490493

491494
const item: Tokens.Table = {
492495
type: 'table',

‎src/Tokens.ts

+104-104
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,112 @@
11
/* eslint-disable no-use-before-define */
22

33
export type MarkedToken = (
4-
Tokens.Space
4+
Tokens.Blockquote
5+
| Tokens.Br
56
| Tokens.Code
7+
| Tokens.Codespan
8+
| Tokens.Def
9+
| Tokens.Del
10+
| Tokens.Em
11+
| Tokens.Escape
612
| Tokens.Heading
7-
| Tokens.Table
813
| Tokens.Hr
9-
| Tokens.Blockquote
10-
| Tokens.List
11-
| Tokens.ListItem
12-
| Tokens.Paragraph
1314
| Tokens.HTML
14-
| Tokens.Text
15-
| Tokens.Def
16-
| Tokens.Escape
17-
| Tokens.Tag
1815
| Tokens.Image
1916
| Tokens.Link
17+
| Tokens.List
18+
| Tokens.ListItem
19+
| Tokens.Paragraph
20+
| Tokens.Space
2021
| Tokens.Strong
21-
| Tokens.Em
22-
| Tokens.Codespan
23-
| Tokens.Br
24-
| Tokens.Del);
22+
| Tokens.Table
23+
| Tokens.Tag
24+
| Tokens.Text
25+
);
2526

2627
export type Token = (
27-
MarkedToken
28+
MarkedToken
2829
| Tokens.Generic);
2930

3031
export namespace Tokens {
31-
export interface Space {
32-
type: 'space';
32+
export interface Blockquote {
33+
type: 'blockquote';
34+
raw: string;
35+
text: string;
36+
tokens: Token[];
37+
}
38+
39+
export interface Br {
40+
type: 'br';
3341
raw: string;
3442
}
3543

44+
export interface Checkbox {
45+
checked: boolean;
46+
}
47+
3648
export interface Code {
3749
type: 'code';
3850
raw: string;
39-
codeBlockStyle?: 'indented' | undefined;
40-
lang?: string | undefined;
51+
codeBlockStyle?: 'indented';
52+
lang?: string;
4153
text: string;
4254
escaped?: boolean;
4355
}
4456

45-
export interface Heading {
46-
type: 'heading';
57+
export interface Codespan {
58+
type: 'codespan';
4759
raw: string;
48-
depth: number;
4960
text: string;
50-
tokens: Token[];
5161
}
5262

53-
export interface Table {
54-
type: 'table';
63+
export interface Def {
64+
type: 'def';
5565
raw: string;
56-
align: Array<'center' | 'left' | 'right' | null>;
57-
header: TableCell[];
58-
rows: TableCell[][];
59-
}
60-
61-
export interface TableRow {
62-
text: string;
66+
tag: string;
67+
href: string;
68+
title: string;
6369
}
6470

65-
export interface TableCell {
71+
export interface Del {
72+
type: 'del';
73+
raw: string;
6674
text: string;
6775
tokens: Token[];
68-
header: boolean;
69-
align: 'center' | 'left' | 'right' | null;
7076
}
7177

72-
export interface Hr {
73-
type: 'hr';
78+
export interface Em {
79+
type: 'em';
7480
raw: string;
81+
text: string;
82+
tokens: Token[];
7583
}
7684

77-
export interface Blockquote {
78-
type: 'blockquote';
85+
export interface Escape {
86+
type: 'escape';
7987
raw: string;
8088
text: string;
81-
tokens: Token[];
8289
}
8390

84-
export interface List {
85-
type: 'list';
91+
export interface Generic {
92+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
93+
[index: string]: any;
94+
type: string;
8695
raw: string;
87-
ordered: boolean;
88-
start: number | '';
89-
loose: boolean;
90-
items: ListItem[];
96+
tokens?: Token[];
9197
}
9298

93-
export interface ListItem {
94-
type: 'list_item';
99+
export interface Heading {
100+
type: 'heading';
95101
raw: string;
96-
task: boolean;
97-
checked?: boolean | undefined;
98-
loose: boolean;
102+
depth: number;
99103
text: string;
100104
tokens: Token[];
101105
}
102106

103-
export interface Checkbox {
104-
checked: boolean;
105-
}
106-
107-
export interface Paragraph {
108-
type: 'paragraph';
107+
export interface Hr {
108+
type: 'hr';
109109
raw: string;
110-
pre?: boolean | undefined;
111-
text: string;
112-
tokens: Token[];
113110
}
114111

115112
export interface HTML {
@@ -120,52 +117,53 @@ export namespace Tokens {
120117
block: boolean;
121118
}
122119

123-
export interface Text {
124-
type: 'text';
120+
export interface Image {
121+
type: 'image';
125122
raw: string;
123+
href: string;
124+
title: string | null;
126125
text: string;
127-
tokens?: Token[];
128-
escaped?: boolean;
129126
}
130127

131-
export interface Def {
132-
type: 'def';
128+
export interface Link {
129+
type: 'link';
133130
raw: string;
134-
tag: string;
135131
href: string;
136-
title: string;
132+
title?: string | null;
133+
text: string;
134+
tokens: Token[];
137135
}
138136

139-
export interface Escape {
140-
type: 'escape';
137+
export interface List {
138+
type: 'list';
141139
raw: string;
142-
text: string;
140+
ordered: boolean;
141+
start: number | '';
142+
loose: boolean;
143+
items: ListItem[];
143144
}
144145

145-
export interface Tag {
146-
type: 'html';
146+
export interface ListItem {
147+
type: 'list_item';
147148
raw: string;
148-
inLink: boolean;
149-
inRawBlock: boolean;
149+
task: boolean;
150+
checked?: boolean;
151+
loose: boolean;
150152
text: string;
151-
block: boolean;
153+
tokens: Token[];
152154
}
153155

154-
export interface Link {
155-
type: 'link';
156+
export interface Paragraph {
157+
type: 'paragraph';
156158
raw: string;
157-
href: string;
158-
title?: string | null;
159+
pre?: boolean;
159160
text: string;
160161
tokens: Token[];
161162
}
162163

163-
export interface Image {
164-
type: 'image';
164+
export interface Space {
165+
type: 'space';
165166
raw: string;
166-
href: string;
167-
title: string | null;
168-
text: string;
169167
}
170168

171169
export interface Strong {
@@ -175,38 +173,40 @@ export namespace Tokens {
175173
tokens: Token[];
176174
}
177175

178-
export interface Em {
179-
type: 'em';
176+
export interface Table {
177+
type: 'table';
180178
raw: string;
181-
text: string;
182-
tokens: Token[];
179+
align: Array<'center' | 'left' | 'right' | null>;
180+
header: TableCell[];
181+
rows: TableCell[][];
183182
}
184183

185-
export interface Codespan {
186-
type: 'codespan';
187-
raw: string;
184+
export interface TableCell {
188185
text: string;
186+
tokens: Token[];
187+
header: boolean;
188+
align: 'center' | 'left' | 'right' | null;
189189
}
190190

191-
export interface Br {
192-
type: 'br';
193-
raw: string;
191+
export interface TableRow {
192+
text: string;
194193
}
195194

196-
export interface Del {
197-
type: 'del';
195+
export interface Tag {
196+
type: 'html';
198197
raw: string;
198+
inLink: boolean;
199+
inRawBlock: boolean;
199200
text: string;
200-
tokens: Token[];
201+
block: boolean;
201202
}
202203

203-
export interface Generic {
204-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
205-
[index: string]: any;
206-
207-
type: string;
204+
export interface Text {
205+
type: 'text';
208206
raw: string;
209-
tokens?: Token[] | undefined;
207+
text: string;
208+
tokens?: Token[];
209+
escaped?: boolean;
210210
}
211211
}
212212

‎src/helpers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export function splitCells(tableRow: string, count?: number) {
7272
if (!cells[0].trim()) {
7373
cells.shift();
7474
}
75-
if (cells.length > 0 && !cells[cells.length - 1].trim()) {
75+
if (cells.length > 0 && !cells.at(-1)?.trim()) {
7676
cells.pop();
7777
}
7878

‎src/marked.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ export function marked(src: string, options: MarkedOptions & { async: true }): P
3434
*/
3535
export function marked(src: string, options: MarkedOptions & { async: false }): string;
3636
export function marked(src: string, options: MarkedOptions & { async: true }): Promise<string>;
37-
export function marked(src: string, options?: MarkedOptions | undefined | null): string | Promise<string>;
38-
export function marked(src: string, opt?: MarkedOptions | undefined | null): string | Promise<string> {
37+
export function marked(src: string, options?: MarkedOptions | null): string | Promise<string>;
38+
export function marked(src: string, opt?: MarkedOptions | null): string | Promise<string> {
3939
return markedInstance.parse(src, opt);
4040
}
4141

‎test/rules.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function propsToString(obj) {
3434
return null;
3535
}
3636
if (obj.constructor.name === 'Object') {
37-
if (obj.exec && obj.exec.name === 'noopTest') {
37+
if (obj.exec?.name === 'noopTest') {
3838
return null;
3939
}
4040
for (const prop in obj) {
@@ -61,7 +61,7 @@ if (process.argv.length > 2) {
6161
rule = rule[prop];
6262
}
6363
}
64-
rulesList[rulePath[0]] = rule && rule[rulePath[0]] ? rule[rulePath[0]] : null;
64+
rulesList[rulePath[0]] = rule?.[rulePath[0]] ?? null;
6565
}
6666
} else {
6767
rulesObj = rules;

‎test/types/marked.ts

+25-5
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ let options: MarkedOptions = {
4646
options = marked.getDefaults();
4747
options = marked.defaults;
4848

49-
function callback(err: Error | null, markdown: string | undefined) {
50-
console.log('Callback called!');
51-
console.log(markdown);
52-
}
53-
5449
let myOldMarked: typeof marked = marked.options(options);
5550
myOldMarked = marked.setOptions(options);
5651

@@ -378,3 +373,28 @@ import { inline } from 'marked';
378373
// Rules is exported
379374
import type { Rules } from 'marked';
380375

376+
marked.parse('', {
377+
async: undefined,
378+
breaks: undefined,
379+
extensions: undefined,
380+
gfm: undefined,
381+
hooks: undefined,
382+
pedantic: undefined,
383+
renderer: undefined,
384+
silent: undefined,
385+
tokenizer: undefined,
386+
walkTokens: undefined,
387+
});
388+
389+
marked.use({
390+
async: undefined,
391+
breaks: undefined,
392+
extensions: undefined,
393+
gfm: undefined,
394+
hooks: undefined,
395+
pedantic: undefined,
396+
renderer: undefined,
397+
silent: undefined,
398+
tokenizer: undefined,
399+
walkTokens: undefined,
400+
});

‎test/unit/marked.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -673,8 +673,8 @@ used extension2 walked</p>
673673
}],
674674
walkTokens(token) {
675675
if (token.tokens) {
676-
const finalChildToken = token.tokens[token.tokens.length - 1];
677-
if (finalChildToken && finalChildToken.type === 'inlineStyleTag') {
676+
const finalChildToken = token.tokens.at(-1);
677+
if (finalChildToken?.type === 'inlineStyleTag') {
678678
token.originalType = token.type;
679679
token.type = 'styled';
680680
token.style = `style="color:${finalChildToken.text};"`;

0 commit comments

Comments
 (0)
Please sign in to comment.