fix: noImplicitAny: true (#2891)
This commit is contained in:
parent
eb4ce21127
commit
ff1602c178
56
lib/marked.cjs
generated
56
lib/marked.cjs
generated
@ -191,12 +191,14 @@ function splitCells(tableRow, count) {
|
||||
if (cells.length > 0 && !cells[cells.length - 1].trim()) {
|
||||
cells.pop();
|
||||
}
|
||||
if (cells.length > count) {
|
||||
cells.splice(count);
|
||||
}
|
||||
else {
|
||||
while (cells.length < count)
|
||||
cells.push('');
|
||||
if (count) {
|
||||
if (cells.length > count) {
|
||||
cells.splice(count);
|
||||
}
|
||||
else {
|
||||
while (cells.length < count)
|
||||
cells.push('');
|
||||
}
|
||||
}
|
||||
for (; i < cells.length; i++) {
|
||||
// leading or trailing whitespace is ignored per the gfm spec
|
||||
@ -629,8 +631,7 @@ class _Tokenizer {
|
||||
if (cap) {
|
||||
const item = {
|
||||
type: 'table',
|
||||
// splitCells expects a number as second argument
|
||||
// @ts-expect-error
|
||||
raw: cap[0],
|
||||
header: splitCells(cap[1]).map(c => {
|
||||
return { text: c };
|
||||
}),
|
||||
@ -638,7 +639,6 @@ class _Tokenizer {
|
||||
rows: cap[3] && cap[3].trim() ? cap[3].replace(/\n[ \t]*$/, '').split('\n') : []
|
||||
};
|
||||
if (item.header.length === item.align.length) {
|
||||
item.raw = cap[0];
|
||||
let l = item.align.length;
|
||||
let i, j, k, row;
|
||||
for (i = 0; i < l; i++) {
|
||||
@ -1408,7 +1408,10 @@ class _Lexer {
|
||||
return leading + ' '.repeat(tabs.length);
|
||||
});
|
||||
}
|
||||
let token, lastToken, cutSrc, lastParagraphClipped;
|
||||
let token;
|
||||
let lastToken;
|
||||
let cutSrc;
|
||||
let lastParagraphClipped;
|
||||
while (src) {
|
||||
if (this.options.extensions
|
||||
&& this.options.extensions.block
|
||||
@ -2258,6 +2261,7 @@ class Marked {
|
||||
default: {
|
||||
if (this.defaults.extensions && this.defaults.extensions.childTokens && this.defaults.extensions.childTokens[token.type]) { // Walk any extensions
|
||||
this.defaults.extensions.childTokens[token.type].forEach((childTokens) => {
|
||||
// @ts-expect-error we assume token[childToken] is an array of tokens but we can't be sure
|
||||
values = values.concat(this.walkTokens(token[childTokens], callback));
|
||||
});
|
||||
}
|
||||
@ -2337,14 +2341,16 @@ class Marked {
|
||||
if (pack.renderer) {
|
||||
const renderer = this.defaults.renderer || new _Renderer(this.defaults);
|
||||
for (const prop in pack.renderer) {
|
||||
const prevRenderer = renderer[prop];
|
||||
const rendererFunc = pack.renderer[prop];
|
||||
const rendererKey = prop;
|
||||
const prevRenderer = renderer[rendererKey];
|
||||
// Replace renderer with func to run extension, but fall back if false
|
||||
renderer[prop] = (...args) => {
|
||||
let ret = pack.renderer[prop].apply(renderer, args);
|
||||
renderer[rendererKey] = (...args) => {
|
||||
let ret = rendererFunc.apply(renderer, args);
|
||||
if (ret === false) {
|
||||
ret = prevRenderer.apply(renderer, args);
|
||||
}
|
||||
return ret;
|
||||
return ret || '';
|
||||
};
|
||||
}
|
||||
opts.renderer = renderer;
|
||||
@ -2352,10 +2358,12 @@ class Marked {
|
||||
if (pack.tokenizer) {
|
||||
const tokenizer = this.defaults.tokenizer || new _Tokenizer(this.defaults);
|
||||
for (const prop in pack.tokenizer) {
|
||||
const prevTokenizer = tokenizer[prop];
|
||||
const tokenizerFunc = pack.tokenizer[prop];
|
||||
const tokenizerKey = prop;
|
||||
const prevTokenizer = tokenizer[tokenizerKey];
|
||||
// Replace tokenizer with func to run extension, but fall back if false
|
||||
tokenizer[prop] = (...args) => {
|
||||
let ret = pack.tokenizer[prop].apply(tokenizer, args);
|
||||
tokenizer[tokenizerKey] = (...args) => {
|
||||
let ret = tokenizerFunc.apply(tokenizer, args);
|
||||
if (ret === false) {
|
||||
ret = prevTokenizer.apply(tokenizer, args);
|
||||
}
|
||||
@ -2368,21 +2376,23 @@ class Marked {
|
||||
if (pack.hooks) {
|
||||
const hooks = this.defaults.hooks || new _Hooks();
|
||||
for (const prop in pack.hooks) {
|
||||
const prevHook = hooks[prop];
|
||||
const hooksFunc = pack.hooks[prop];
|
||||
const hooksKey = prop;
|
||||
const prevHook = hooks[hooksKey];
|
||||
if (_Hooks.passThroughHooks.has(prop)) {
|
||||
hooks[prop] = (arg) => {
|
||||
hooks[hooksKey] = (arg) => {
|
||||
if (this.defaults.async) {
|
||||
return Promise.resolve(pack.hooks[prop].call(hooks, arg)).then(ret => {
|
||||
return Promise.resolve(hooksFunc.call(hooks, arg)).then(ret => {
|
||||
return prevHook.call(hooks, ret);
|
||||
});
|
||||
}
|
||||
const ret = pack.hooks[prop].call(hooks, arg);
|
||||
const ret = hooksFunc.call(hooks, arg);
|
||||
return prevHook.call(hooks, ret);
|
||||
};
|
||||
}
|
||||
else {
|
||||
hooks[prop] = (...args) => {
|
||||
let ret = pack.hooks[prop].apply(hooks, args);
|
||||
hooks[hooksKey] = (...args) => {
|
||||
let ret = hooksFunc.apply(hooks, args);
|
||||
if (ret === false) {
|
||||
ret = prevHook.apply(hooks, args);
|
||||
}
|
||||
|
2
lib/marked.cjs.map
generated
2
lib/marked.cjs.map
generated
File diff suppressed because one or more lines are too long
557
lib/marked.d.ts
generated
vendored
557
lib/marked.d.ts
generated
vendored
@ -1,5 +1,5 @@
|
||||
declare module "Tokens" {
|
||||
export 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) & {
|
||||
export 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 | Tokens.Generic) & {
|
||||
loose?: boolean;
|
||||
tokens?: Token[];
|
||||
};
|
||||
@ -25,7 +25,7 @@ declare module "Tokens" {
|
||||
}
|
||||
interface Table {
|
||||
type: 'table';
|
||||
raw?: string;
|
||||
raw: string;
|
||||
align: Array<'center' | 'left' | 'right' | null>;
|
||||
header: TableCell[];
|
||||
rows: TableCell[][];
|
||||
@ -156,7 +156,7 @@ declare module "Tokens" {
|
||||
};
|
||||
}
|
||||
declare module "Tokenizer" {
|
||||
import { _Lexer } from "Lexer";
|
||||
import type { _Lexer } from "Lexer";
|
||||
import type { Links, Tokens } from "Tokens";
|
||||
import type { MarkedOptions } from "MarkedOptions";
|
||||
/**
|
||||
@ -309,7 +309,7 @@ declare module "Instance" {
|
||||
export class Marked {
|
||||
#private;
|
||||
defaults: MarkedOptions;
|
||||
options: (opt: any) => this;
|
||||
options: (opt: MarkedOptions) => this;
|
||||
parse: (src: string, optOrCallback?: MarkedOptions | ResultCallback | undefined | null, callback?: ResultCallback | undefined) => string | Promise<string | undefined> | undefined;
|
||||
parseInline: (src: string, optOrCallback?: MarkedOptions | ResultCallback | undefined | null, callback?: ResultCallback | undefined) => string | Promise<string | undefined> | undefined;
|
||||
Parser: typeof _Parser;
|
||||
@ -327,7 +327,7 @@ declare module "Instance" {
|
||||
*/
|
||||
walkTokens<T = void>(tokens: Token[] | TokensList, callback: (token: Token) => T | T[]): T[];
|
||||
use(...args: MarkedExtension[]): this;
|
||||
setOptions(opt: any): this;
|
||||
setOptions(opt: MarkedOptions): this;
|
||||
}
|
||||
}
|
||||
declare module "helpers" {
|
||||
@ -345,7 +345,7 @@ declare module "helpers" {
|
||||
export const noopTest: {
|
||||
exec: () => null;
|
||||
};
|
||||
export function splitCells(tableRow: string, count: number): string[];
|
||||
export function splitCells(tableRow: string, count?: number): string[];
|
||||
/**
|
||||
* Remove trailing 'c's. Equivalent to str.replace(/c*$/, '').
|
||||
* /c*$/ is vulnerable to REDOS.
|
||||
@ -358,6 +358,279 @@ declare module "helpers" {
|
||||
export function findClosingBracket(str: string, b: string): number;
|
||||
export function checkDeprecations(opt: MarkedOptions, callback?: ResultCallback): void;
|
||||
}
|
||||
declare module "Renderer" {
|
||||
import type { MarkedOptions } from "MarkedOptions";
|
||||
import type { _Slugger } from "Slugger";
|
||||
/**
|
||||
* Renderer
|
||||
*/
|
||||
export class _Renderer {
|
||||
options: MarkedOptions;
|
||||
constructor(options?: MarkedOptions);
|
||||
code(code: string, infostring: string | undefined, escaped: boolean): string;
|
||||
blockquote(quote: string): string;
|
||||
html(html: string, block?: boolean): string;
|
||||
heading(text: string, level: number, raw: string, slugger: _Slugger): string;
|
||||
hr(): string;
|
||||
list(body: string, ordered: boolean, start: number | ''): string;
|
||||
listitem(text: string, task: boolean, checked: boolean): string;
|
||||
checkbox(checked: boolean): string;
|
||||
paragraph(text: string): string;
|
||||
table(header: string, body: string): string;
|
||||
tablerow(content: string): string;
|
||||
tablecell(content: string, flags: {
|
||||
header: boolean;
|
||||
align: 'center' | 'left' | 'right' | null;
|
||||
}): string;
|
||||
/**
|
||||
* span level renderer
|
||||
*/
|
||||
strong(text: string): string;
|
||||
em(text: string): string;
|
||||
codespan(text: string): string;
|
||||
br(): string;
|
||||
del(text: string): string;
|
||||
link(href: string, title: string | null | undefined, text: string): string;
|
||||
image(href: string, title: string | null, text: string): string;
|
||||
text(text: string): string;
|
||||
}
|
||||
}
|
||||
declare module "Parser" {
|
||||
import { _Renderer } from "Renderer";
|
||||
import { _TextRenderer } from "TextRenderer";
|
||||
import { _Slugger } from "Slugger";
|
||||
import type { Token } from "Tokens";
|
||||
import type { MarkedOptions } from "MarkedOptions";
|
||||
/**
|
||||
* Parsing & Compiling
|
||||
*/
|
||||
export class _Parser {
|
||||
options: MarkedOptions;
|
||||
renderer: _Renderer;
|
||||
textRenderer: _TextRenderer;
|
||||
slugger: _Slugger;
|
||||
constructor(options?: MarkedOptions);
|
||||
/**
|
||||
* Static Parse Method
|
||||
*/
|
||||
static parse(tokens: Token[], options?: MarkedOptions): string;
|
||||
/**
|
||||
* Static Parse Inline Method
|
||||
*/
|
||||
static parseInline(tokens: Token[], options?: MarkedOptions): string;
|
||||
/**
|
||||
* Parse Loop
|
||||
*/
|
||||
parse(tokens: Token[], top?: boolean): string;
|
||||
/**
|
||||
* Parse Inline Tokens
|
||||
*/
|
||||
parseInline(tokens: Token[], renderer?: _Renderer | _TextRenderer): string;
|
||||
}
|
||||
}
|
||||
declare module "MarkedOptions" {
|
||||
import type { Token, Tokens, TokensList } from "Tokens";
|
||||
import type { _Parser } from "Parser";
|
||||
import type { _Lexer } from "Lexer";
|
||||
import type { _Renderer } from "Renderer";
|
||||
import type { _Tokenizer } from "Tokenizer";
|
||||
export interface SluggerOptions {
|
||||
/** Generates the next unique slug without updating the internal accumulator. */
|
||||
dryrun?: boolean;
|
||||
}
|
||||
export interface TokenizerThis {
|
||||
lexer: _Lexer;
|
||||
}
|
||||
export interface TokenizerExtension {
|
||||
name: string;
|
||||
level: 'block' | 'inline';
|
||||
start?: ((this: TokenizerThis, src: string) => number | void) | undefined;
|
||||
tokenizer: (this: TokenizerThis, src: string, tokens: Token[] | TokensList) => Tokens.Generic | undefined;
|
||||
childTokens?: string[] | undefined;
|
||||
}
|
||||
export interface RendererThis {
|
||||
parser: _Parser;
|
||||
}
|
||||
export type RendererExtensionFunction = (this: RendererThis, token: Tokens.Generic) => string | false | undefined;
|
||||
export interface RendererExtension {
|
||||
name: string;
|
||||
renderer: RendererExtensionFunction;
|
||||
}
|
||||
export type TokenizerAndRendererExtension = TokenizerExtension | RendererExtension | (TokenizerExtension & RendererExtension);
|
||||
type RendererApi = Omit<_Renderer, 'constructor' | 'options'>;
|
||||
type RendererObject = {
|
||||
[K in keyof RendererApi]?: (...args: Parameters<RendererApi[K]>) => ReturnType<RendererApi[K]> | false;
|
||||
};
|
||||
type TokenizerApi = Omit<_Tokenizer, 'constructor' | 'options' | 'rules' | 'lexer'>;
|
||||
type TokenizerObject = {
|
||||
[K in keyof TokenizerApi]?: (...args: Parameters<TokenizerApi[K]>) => ReturnType<TokenizerApi[K]> | false;
|
||||
};
|
||||
export interface MarkedExtension {
|
||||
/**
|
||||
* True will tell marked to await any walkTokens functions before parsing the tokens and returning an HTML string.
|
||||
*/
|
||||
async?: boolean;
|
||||
/**
|
||||
* A prefix URL for any relative link.
|
||||
* @deprecated Deprecated in v5.0.0 use marked-base-url to prefix url for any relative link.
|
||||
*/
|
||||
baseUrl?: string | undefined | null;
|
||||
/**
|
||||
* Enable GFM line breaks. This option requires the gfm option to be true.
|
||||
*/
|
||||
breaks?: boolean | undefined;
|
||||
/**
|
||||
* Add tokenizers and renderers to marked
|
||||
*/
|
||||
extensions?: TokenizerAndRendererExtension[] | undefined | null;
|
||||
/**
|
||||
* Enable GitHub flavored markdown.
|
||||
*/
|
||||
gfm?: boolean | undefined;
|
||||
/**
|
||||
* Include an id attribute when emitting headings.
|
||||
* @deprecated Deprecated in v5.0.0 use marked-gfm-heading-id to include an id attribute when emitting headings (h1, h2, h3, etc).
|
||||
*/
|
||||
headerIds?: boolean | undefined;
|
||||
/**
|
||||
* Set the prefix for header tag ids.
|
||||
* @deprecated Deprecated in v5.0.0 use marked-gfm-heading-id to add a string to prefix the id attribute when emitting headings (h1, h2, h3, etc).
|
||||
*/
|
||||
headerPrefix?: string | undefined;
|
||||
/**
|
||||
* A function to highlight code blocks. The function can either be
|
||||
* synchronous (returning a string) or asynchronous (callback invoked
|
||||
* with an error if any occurred during highlighting and a string
|
||||
* 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: 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.
|
||||
* postprocess is called to process html after marked has finished parsing.
|
||||
*/
|
||||
hooks?: {
|
||||
preprocess: (markdown: string) => string | Promise<string>;
|
||||
postprocess: (html: string) => string | Promise<string>;
|
||||
options?: MarkedOptions;
|
||||
} | null;
|
||||
/**
|
||||
* Set the prefix for code block classes.
|
||||
* @deprecated Deprecated in v5.0.0 use marked-highlight to prefix the className in a <code> block. Useful for syntax highlighting.
|
||||
*/
|
||||
langPrefix?: string | undefined;
|
||||
/**
|
||||
* Mangle autolinks (<email@domain.com>).
|
||||
* @deprecated Deprecated in v5.0.0 use marked-mangle to mangle email addresses.
|
||||
*/
|
||||
mangle?: boolean | undefined;
|
||||
/**
|
||||
* Conform to obscure parts of markdown.pl as much as possible. Don't fix any of the original markdown bugs or poor behavior.
|
||||
*/
|
||||
pedantic?: boolean | undefined;
|
||||
/**
|
||||
* Type: object Default: new Renderer()
|
||||
*
|
||||
* An object containing functions to render tokens to HTML.
|
||||
*/
|
||||
renderer?: RendererObject | undefined | null;
|
||||
/**
|
||||
* Sanitize the output. Ignore any HTML that has been input. If true, sanitize the HTML passed into markdownString with the sanitizer function.
|
||||
* @deprecated Warning: This feature is deprecated and it should NOT be used as it cannot be considered secure. Instead use a sanitize library, like DOMPurify (recommended), sanitize-html or insane on the output HTML!
|
||||
*/
|
||||
sanitize?: boolean | undefined;
|
||||
/**
|
||||
* Optionally sanitize found HTML with a sanitizer function.
|
||||
* @deprecated A function to sanitize the HTML passed into markdownString.
|
||||
*/
|
||||
sanitizer?: ((html: string) => string) | null;
|
||||
/**
|
||||
* Shows an HTML error message when rendering fails.
|
||||
*/
|
||||
silent?: boolean | undefined;
|
||||
/**
|
||||
* Use smarter list behavior than the original markdown. May eventually be default with the old behavior moved into pedantic.
|
||||
*/
|
||||
smartLists?: boolean | undefined;
|
||||
/**
|
||||
* Use "smart" typograhic punctuation for things like quotes and dashes.
|
||||
* @deprecated Deprecated in v5.0.0 use marked-smartypants to use "smart" typographic punctuation for things like quotes and dashes.
|
||||
*/
|
||||
smartypants?: boolean | undefined;
|
||||
/**
|
||||
* The tokenizer defines how to turn markdown text into tokens.
|
||||
*/
|
||||
tokenizer?: TokenizerObject | 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>) | 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;
|
||||
}
|
||||
export interface MarkedOptions extends Omit<MarkedExtension, 'extensions' | 'renderer' | 'tokenizer' | 'walkTokens'> {
|
||||
/**
|
||||
* Type: object Default: new Renderer()
|
||||
*
|
||||
* An object containing functions to render tokens to HTML.
|
||||
*/
|
||||
renderer?: Omit<_Renderer, 'constructor'> | undefined | null;
|
||||
/**
|
||||
* 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, RendererExtensionFunction>;
|
||||
childTokens: Record<string, string[]>;
|
||||
block: any[];
|
||||
inline: any[];
|
||||
startBlock: Array<(this: TokenizerThis, src: string) => number | void>;
|
||||
startInline: Array<(this: TokenizerThis, src: string) => number | void>;
|
||||
}) | undefined | null;
|
||||
}
|
||||
}
|
||||
declare module "defaults" {
|
||||
import type { MarkedOptions } from "MarkedOptions";
|
||||
/**
|
||||
* Gets the original marked default options.
|
||||
*/
|
||||
export function _getDefaults(): MarkedOptions;
|
||||
export let _defaults: MarkedOptions;
|
||||
export function changeDefaults(newDefaults: MarkedOptions): void;
|
||||
}
|
||||
declare module "Hooks" {
|
||||
import type { MarkedOptions } from "MarkedOptions";
|
||||
export class _Hooks {
|
||||
options: MarkedOptions;
|
||||
constructor(options?: MarkedOptions);
|
||||
static passThroughHooks: Set<string>;
|
||||
/**
|
||||
* Process markdown before marked
|
||||
*/
|
||||
preprocess(markdown: string): string;
|
||||
/**
|
||||
* Process HTML after marked is finished
|
||||
*/
|
||||
postprocess(html: string): string;
|
||||
}
|
||||
}
|
||||
declare module "marked" {
|
||||
import { _Lexer } from "Lexer";
|
||||
import { _Parser } from "Parser";
|
||||
@ -450,275 +723,3 @@ declare module "marked" {
|
||||
export type * from "rules";
|
||||
export type * from "Tokens";
|
||||
}
|
||||
declare module "Renderer" {
|
||||
import type { MarkedOptions } from "MarkedOptions";
|
||||
import { Slugger } from "marked";
|
||||
/**
|
||||
* Renderer
|
||||
*/
|
||||
export class _Renderer {
|
||||
options: MarkedOptions;
|
||||
constructor(options?: MarkedOptions);
|
||||
code(code: string, infostring: string | undefined, escaped: boolean): string;
|
||||
blockquote(quote: string): string;
|
||||
html(html: string, block?: boolean): string;
|
||||
heading(text: string, level: number, raw: string, slugger: Slugger): string;
|
||||
hr(): string;
|
||||
list(body: string, ordered: boolean, start: number | ''): string;
|
||||
listitem(text: string, task: boolean, checked: boolean): string;
|
||||
checkbox(checked: boolean): string;
|
||||
paragraph(text: string): string;
|
||||
table(header: string, body: string): string;
|
||||
tablerow(content: string): string;
|
||||
tablecell(content: string, flags: {
|
||||
header: boolean;
|
||||
align: 'center' | 'left' | 'right' | null;
|
||||
}): string;
|
||||
/**
|
||||
* span level renderer
|
||||
*/
|
||||
strong(text: string): string;
|
||||
em(text: string): string;
|
||||
codespan(text: string): string;
|
||||
br(): string;
|
||||
del(text: string): string;
|
||||
link(href: string, title: string | null | undefined, text: string): string;
|
||||
image(href: string, title: string | null, text: string): string;
|
||||
text(text: string): string;
|
||||
}
|
||||
}
|
||||
declare module "Parser" {
|
||||
import { _Renderer } from "Renderer";
|
||||
import { _TextRenderer } from "TextRenderer";
|
||||
import { _Slugger } from "Slugger";
|
||||
import type { Token } from "Tokens";
|
||||
import type { MarkedOptions } from "MarkedOptions";
|
||||
/**
|
||||
* Parsing & Compiling
|
||||
*/
|
||||
export class _Parser {
|
||||
options: MarkedOptions;
|
||||
renderer: _Renderer;
|
||||
textRenderer: _TextRenderer;
|
||||
slugger: _Slugger;
|
||||
constructor(options?: MarkedOptions);
|
||||
/**
|
||||
* Static Parse Method
|
||||
*/
|
||||
static parse(tokens: Token[], options?: MarkedOptions): string;
|
||||
/**
|
||||
* Static Parse Inline Method
|
||||
*/
|
||||
static parseInline(tokens: Token[], options?: MarkedOptions): string;
|
||||
/**
|
||||
* Parse Loop
|
||||
*/
|
||||
parse(tokens: Token[], top?: boolean): string;
|
||||
/**
|
||||
* Parse Inline Tokens
|
||||
*/
|
||||
parseInline(tokens: Token[], renderer?: _Renderer | _TextRenderer): string;
|
||||
}
|
||||
}
|
||||
declare module "MarkedOptions" {
|
||||
import type { Token, Tokens, TokensList } from "Tokens";
|
||||
import { _Parser } from "Parser";
|
||||
import { _Lexer } from "Lexer";
|
||||
import { _Renderer } from "Renderer";
|
||||
import { _Tokenizer } from "Tokenizer";
|
||||
export interface SluggerOptions {
|
||||
/** Generates the next unique slug without updating the internal accumulator. */
|
||||
dryrun?: boolean;
|
||||
}
|
||||
export interface TokenizerThis {
|
||||
lexer: _Lexer;
|
||||
}
|
||||
export interface TokenizerExtension {
|
||||
name: string;
|
||||
level: 'block' | 'inline';
|
||||
start?: ((this: TokenizerThis, src: string) => number | void) | undefined;
|
||||
tokenizer: (this: TokenizerThis, src: string, tokens: Token[] | TokensList) => Tokens.Generic | void;
|
||||
childTokens?: string[] | undefined;
|
||||
}
|
||||
export interface RendererThis {
|
||||
parser: _Parser;
|
||||
}
|
||||
export interface RendererExtension {
|
||||
name: string;
|
||||
renderer: (this: RendererThis, token: Tokens.Generic) => string | false | undefined;
|
||||
}
|
||||
export type TokenizerAndRendererExtension = TokenizerExtension | RendererExtension | (TokenizerExtension & RendererExtension);
|
||||
type RendererApi = Omit<_Renderer, 'constructor' | 'options'>;
|
||||
type RendererObject = {
|
||||
[K in keyof RendererApi]?: (...args: Parameters<RendererApi[K]>) => ReturnType<RendererApi[K]> | false;
|
||||
};
|
||||
type TokenizerApi = Omit<_Tokenizer, 'constructor' | 'options' | 'rules' | 'lexer'>;
|
||||
type TokenizerObject = {
|
||||
[K in keyof TokenizerApi]?: (...args: Parameters<TokenizerApi[K]>) => ReturnType<TokenizerApi[K]> | false;
|
||||
};
|
||||
export interface MarkedExtension {
|
||||
/**
|
||||
* True will tell marked to await any walkTokens functions before parsing the tokens and returning an HTML string.
|
||||
*/
|
||||
async?: boolean;
|
||||
/**
|
||||
* A prefix URL for any relative link.
|
||||
* @deprecated Deprecated in v5.0.0 use marked-base-url to prefix url for any relative link.
|
||||
*/
|
||||
baseUrl?: string | undefined | null;
|
||||
/**
|
||||
* Enable GFM line breaks. This option requires the gfm option to be true.
|
||||
*/
|
||||
breaks?: boolean | undefined;
|
||||
/**
|
||||
* Add tokenizers and renderers to marked
|
||||
*/
|
||||
extensions?: TokenizerAndRendererExtension[] | undefined | null;
|
||||
/**
|
||||
* Enable GitHub flavored markdown.
|
||||
*/
|
||||
gfm?: boolean | undefined;
|
||||
/**
|
||||
* Include an id attribute when emitting headings.
|
||||
* @deprecated Deprecated in v5.0.0 use marked-gfm-heading-id to include an id attribute when emitting headings (h1, h2, h3, etc).
|
||||
*/
|
||||
headerIds?: boolean | undefined;
|
||||
/**
|
||||
* Set the prefix for header tag ids.
|
||||
* @deprecated Deprecated in v5.0.0 use marked-gfm-heading-id to add a string to prefix the id attribute when emitting headings (h1, h2, h3, etc).
|
||||
*/
|
||||
headerPrefix?: string | undefined;
|
||||
/**
|
||||
* A function to highlight code blocks. The function can either be
|
||||
* synchronous (returning a string) or asynchronous (callback invoked
|
||||
* with an error if any occurred during highlighting and a string
|
||||
* 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: 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.
|
||||
* postprocess is called to process html after marked has finished parsing.
|
||||
*/
|
||||
hooks?: {
|
||||
preprocess: (markdown: string) => string;
|
||||
postprocess: (html: string | undefined) => string | undefined;
|
||||
options?: MarkedOptions;
|
||||
} | null;
|
||||
/**
|
||||
* Set the prefix for code block classes.
|
||||
* @deprecated Deprecated in v5.0.0 use marked-highlight to prefix the className in a <code> block. Useful for syntax highlighting.
|
||||
*/
|
||||
langPrefix?: string | undefined;
|
||||
/**
|
||||
* Mangle autolinks (<email@domain.com>).
|
||||
* @deprecated Deprecated in v5.0.0 use marked-mangle to mangle email addresses.
|
||||
*/
|
||||
mangle?: boolean | undefined;
|
||||
/**
|
||||
* Conform to obscure parts of markdown.pl as much as possible. Don't fix any of the original markdown bugs or poor behavior.
|
||||
*/
|
||||
pedantic?: boolean | undefined;
|
||||
/**
|
||||
* Type: object Default: new Renderer()
|
||||
*
|
||||
* An object containing functions to render tokens to HTML.
|
||||
*/
|
||||
renderer?: RendererObject | undefined | null;
|
||||
/**
|
||||
* Sanitize the output. Ignore any HTML that has been input. If true, sanitize the HTML passed into markdownString with the sanitizer function.
|
||||
* @deprecated Warning: This feature is deprecated and it should NOT be used as it cannot be considered secure. Instead use a sanitize library, like DOMPurify (recommended), sanitize-html or insane on the output HTML!
|
||||
*/
|
||||
sanitize?: boolean | undefined;
|
||||
/**
|
||||
* Optionally sanitize found HTML with a sanitizer function.
|
||||
* @deprecated A function to sanitize the HTML passed into markdownString.
|
||||
*/
|
||||
sanitizer?: ((html: string) => string) | null;
|
||||
/**
|
||||
* Shows an HTML error message when rendering fails.
|
||||
*/
|
||||
silent?: boolean | undefined;
|
||||
/**
|
||||
* Use smarter list behavior than the original markdown. May eventually be default with the old behavior moved into pedantic.
|
||||
*/
|
||||
smartLists?: boolean | undefined;
|
||||
/**
|
||||
* Use "smart" typograhic punctuation for things like quotes and dashes.
|
||||
* @deprecated Deprecated in v5.0.0 use marked-smartypants to use "smart" typographic punctuation for things like quotes and dashes.
|
||||
*/
|
||||
smartypants?: boolean | undefined;
|
||||
/**
|
||||
* The tokenizer defines how to turn markdown text into tokens.
|
||||
*/
|
||||
tokenizer?: TokenizerObject | 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>) | 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;
|
||||
}
|
||||
export interface MarkedOptions extends Omit<MarkedExtension, 'extensions' | 'renderer' | 'tokenizer' | 'walkTokens'> {
|
||||
/**
|
||||
* Type: object Default: new Renderer()
|
||||
*
|
||||
* An object containing functions to render tokens to HTML.
|
||||
*/
|
||||
renderer?: Omit<_Renderer, 'constructor'> | undefined | null;
|
||||
/**
|
||||
* 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, (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>;
|
||||
}) | undefined | null;
|
||||
}
|
||||
}
|
||||
declare module "defaults" {
|
||||
import type { MarkedOptions } from "MarkedOptions";
|
||||
/**
|
||||
* Gets the original marked default options.
|
||||
*/
|
||||
export function _getDefaults(): MarkedOptions;
|
||||
export let _defaults: MarkedOptions;
|
||||
export function changeDefaults(newDefaults: MarkedOptions): void;
|
||||
}
|
||||
declare module "Hooks" {
|
||||
import type { MarkedOptions } from "MarkedOptions";
|
||||
export class _Hooks {
|
||||
options: MarkedOptions;
|
||||
constructor(options?: MarkedOptions);
|
||||
static passThroughHooks: Set<string>;
|
||||
/**
|
||||
* Process markdown before marked
|
||||
*/
|
||||
preprocess(markdown: string): string;
|
||||
/**
|
||||
* Process HTML after marked is finished
|
||||
*/
|
||||
postprocess(html: string | undefined): string | undefined;
|
||||
}
|
||||
}
|
||||
|
56
lib/marked.esm.js
generated
56
lib/marked.esm.js
generated
@ -189,12 +189,14 @@ function splitCells(tableRow, count) {
|
||||
if (cells.length > 0 && !cells[cells.length - 1].trim()) {
|
||||
cells.pop();
|
||||
}
|
||||
if (cells.length > count) {
|
||||
cells.splice(count);
|
||||
}
|
||||
else {
|
||||
while (cells.length < count)
|
||||
cells.push('');
|
||||
if (count) {
|
||||
if (cells.length > count) {
|
||||
cells.splice(count);
|
||||
}
|
||||
else {
|
||||
while (cells.length < count)
|
||||
cells.push('');
|
||||
}
|
||||
}
|
||||
for (; i < cells.length; i++) {
|
||||
// leading or trailing whitespace is ignored per the gfm spec
|
||||
@ -627,8 +629,7 @@ class _Tokenizer {
|
||||
if (cap) {
|
||||
const item = {
|
||||
type: 'table',
|
||||
// splitCells expects a number as second argument
|
||||
// @ts-expect-error
|
||||
raw: cap[0],
|
||||
header: splitCells(cap[1]).map(c => {
|
||||
return { text: c };
|
||||
}),
|
||||
@ -636,7 +637,6 @@ class _Tokenizer {
|
||||
rows: cap[3] && cap[3].trim() ? cap[3].replace(/\n[ \t]*$/, '').split('\n') : []
|
||||
};
|
||||
if (item.header.length === item.align.length) {
|
||||
item.raw = cap[0];
|
||||
let l = item.align.length;
|
||||
let i, j, k, row;
|
||||
for (i = 0; i < l; i++) {
|
||||
@ -1406,7 +1406,10 @@ class _Lexer {
|
||||
return leading + ' '.repeat(tabs.length);
|
||||
});
|
||||
}
|
||||
let token, lastToken, cutSrc, lastParagraphClipped;
|
||||
let token;
|
||||
let lastToken;
|
||||
let cutSrc;
|
||||
let lastParagraphClipped;
|
||||
while (src) {
|
||||
if (this.options.extensions
|
||||
&& this.options.extensions.block
|
||||
@ -2256,6 +2259,7 @@ class Marked {
|
||||
default: {
|
||||
if (this.defaults.extensions && this.defaults.extensions.childTokens && this.defaults.extensions.childTokens[token.type]) { // Walk any extensions
|
||||
this.defaults.extensions.childTokens[token.type].forEach((childTokens) => {
|
||||
// @ts-expect-error we assume token[childToken] is an array of tokens but we can't be sure
|
||||
values = values.concat(this.walkTokens(token[childTokens], callback));
|
||||
});
|
||||
}
|
||||
@ -2335,14 +2339,16 @@ class Marked {
|
||||
if (pack.renderer) {
|
||||
const renderer = this.defaults.renderer || new _Renderer(this.defaults);
|
||||
for (const prop in pack.renderer) {
|
||||
const prevRenderer = renderer[prop];
|
||||
const rendererFunc = pack.renderer[prop];
|
||||
const rendererKey = prop;
|
||||
const prevRenderer = renderer[rendererKey];
|
||||
// Replace renderer with func to run extension, but fall back if false
|
||||
renderer[prop] = (...args) => {
|
||||
let ret = pack.renderer[prop].apply(renderer, args);
|
||||
renderer[rendererKey] = (...args) => {
|
||||
let ret = rendererFunc.apply(renderer, args);
|
||||
if (ret === false) {
|
||||
ret = prevRenderer.apply(renderer, args);
|
||||
}
|
||||
return ret;
|
||||
return ret || '';
|
||||
};
|
||||
}
|
||||
opts.renderer = renderer;
|
||||
@ -2350,10 +2356,12 @@ class Marked {
|
||||
if (pack.tokenizer) {
|
||||
const tokenizer = this.defaults.tokenizer || new _Tokenizer(this.defaults);
|
||||
for (const prop in pack.tokenizer) {
|
||||
const prevTokenizer = tokenizer[prop];
|
||||
const tokenizerFunc = pack.tokenizer[prop];
|
||||
const tokenizerKey = prop;
|
||||
const prevTokenizer = tokenizer[tokenizerKey];
|
||||
// Replace tokenizer with func to run extension, but fall back if false
|
||||
tokenizer[prop] = (...args) => {
|
||||
let ret = pack.tokenizer[prop].apply(tokenizer, args);
|
||||
tokenizer[tokenizerKey] = (...args) => {
|
||||
let ret = tokenizerFunc.apply(tokenizer, args);
|
||||
if (ret === false) {
|
||||
ret = prevTokenizer.apply(tokenizer, args);
|
||||
}
|
||||
@ -2366,21 +2374,23 @@ class Marked {
|
||||
if (pack.hooks) {
|
||||
const hooks = this.defaults.hooks || new _Hooks();
|
||||
for (const prop in pack.hooks) {
|
||||
const prevHook = hooks[prop];
|
||||
const hooksFunc = pack.hooks[prop];
|
||||
const hooksKey = prop;
|
||||
const prevHook = hooks[hooksKey];
|
||||
if (_Hooks.passThroughHooks.has(prop)) {
|
||||
hooks[prop] = (arg) => {
|
||||
hooks[hooksKey] = (arg) => {
|
||||
if (this.defaults.async) {
|
||||
return Promise.resolve(pack.hooks[prop].call(hooks, arg)).then(ret => {
|
||||
return Promise.resolve(hooksFunc.call(hooks, arg)).then(ret => {
|
||||
return prevHook.call(hooks, ret);
|
||||
});
|
||||
}
|
||||
const ret = pack.hooks[prop].call(hooks, arg);
|
||||
const ret = hooksFunc.call(hooks, arg);
|
||||
return prevHook.call(hooks, ret);
|
||||
};
|
||||
}
|
||||
else {
|
||||
hooks[prop] = (...args) => {
|
||||
let ret = pack.hooks[prop].apply(hooks, args);
|
||||
hooks[hooksKey] = (...args) => {
|
||||
let ret = hooksFunc.apply(hooks, args);
|
||||
if (ret === false) {
|
||||
ret = prevHook.apply(hooks, args);
|
||||
}
|
||||
|
2
lib/marked.esm.js.map
generated
2
lib/marked.esm.js.map
generated
File diff suppressed because one or more lines are too long
56
lib/marked.umd.js
generated
56
lib/marked.umd.js
generated
@ -195,12 +195,14 @@
|
||||
if (cells.length > 0 && !cells[cells.length - 1].trim()) {
|
||||
cells.pop();
|
||||
}
|
||||
if (cells.length > count) {
|
||||
cells.splice(count);
|
||||
}
|
||||
else {
|
||||
while (cells.length < count)
|
||||
cells.push('');
|
||||
if (count) {
|
||||
if (cells.length > count) {
|
||||
cells.splice(count);
|
||||
}
|
||||
else {
|
||||
while (cells.length < count)
|
||||
cells.push('');
|
||||
}
|
||||
}
|
||||
for (; i < cells.length; i++) {
|
||||
// leading or trailing whitespace is ignored per the gfm spec
|
||||
@ -633,8 +635,7 @@
|
||||
if (cap) {
|
||||
const item = {
|
||||
type: 'table',
|
||||
// splitCells expects a number as second argument
|
||||
// @ts-expect-error
|
||||
raw: cap[0],
|
||||
header: splitCells(cap[1]).map(c => {
|
||||
return { text: c };
|
||||
}),
|
||||
@ -642,7 +643,6 @@
|
||||
rows: cap[3] && cap[3].trim() ? cap[3].replace(/\n[ \t]*$/, '').split('\n') : []
|
||||
};
|
||||
if (item.header.length === item.align.length) {
|
||||
item.raw = cap[0];
|
||||
let l = item.align.length;
|
||||
let i, j, k, row;
|
||||
for (i = 0; i < l; i++) {
|
||||
@ -1412,7 +1412,10 @@
|
||||
return leading + ' '.repeat(tabs.length);
|
||||
});
|
||||
}
|
||||
let token, lastToken, cutSrc, lastParagraphClipped;
|
||||
let token;
|
||||
let lastToken;
|
||||
let cutSrc;
|
||||
let lastParagraphClipped;
|
||||
while (src) {
|
||||
if (this.options.extensions
|
||||
&& this.options.extensions.block
|
||||
@ -2262,6 +2265,7 @@
|
||||
default: {
|
||||
if (this.defaults.extensions && this.defaults.extensions.childTokens && this.defaults.extensions.childTokens[token.type]) { // Walk any extensions
|
||||
this.defaults.extensions.childTokens[token.type].forEach((childTokens) => {
|
||||
// @ts-expect-error we assume token[childToken] is an array of tokens but we can't be sure
|
||||
values = values.concat(this.walkTokens(token[childTokens], callback));
|
||||
});
|
||||
}
|
||||
@ -2341,14 +2345,16 @@
|
||||
if (pack.renderer) {
|
||||
const renderer = this.defaults.renderer || new _Renderer(this.defaults);
|
||||
for (const prop in pack.renderer) {
|
||||
const prevRenderer = renderer[prop];
|
||||
const rendererFunc = pack.renderer[prop];
|
||||
const rendererKey = prop;
|
||||
const prevRenderer = renderer[rendererKey];
|
||||
// Replace renderer with func to run extension, but fall back if false
|
||||
renderer[prop] = (...args) => {
|
||||
let ret = pack.renderer[prop].apply(renderer, args);
|
||||
renderer[rendererKey] = (...args) => {
|
||||
let ret = rendererFunc.apply(renderer, args);
|
||||
if (ret === false) {
|
||||
ret = prevRenderer.apply(renderer, args);
|
||||
}
|
||||
return ret;
|
||||
return ret || '';
|
||||
};
|
||||
}
|
||||
opts.renderer = renderer;
|
||||
@ -2356,10 +2362,12 @@
|
||||
if (pack.tokenizer) {
|
||||
const tokenizer = this.defaults.tokenizer || new _Tokenizer(this.defaults);
|
||||
for (const prop in pack.tokenizer) {
|
||||
const prevTokenizer = tokenizer[prop];
|
||||
const tokenizerFunc = pack.tokenizer[prop];
|
||||
const tokenizerKey = prop;
|
||||
const prevTokenizer = tokenizer[tokenizerKey];
|
||||
// Replace tokenizer with func to run extension, but fall back if false
|
||||
tokenizer[prop] = (...args) => {
|
||||
let ret = pack.tokenizer[prop].apply(tokenizer, args);
|
||||
tokenizer[tokenizerKey] = (...args) => {
|
||||
let ret = tokenizerFunc.apply(tokenizer, args);
|
||||
if (ret === false) {
|
||||
ret = prevTokenizer.apply(tokenizer, args);
|
||||
}
|
||||
@ -2372,21 +2380,23 @@
|
||||
if (pack.hooks) {
|
||||
const hooks = this.defaults.hooks || new _Hooks();
|
||||
for (const prop in pack.hooks) {
|
||||
const prevHook = hooks[prop];
|
||||
const hooksFunc = pack.hooks[prop];
|
||||
const hooksKey = prop;
|
||||
const prevHook = hooks[hooksKey];
|
||||
if (_Hooks.passThroughHooks.has(prop)) {
|
||||
hooks[prop] = (arg) => {
|
||||
hooks[hooksKey] = (arg) => {
|
||||
if (this.defaults.async) {
|
||||
return Promise.resolve(pack.hooks[prop].call(hooks, arg)).then(ret => {
|
||||
return Promise.resolve(hooksFunc.call(hooks, arg)).then(ret => {
|
||||
return prevHook.call(hooks, ret);
|
||||
});
|
||||
}
|
||||
const ret = pack.hooks[prop].call(hooks, arg);
|
||||
const ret = hooksFunc.call(hooks, arg);
|
||||
return prevHook.call(hooks, ret);
|
||||
};
|
||||
}
|
||||
else {
|
||||
hooks[prop] = (...args) => {
|
||||
let ret = pack.hooks[prop].apply(hooks, args);
|
||||
hooks[hooksKey] = (...args) => {
|
||||
let ret = hooksFunc.apply(hooks, args);
|
||||
if (ret === false) {
|
||||
ret = prevHook.apply(hooks, args);
|
||||
}
|
||||
|
2
lib/marked.umd.js.map
generated
2
lib/marked.umd.js.map
generated
File diff suppressed because one or more lines are too long
4
marked.min.js
generated
vendored
4
marked.min.js
generated
vendored
File diff suppressed because one or more lines are too long
6
package-lock.json
generated
6
package-lock.json
generated
@ -10490,9 +10490,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/tslib": {
|
||||
"version": "1.14.1",
|
||||
"resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz",
|
||||
"integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==",
|
||||
"version": "2.6.1",
|
||||
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.1.tgz",
|
||||
"integrity": "sha512-t0hLfiEKfMUoqhG+U1oid7Pva4bbDPHYfJNiB7BiIjRkj1pyC++4N3huJfqY6aRH6VTB0rvtzQwjM4K6qpfOig==",
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"peer": true
|
||||
|
@ -89,7 +89,7 @@
|
||||
"bench": "npm run build && node test/bench.js",
|
||||
"lint": "eslint --fix .",
|
||||
"build:reset": "git checkout upstream/master lib/marked.cjs lib/marked.umd.js lib/marked.esm.js marked.min.js",
|
||||
"build": "npm run rollup && npm run minify",
|
||||
"build": "npm run rollup && npm run minify && npm run build:types",
|
||||
"build:docs": "node build-docs.js",
|
||||
"build:types": "tsc --project tsconfig-types.json",
|
||||
"rollup": "rollup -c rollup.config.js",
|
||||
|
@ -23,7 +23,7 @@ export class _Hooks {
|
||||
/**
|
||||
* Process HTML after marked is finished
|
||||
*/
|
||||
postprocess(html: string | undefined) {
|
||||
postprocess(html: string) {
|
||||
return html;
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { _getDefaults } from './defaults.js';
|
||||
import { _getDefaults } from './defaults.ts';
|
||||
import { _Lexer } from './Lexer.ts';
|
||||
import { _Parser } from './Parser.ts';
|
||||
import { _Hooks } from './Hooks.ts';
|
||||
@ -15,6 +15,9 @@ import type { Token, TokensList } from './Tokens.ts';
|
||||
|
||||
export type ResultCallback = (error: Error | null, parseResult?: string) => undefined | void;
|
||||
|
||||
type UnknownFunction = (...args: unknown[]) => unknown;
|
||||
type GenericRendererFunction = (...args: unknown[]) => string | false;
|
||||
|
||||
export class Marked {
|
||||
defaults = _getDefaults();
|
||||
options = this.setOptions;
|
||||
@ -62,6 +65,7 @@ export class Marked {
|
||||
default: {
|
||||
if (this.defaults.extensions && this.defaults.extensions.childTokens && this.defaults.extensions.childTokens[token.type]) { // Walk any extensions
|
||||
this.defaults.extensions.childTokens[token.type].forEach((childTokens) => {
|
||||
// @ts-expect-error we assume token[childToken] is an array of tokens but we can't be sure
|
||||
values = values.concat(this.walkTokens(token[childTokens], callback));
|
||||
});
|
||||
} else if (token.tokens) {
|
||||
@ -140,14 +144,16 @@ export class Marked {
|
||||
if (pack.renderer) {
|
||||
const renderer = this.defaults.renderer || new _Renderer(this.defaults);
|
||||
for (const prop in pack.renderer) {
|
||||
const prevRenderer = renderer[prop];
|
||||
const rendererFunc = pack.renderer[prop as keyof MarkedExtension['renderer']] as GenericRendererFunction;
|
||||
const rendererKey = prop as keyof _Renderer;
|
||||
const prevRenderer = renderer[rendererKey] as GenericRendererFunction;
|
||||
// Replace renderer with func to run extension, but fall back if false
|
||||
renderer[prop] = (...args: unknown[]) => {
|
||||
let ret = pack.renderer![prop].apply(renderer, args);
|
||||
renderer[rendererKey] = (...args: unknown[]) => {
|
||||
let ret = rendererFunc.apply(renderer, args);
|
||||
if (ret === false) {
|
||||
ret = prevRenderer.apply(renderer, args);
|
||||
}
|
||||
return ret;
|
||||
return ret || '';
|
||||
};
|
||||
}
|
||||
opts.renderer = renderer;
|
||||
@ -155,10 +161,12 @@ export class Marked {
|
||||
if (pack.tokenizer) {
|
||||
const tokenizer = this.defaults.tokenizer || new _Tokenizer(this.defaults);
|
||||
for (const prop in pack.tokenizer) {
|
||||
const prevTokenizer = tokenizer[prop];
|
||||
const tokenizerFunc = pack.tokenizer[prop as keyof MarkedExtension['tokenizer']] as UnknownFunction;
|
||||
const tokenizerKey = prop as keyof _Tokenizer;
|
||||
const prevTokenizer = tokenizer[tokenizerKey] as UnknownFunction;
|
||||
// Replace tokenizer with func to run extension, but fall back if false
|
||||
tokenizer[prop] = (...args: unknown[]) => {
|
||||
let ret = pack.tokenizer![prop].apply(tokenizer, args);
|
||||
tokenizer[tokenizerKey] = (...args: unknown[]) => {
|
||||
let ret = tokenizerFunc.apply(tokenizer, args);
|
||||
if (ret === false) {
|
||||
ret = prevTokenizer.apply(tokenizer, args);
|
||||
}
|
||||
@ -172,25 +180,27 @@ export class Marked {
|
||||
if (pack.hooks) {
|
||||
const hooks = this.defaults.hooks || new _Hooks();
|
||||
for (const prop in pack.hooks) {
|
||||
const prevHook = hooks[prop];
|
||||
const hooksFunc = pack.hooks[prop as keyof MarkedExtension['hooks']] as UnknownFunction;
|
||||
const hooksKey = prop as keyof _Hooks;
|
||||
const prevHook = hooks[hooksKey] as UnknownFunction;
|
||||
if (_Hooks.passThroughHooks.has(prop)) {
|
||||
hooks[prop as 'preprocess' | 'postprocess'] = (arg: string | undefined) => {
|
||||
hooks[hooksKey as 'preprocess' | 'postprocess'] = (arg: string | undefined) => {
|
||||
if (this.defaults.async) {
|
||||
return Promise.resolve(pack.hooks![prop].call(hooks, arg)).then(ret => {
|
||||
return prevHook.call(hooks, ret);
|
||||
return Promise.resolve(hooksFunc.call(hooks, arg)).then(ret => {
|
||||
return prevHook.call(hooks, ret) as string;
|
||||
});
|
||||
}
|
||||
|
||||
const ret = pack.hooks![prop].call(hooks, arg);
|
||||
return prevHook.call(hooks, ret);
|
||||
const ret = hooksFunc.call(hooks, arg);
|
||||
return prevHook.call(hooks, ret) as string;
|
||||
};
|
||||
} else {
|
||||
hooks[prop] = (...args) => {
|
||||
let ret = pack.hooks![prop].apply(hooks, args);
|
||||
hooks[hooksKey] = (...args: unknown[]) => {
|
||||
let ret = hooksFunc.apply(hooks, args);
|
||||
if (ret === false) {
|
||||
ret = prevHook.apply(hooks, args);
|
||||
}
|
||||
return ret;
|
||||
return ret as string;
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -216,12 +226,12 @@ export class Marked {
|
||||
return this;
|
||||
}
|
||||
|
||||
setOptions(opt) {
|
||||
setOptions(opt: MarkedOptions) {
|
||||
this.defaults = { ...this.defaults, ...opt };
|
||||
return this;
|
||||
}
|
||||
|
||||
#parseMarkdown(lexer: (src: string, options?: MarkedOptions) => TokensList | Token[], parser: (tokens: Token[], options?: MarkedOptions) => string | undefined) {
|
||||
#parseMarkdown(lexer: (src: string, options?: MarkedOptions) => TokensList | Token[], parser: (tokens: Token[], options?: MarkedOptions) => string) {
|
||||
return (src: string, optOrCallback?: MarkedOptions | ResultCallback | undefined | null, callback?: ResultCallback | undefined): string | Promise<string | undefined> | undefined => {
|
||||
if (typeof optOrCallback === 'function') {
|
||||
callback = optOrCallback;
|
||||
@ -253,7 +263,7 @@ export class Marked {
|
||||
|
||||
try {
|
||||
if (opt.hooks) {
|
||||
src = opt.hooks.preprocess(src);
|
||||
src = opt.hooks.preprocess(src) as string;
|
||||
}
|
||||
tokens = lexer(src, opt);
|
||||
} catch (e) {
|
||||
@ -270,7 +280,7 @@ export class Marked {
|
||||
}
|
||||
out = parser(tokens, opt)!;
|
||||
if (opt.hooks) {
|
||||
out = opt.hooks.postprocess(out);
|
||||
out = opt.hooks.postprocess(out) as string;
|
||||
}
|
||||
} catch (e) {
|
||||
err = e as Error;
|
||||
@ -333,7 +343,7 @@ export class Marked {
|
||||
|
||||
try {
|
||||
if (opt.hooks) {
|
||||
src = opt.hooks.preprocess(src);
|
||||
src = opt.hooks.preprocess(src) as string;
|
||||
}
|
||||
const tokens = lexer(src, opt);
|
||||
if (opt.walkTokens) {
|
||||
@ -341,7 +351,7 @@ export class Marked {
|
||||
}
|
||||
let html = parser(tokens, opt);
|
||||
if (opt.hooks) {
|
||||
html = opt.hooks.postprocess(html);
|
||||
html = opt.hooks.postprocess(html) as string;
|
||||
}
|
||||
return html;
|
||||
} catch (e) {
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { _Tokenizer } from './Tokenizer.ts';
|
||||
import { _defaults } from './defaults.ts';
|
||||
import { block, inline } from './rules.ts';
|
||||
import type { Token, TokensList } from './Tokens.ts';
|
||||
import type { Token, TokensList, Tokens } from './Tokens.ts';
|
||||
import type { MarkedOptions, TokenizerExtension } from './MarkedOptions.ts';
|
||||
import type { Rules } from './rules.ts';
|
||||
|
||||
@ -154,7 +154,10 @@ export class _Lexer {
|
||||
});
|
||||
}
|
||||
|
||||
let token, lastToken, cutSrc, lastParagraphClipped;
|
||||
let token: Tokens.Generic | undefined;
|
||||
let lastToken;
|
||||
let cutSrc;
|
||||
let lastParagraphClipped;
|
||||
|
||||
while (src) {
|
||||
if (this.options.extensions
|
||||
|
@ -17,7 +17,7 @@ export interface TokenizerExtension {
|
||||
name: string;
|
||||
level: 'block' | 'inline';
|
||||
start?: ((this: TokenizerThis, src: string) => number | void) | undefined;
|
||||
tokenizer: (this: TokenizerThis, src: string, tokens: Token[] | TokensList) => Tokens.Generic | void;
|
||||
tokenizer: (this: TokenizerThis, src: string, tokens: Token[] | TokensList) => Tokens.Generic | undefined;
|
||||
childTokens?: string[] | undefined;
|
||||
}
|
||||
|
||||
@ -25,9 +25,11 @@ export interface RendererThis {
|
||||
parser: _Parser;
|
||||
}
|
||||
|
||||
export type RendererExtensionFunction = (this: RendererThis, token: Tokens.Generic) => string | false | undefined;
|
||||
|
||||
export interface RendererExtension {
|
||||
name: string;
|
||||
renderer: (this: RendererThis, token: Tokens.Generic) => string | false | undefined;
|
||||
renderer: RendererExtensionFunction;
|
||||
}
|
||||
|
||||
export type TokenizerAndRendererExtension = TokenizerExtension | RendererExtension | (TokenizerExtension & RendererExtension);
|
||||
@ -98,8 +100,8 @@ export interface MarkedExtension {
|
||||
* postprocess is called to process html after marked has finished parsing.
|
||||
*/
|
||||
hooks?: {
|
||||
preprocess: (markdown: string) => string,
|
||||
postprocess: (html: string | undefined) => string | undefined,
|
||||
preprocess: (markdown: string) => string | Promise<string>,
|
||||
postprocess: (html: string) => string | Promise<string>,
|
||||
// eslint-disable-next-line no-use-before-define
|
||||
options?: MarkedOptions
|
||||
} | null;
|
||||
@ -201,7 +203,7 @@ export interface MarkedOptions extends Omit<MarkedExtension, 'extensions' | 'ren
|
||||
*/
|
||||
extensions?:
|
||||
| (TokenizerAndRendererExtension[] & {
|
||||
renderers: Record<string, (this: RendererThis, token: Tokens.Generic) => string | false | undefined>,
|
||||
renderers: Record<string, RendererExtensionFunction>,
|
||||
childTokens: Record<string, string[]>,
|
||||
block: any[],
|
||||
inline: any[],
|
||||
|
@ -89,9 +89,9 @@ export class _Parser {
|
||||
}
|
||||
case 'heading': {
|
||||
out += this.renderer.heading(
|
||||
this.parseInline(token.tokens) as string,
|
||||
this.parseInline(token.tokens!),
|
||||
token.depth,
|
||||
unescape(this.parseInline(token.tokens, this.textRenderer) as string),
|
||||
unescape(this.parseInline(token.tokens!, this.textRenderer)),
|
||||
this.slugger);
|
||||
continue;
|
||||
}
|
||||
@ -135,7 +135,7 @@ export class _Parser {
|
||||
continue;
|
||||
}
|
||||
case 'blockquote': {
|
||||
body = this.parse(token.tokens)!;
|
||||
body = this.parse(token.tokens!);
|
||||
out += this.renderer.blockquote(body);
|
||||
continue;
|
||||
}
|
||||
@ -183,13 +183,13 @@ export class _Parser {
|
||||
continue;
|
||||
}
|
||||
case 'paragraph': {
|
||||
out += this.renderer.paragraph(this.parseInline(token.tokens)!);
|
||||
out += this.renderer.paragraph(this.parseInline(token.tokens!)!);
|
||||
continue;
|
||||
}
|
||||
case 'text': {
|
||||
body = token.tokens ? this.parseInline(token.tokens) : token.text;
|
||||
while (i + 1 < l && tokens[i + 1].type === 'text') {
|
||||
token = tokens[++i];
|
||||
token = tokens[++i] as Tokens.Text;
|
||||
body += '\n' + (token.tokens ? this.parseInline(token.tokens) : token.text);
|
||||
}
|
||||
out += top ? this.renderer.paragraph(body!) : body;
|
||||
@ -244,7 +244,7 @@ export class _Parser {
|
||||
break;
|
||||
}
|
||||
case 'link': {
|
||||
out += renderer.link(token.href, token.title, this.parseInline(token.tokens, renderer)!);
|
||||
out += renderer.link(token.href, token.title, this.parseInline(token.tokens!, renderer)!);
|
||||
break;
|
||||
}
|
||||
case 'image': {
|
||||
@ -252,11 +252,11 @@ export class _Parser {
|
||||
break;
|
||||
}
|
||||
case 'strong': {
|
||||
out += renderer.strong(this.parseInline(token.tokens, renderer)!);
|
||||
out += renderer.strong(this.parseInline(token.tokens!, renderer)!);
|
||||
break;
|
||||
}
|
||||
case 'em': {
|
||||
out += renderer.em(this.parseInline(token.tokens, renderer)!);
|
||||
out += renderer.em(this.parseInline(token.tokens!, renderer)!);
|
||||
break;
|
||||
}
|
||||
case 'codespan': {
|
||||
@ -268,7 +268,7 @@ export class _Parser {
|
||||
break;
|
||||
}
|
||||
case 'del': {
|
||||
out += renderer.del(this.parseInline(token.tokens, renderer)!);
|
||||
out += renderer.del(this.parseInline(token.tokens!, renderer)!);
|
||||
break;
|
||||
}
|
||||
case 'text': {
|
||||
|
@ -407,8 +407,7 @@ export class _Tokenizer {
|
||||
if (cap) {
|
||||
const item: Tokens.Table = {
|
||||
type: 'table',
|
||||
// splitCells expects a number as second argument
|
||||
// @ts-expect-error
|
||||
raw: cap[0],
|
||||
header: splitCells(cap[1]).map(c => {
|
||||
return { text: c };
|
||||
}),
|
||||
@ -417,8 +416,6 @@ export class _Tokenizer {
|
||||
};
|
||||
|
||||
if (item.header.length === item.align.length) {
|
||||
item.raw = cap[0];
|
||||
|
||||
let l = item.align.length;
|
||||
let i, j, k, row;
|
||||
for (i = 0; i < l; i++) {
|
||||
|
@ -19,7 +19,8 @@ export type Token = (Tokens.Space
|
||||
| Tokens.Em
|
||||
| Tokens.Codespan
|
||||
| Tokens.Br
|
||||
| Tokens.Del) & { loose?: boolean, tokens?: Token[] };
|
||||
| Tokens.Del
|
||||
| Tokens.Generic) & { loose?: boolean, tokens?: Token[] };
|
||||
|
||||
export namespace Tokens {
|
||||
export interface Space {
|
||||
@ -46,7 +47,7 @@ export namespace Tokens {
|
||||
|
||||
export interface Table {
|
||||
type: 'table';
|
||||
raw?: string;
|
||||
raw: string;
|
||||
align: Array<'center' | 'left' | 'right' | null>;
|
||||
header: TableCell[];
|
||||
rows: TableCell[][];
|
||||
|
@ -9,7 +9,7 @@ const escapeTest = /[&<>"']/;
|
||||
const escapeReplace = new RegExp(escapeTest.source, 'g');
|
||||
const escapeTestNoEncode = /[<>"']|&(?!(#\d{1,7}|#[Xx][a-fA-F0-9]{1,6}|\w+);)/;
|
||||
const escapeReplaceNoEncode = new RegExp(escapeTestNoEncode.source, 'g');
|
||||
const escapeReplacements = {
|
||||
const escapeReplacements: {[index: string]: string} = {
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
@ -131,7 +131,7 @@ export function resolveUrl(base: string, href: string) {
|
||||
|
||||
export const noopTest = { exec: () => null };
|
||||
|
||||
export function splitCells(tableRow: string, count: number) {
|
||||
export function splitCells(tableRow: string, count?: number) {
|
||||
// ensure that every cell-delimiting pipe has a space
|
||||
// before it to distinguish it from an escaped pipe
|
||||
const row = tableRow.replace(/\|/g, (match, offset, str) => {
|
||||
@ -158,10 +158,12 @@ export function splitCells(tableRow: string, count: number) {
|
||||
cells.pop();
|
||||
}
|
||||
|
||||
if (cells.length > count) {
|
||||
cells.splice(count);
|
||||
} else {
|
||||
while (cells.length < count) cells.push('');
|
||||
if (count) {
|
||||
if (cells.length > count) {
|
||||
cells.splice(count);
|
||||
} else {
|
||||
while (cells.length < count) cells.push('');
|
||||
}
|
||||
}
|
||||
|
||||
for (; i < cells.length; i++) {
|
||||
|
@ -186,7 +186,7 @@ marked.use({
|
||||
}
|
||||
});
|
||||
|
||||
interface NameToken {
|
||||
interface NameToken extends Tokens.Generic {
|
||||
type: 'name';
|
||||
raw: string;
|
||||
text: string;
|
||||
@ -198,7 +198,7 @@ const tokenizerExtension: TokenizerExtension = {
|
||||
name: 'name',
|
||||
level: 'block',
|
||||
start: (src: string) => src.match(/name/)?.index,
|
||||
tokenizer(src: string): NameToken | void {
|
||||
tokenizer(src: string): NameToken | undefined {
|
||||
if (src === 'name') {
|
||||
const token: NameToken = {
|
||||
type: 'name',
|
||||
|
@ -8,7 +8,6 @@
|
||||
"noEmit": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"moduleResolution": "NodeNext",
|
||||
"noImplicitAny": false,
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"marked": [
|
||||
|
@ -7,7 +7,6 @@
|
||||
"emitDeclarationOnly": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"moduleResolution": "NodeNext",
|
||||
"noImplicitAny": false,
|
||||
"allowImportingTsExtensions": true,
|
||||
"outFile": "lib/marked.d.ts"
|
||||
},
|
||||
|
@ -8,7 +8,6 @@
|
||||
"noEmit": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"moduleResolution": "NodeNext",
|
||||
"noImplicitAny": false,
|
||||
"allowImportingTsExtensions": true,
|
||||
"sourceMap": false
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user