Skip to content

Commit

Permalink
Replaced ts-ignore with ts-expect-error and reduced usages of any
Browse files Browse the repository at this point in the history
  • Loading branch information
DreierF committed May 16, 2023
1 parent fa39ce6 commit 603e8bb
Show file tree
Hide file tree
Showing 19 changed files with 153 additions and 168 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ npm install -g marked

```sh
npm install marked
npm install @types/marked # For TypeScript projects
```

## Usage
Expand Down
29 changes: 14 additions & 15 deletions lib/marked.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -582,9 +582,10 @@ var _Tokenizer = class {
};
if (this.options.sanitize) {
const text = this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0]);
token.type = "paragraph";
token.text = text;
token.tokens = this.lexer.inline(text);
const paragraph = token;
paragraph.type = "paragraph";
paragraph.text = text;
paragraph.tokens = this.lexer.inline(text);
}
return token;
}
Expand All @@ -609,7 +610,8 @@ var _Tokenizer = class {
if (cap) {
const item = {
type: "table",
// @ts-ignore
// splitCells expects a number as second argument
// @ts-expect-error
header: splitCells(cap[1]).map((c) => {
return { text: c };
}),
Expand Down Expand Up @@ -961,7 +963,6 @@ var block = {
list: /^( {0,3}bull)([ \t][^\n]+?)?(?:\n|$)/,
html: "^ {0,3}(?:<(script|pre|style|textarea)[\\s>][\\s\\S]*?(?:</\\1>[^\\n]*\\n+|$)|comment[^\\n]*(\\n+|$)|<\\?[\\s\\S]*?(?:\\?>\\n*|$)|<![A-Z][\\s\\S]*?(?:>\\n*|$)|<!\\[CDATA\\[[\\s\\S]*?(?:\\]\\]>\\n*|$)|</?(tag)(?: +|\\n|/?>)[\\s\\S]*?(?:(?:\\n *)+\\n|$)|<(?!script|pre|style|textarea)([a-z][\\w-]*)(?:attribute)*? */?>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n *)+\\n|$)|</(?!script|pre|style|textarea)[a-z][\\w-]*\\s*>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n *)+\\n|$))",
def: /^ {0,3}\[(label)\]: *(?:\n *)?([^<\s][^\s]*|<.*?>)(?:(?: +(?:\n *)?| *\n *)(title))? *(?:\n+|$)/,
// @ts-ignore
table: noopTest,
lheading: /^((?:.|\n(?!\n))+?)\n {0,3}(=+|-+) *(?:\n+|$)/,
// regex template, placeholders will be replaced according to different paragraph
Expand Down Expand Up @@ -1003,7 +1004,6 @@ block.pedantic = {
var inline = {
escape: /^\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/,
autolink: /^<(scheme:[^\s\x00-\x1f<>]*|email)>/,
// @ts-ignore
url: noopTest,
tag: "^comment|^</[a-zA-Z][\\w:-]*\\s*>|^<[a-zA-Z][\\w-]*(?:attribute)*?\\s*/?>|^<\\?[\\s\\S]*?\\?>|^<![a-zA-Z]+\\s[\\s\\S]*?>|^<!\\[CDATA\\[[\\s\\S]*?\\]\\]>",
// CDATA section
Expand All @@ -1021,7 +1021,6 @@ var inline = {
},
code: /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/,
br: /^( {2,}|\\)\n(?!\s*$)/,
// @ts-ignore
del: noopTest,
text: /^(`+|[^`])(?:(?= {2,}\n)|[\s\S]*?(?:(?=[\\<!\[`*_]|\b_|$)|[^ ](?= {2,}\n)))/,
punctuation: /^([\spunctuation])/
Expand Down Expand Up @@ -1265,7 +1264,7 @@ var _Lexer = class {
let startIndex = Infinity;
const tempSrc = src.slice(1);
let tempStart;
this.options.extensions.startBlock.forEach(function(getStartIndex) {
this.options.extensions.startBlock.forEach((getStartIndex) => {
tempStart = getStartIndex.call({ lexer: this }, tempSrc);
if (typeof tempStart === "number" && tempStart >= 0) {
startIndex = Math.min(startIndex, tempStart);
Expand Down Expand Up @@ -1426,7 +1425,7 @@ var _Lexer = class {
let startIndex = Infinity;
const tempSrc = src.slice(1);
let tempStart;
this.options.extensions.startInline.forEach(function(getStartIndex) {
this.options.extensions.startInline.forEach((getStartIndex) => {
tempStart = getStartIndex.call({ lexer: this }, tempSrc);
if (typeof tempStart === "number" && tempStart >= 0) {
startIndex = Math.min(startIndex, tempStart);
Expand Down Expand Up @@ -2096,21 +2095,21 @@ marked.use = function(...args) {
if (!ext.name) {
throw new Error("extension name required");
}
if (ext.renderer) {
if ("renderer" in ext) {
const prevRenderer = extensions.renderers[ext.name];
if (prevRenderer) {
extensions.renderers[ext.name] = function(...args2) {
let ret = ext.renderer.apply(this, args2);
extensions.renderers[ext.name] = function(token) {
let ret = ext.renderer.apply(this, [token]);
if (ret === false) {
ret = prevRenderer.apply(this, args2);
ret = prevRenderer.apply(this, [token]);
}
return ret;
};
} else {
extensions.renderers[ext.name] = ext.renderer;
}
}
if (ext.tokenizer) {
if ("tokenizer" in ext) {
if (!ext.level || ext.level !== "block" && ext.level !== "inline") {
throw new Error("extension level must be 'block' or 'inline'");
}
Expand All @@ -2135,7 +2134,7 @@ marked.use = function(...args) {
}
}
}
if (ext.childTokens) {
if ("childTokens" in ext && ext.childTokens) {
extensions.childTokens[ext.name] = ext.childTokens;
}
});
Expand Down
2 changes: 1 addition & 1 deletion lib/marked.cjs.map

Large diffs are not rendered by default.

38 changes: 23 additions & 15 deletions lib/marked.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
type Token = (Tokens.Space | Tokens.Code | Tokens.Heading | Tokens.Table | Tokens.Hr | Tokens.Blockquote | Tokens.List | Tokens.ListItem | Tokens.Paragraph | Tokens.HTML | Tokens.Text | Tokens.Def | Tokens.Escape | Tokens.Tag | Tokens.Image | Tokens.Link | Tokens.Strong | Tokens.Em | Tokens.Codespan | Tokens.Br | Tokens.Del) & {
loose?: boolean;
tokens?: Token[];
};
declare namespace Tokens {
interface Space {
Expand All @@ -23,14 +24,14 @@ declare namespace Tokens {
}
interface Table {
type: 'table';
raw: string;
raw?: string;
align: Array<'center' | 'left' | 'right' | null>;
header: TableCell[];
rows: TableCell[][];
}
interface TableCell {
text: string;
tokens: Token[];
tokens?: Token[];
}
interface Hr {
type: 'hr';
Expand All @@ -57,7 +58,7 @@ declare namespace Tokens {
checked?: boolean | undefined;
loose: boolean;
text: string;
tokens: Token[];
tokens?: Token[];
}
interface Paragraph {
type: 'paragraph';
Expand Down Expand Up @@ -172,7 +173,7 @@ declare class _Renderer {
* @param raw
* @param slugger
*/
heading(text: string, level: number, raw: string, slugger: any): string;
heading(text: string, level: number, raw: string, slugger: _Slugger): string;
hr(): string;
list(body: string, ordered: boolean, start: number | ''): string;
/**
Expand Down Expand Up @@ -296,7 +297,7 @@ declare class _Parser {
* Tokenizer
*/
declare class _Tokenizer {
options: MarkedExtension;
options: MarkedOptions;
rules: any;
lexer: _Lexer;
constructor(options?: MarkedOptions);
Expand Down Expand Up @@ -399,7 +400,7 @@ interface MarkedExtension {
* if highlighting was successful)
* @deprecated Deprecated in v5.0.0 use marked-highlight to add highlighting to code blocks.
*/
highlight?: ((code: string, lang: string | undefined, callback?: (error: any, code?: string) => void) => string | void) | null;
highlight?: ((code: string, lang: string | undefined, callback?: (error: Error, code?: string) => void) => string | void) | null;
/**
* Hooks are methods that hook into some part of marked.
* preprocess is called to process markdown before sending it to marked.
Expand Down Expand Up @@ -463,14 +464,14 @@ interface MarkedExtension {
* Each token is passed by reference so updates are persisted when passed to the parser.
* The return value of the function is ignored.
*/
walkTokens?: ((token: Token) => void) | undefined | null;
walkTokens?: ((token: Token) => void | Promise<void>) | undefined | null;
/**
* Generate closing slash for self-closing tags (<br/> instead of <br>)
* @deprecated Deprecated in v5.0.0 use marked-xhtml to emit self-closing HTML tags for void elements (<br/>, <img/>, etc.) with a "/" as required by XHTML.
*/
xhtml?: boolean | undefined;
}
interface MarkedOptions extends Omit<MarkedExtension, 'extensions' | 'renderer' | 'tokenizer'> {
interface MarkedOptions extends Omit<MarkedExtension, 'extensions' | 'renderer' | 'tokenizer' | 'walkTokens'> {
/**
* Type: object Default: new Renderer()
*
Expand All @@ -481,13 +482,20 @@ interface MarkedOptions extends Omit<MarkedExtension, 'extensions' | 'renderer'
* The tokenizer defines how to turn markdown text into tokens.
*/
tokenizer?: Omit<_Tokenizer, 'constructor'> | undefined | null;
/**
* The walkTokens function gets called with every token.
* Child tokens are called before moving on to sibling tokens.
* Each token is passed by reference so updates are persisted when passed to the parser.
* The return value of the function is ignored.
*/
walkTokens?: ((token: Token) => void | Promise<void> | Array<void | Promise<void>>) | undefined | null;
/**
* Add tokenizers and renderers to marked
*/
extensions?: (TokenizerAndRendererExtension[] & {
renderers: Record<string, any>;
childTokens: any;
block: any;
renderers: Record<string, (this: RendererThis, token: Tokens.Generic) => string | false | undefined>;
childTokens: Record<string, string[]>;
block: any[];
inline: any[];
startBlock: Array<(this: TokenizerThis, src: string) => number | void>;
startInline: Array<(this: TokenizerThis, src: string) => number | void>;
Expand Down Expand Up @@ -556,7 +564,7 @@ declare class _Hooks {
declare function _getDefaults(): MarkedOptions;
declare let _defaults: MarkedOptions;

type ResultCallback = (error: any, parseResult?: string) => undefined | void;
type ResultCallback = (error: Error | null, parseResult?: string) => undefined | void;
/**
* Compiles markdown to HTML asynchronously.
*
Expand Down Expand Up @@ -597,7 +605,7 @@ declare namespace marked {
var defaults: MarkedOptions;
var use: (...args: MarkedExtension[]) => void;
var walkTokens: <T = void>(tokens: TokensList | Token[], callback: (token: Token) => T | T[]) => T[];
var parseInline: (src: string, opt?: ResultCallback | MarkedOptions | undefined, callback?: ResultCallback | undefined) => string | Promise<string | undefined> | undefined;
var parseInline: (src: string, opt?: ResultCallback | MarkedOptions | null | undefined, callback?: ResultCallback | undefined) => string | Promise<string | undefined> | undefined;
var Parser: typeof _Parser;
var parser: typeof _Parser.parse;
var Renderer: typeof _Renderer;
Expand All @@ -613,9 +621,9 @@ declare const options: (opt: MarkedOptions) => typeof marked;
declare const setOptions: (opt: MarkedOptions) => typeof marked;
declare const use: (...args: MarkedExtension[]) => void;
declare const walkTokens: <T = void>(tokens: Token[] | TokensList, callback: (token: Token) => T | T[]) => T[];
declare const parseInline: (src: string, opt?: MarkedOptions | ResultCallback | undefined, callback?: ResultCallback | undefined) => string | Promise<string | undefined> | undefined;
declare const parseInline: (src: string, opt?: MarkedOptions | ResultCallback | undefined | null, callback?: ResultCallback | undefined) => string | Promise<string | undefined> | undefined;
declare const parse: typeof marked;
declare const parser: typeof _Parser.parse;
declare const lexer: typeof _Lexer.lex;

export { _Hooks as Hooks, _Lexer as Lexer, MarkedExtension, MarkedOptions, _Parser as Parser, _Renderer as Renderer, RendererExtension, RendererThis, Rule, Rules, _Slugger as Slugger, SluggerOptions, _TextRenderer as TextRenderer, Token, _Tokenizer as Tokenizer, TokenizerAndRendererExtension, TokenizerExtension, TokenizerThis, Tokens, TokensList, _defaults as defaults, _getDefaults as getDefaults, lexer, marked, options, parse, parseInline, parser, setOptions, use, walkTokens };
export { _Hooks as Hooks, _Lexer as Lexer, MarkedExtension, MarkedOptions, _Parser as Parser, _Renderer as Renderer, RendererExtension, RendererThis, ResultCallback, Rule, Rules, _Slugger as Slugger, SluggerOptions, _TextRenderer as TextRenderer, Token, _Tokenizer as Tokenizer, TokenizerAndRendererExtension, TokenizerExtension, TokenizerThis, Tokens, TokensList, _defaults as defaults, _getDefaults as getDefaults, lexer, marked, options, parse, parseInline, parser, setOptions, use, walkTokens };
29 changes: 14 additions & 15 deletions lib/marked.esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/marked.esm.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit 603e8bb

Please sign in to comment.