discord-markdown-parser
Version:
Parse discord-style markdown into an abstract syntax tree.
151 lines (150 loc) • 6.7 kB
TypeScript
/**
* Simple-Markdown
* ===============
*
* Simple-Markdown's primary goal is to be easy to adapt. It aims
* to be compliant with John Gruber's [Markdown Syntax page][1],
* but compatiblity with other markdown implementations' edge-cases
* will be sacrificed where it conflicts with simplicity or
* extensibility.
*
* If your goal is to simply embed a standard markdown implementation
* in your website, simple-markdown is probably not the best library
* for you (although it should work). But if you have struggled to
* customize an existing library to meet your needs, simple-markdown
* might be able to help.
*
* Many of the regexes and original logic has been adapted from
* the wonderful [marked.js](https://github.com/chjj/marked)
*/
import type { Capture, MatchFunction, State } from './troublesome-types';
type Attr = string | number | boolean | null | undefined;
type SingleASTNode = {
type: string;
[key: string]: any;
};
type UnTypedASTNode = {
[key: string]: any;
};
type ASTNode = SingleASTNode | Array<SingleASTNode>;
type Parser = (source: string, state?: State | null | undefined) => Array<SingleASTNode>;
type ParseFunction = (capture: Capture, nestedParse: Parser, state: State) => UnTypedASTNode | ASTNode;
type SingleNodeParseFunction = (capture: Capture, nestedParse: Parser, state: State) => UnTypedASTNode;
type Output<Result> = (node: ASTNode, state?: State | null | undefined) => Result;
type NodeOutput<Result> = (node: SingleASTNode, nestedOutput: Output<Result>, state: State) => Result;
type ArrayNodeOutput<Result> = (node: Array<SingleASTNode>, nestedOutput: Output<Result>, state: State) => Result;
type HtmlOutput = Output<string>;
type HtmlNodeOutput = NodeOutput<string>;
type ParserRule = {
readonly order: number;
readonly match: MatchFunction;
readonly quality?: (capture: Capture, state: State, prevCapture: string) => number;
readonly parse: ParseFunction;
};
type SingleNodeParserRule = {
readonly order: number;
readonly match: MatchFunction;
readonly quality?: (capture: Capture, state: State, prevCapture: string) => number;
readonly parse: SingleNodeParseFunction;
};
type HtmlOutputRule = {
readonly html: HtmlNodeOutput | null;
};
type ArrayRule = {
readonly html: ArrayNodeOutput<string>;
readonly [key: string]: ArrayNodeOutput<any>;
};
type ParserRules = {
readonly Array?: ArrayRule;
readonly [type: string]: ParserRule;
};
type OutputRules<Rule> = {
readonly Array?: ArrayRule;
readonly [type: string]: Rule;
};
type Rules<OutputRule> = {
readonly Array?: ArrayRule;
readonly [type: string]: ParserRule & OutputRule;
};
type HtmlRules = {
readonly Array?: {
readonly html: ArrayNodeOutput<string>;
};
readonly [type: string]: ParserRule & HtmlOutputRule;
};
type NonNullHtmlOutputRule = {
readonly html: HtmlNodeOutput;
};
type DefaultInRule = SingleNodeParserRule & HtmlOutputRule;
type TextInOutRule = SingleNodeParserRule & NonNullHtmlOutputRule;
type LenientInOutRule = SingleNodeParserRule & NonNullHtmlOutputRule;
type DefaultInOutRule = SingleNodeParserRule & NonNullHtmlOutputRule;
type DefaultRules = {
readonly Array: {
readonly html: ArrayNodeOutput<string>;
};
readonly heading: DefaultInOutRule;
readonly nptable: DefaultInRule;
readonly lheading: DefaultInRule;
readonly hr: DefaultInOutRule;
readonly codeBlock: DefaultInOutRule;
readonly fence: DefaultInRule;
readonly blockQuote: DefaultInOutRule;
readonly list: DefaultInOutRule;
readonly def: LenientInOutRule;
readonly table: DefaultInOutRule;
readonly tableSeparator: DefaultInRule;
readonly newline: TextInOutRule;
readonly paragraph: DefaultInOutRule;
readonly escape: DefaultInRule;
readonly autolink: DefaultInRule;
readonly mailto: DefaultInRule;
readonly url: DefaultInRule;
readonly link: DefaultInOutRule;
readonly image: DefaultInOutRule;
readonly reflink: DefaultInRule;
readonly refimage: DefaultInRule;
readonly em: DefaultInOutRule;
readonly strong: DefaultInOutRule;
readonly u: DefaultInOutRule;
readonly del: DefaultInOutRule;
readonly inlineCode: DefaultInOutRule;
readonly br: DefaultInOutRule;
readonly text: TextInOutRule;
};
type Exports = {
readonly defaultRules: DefaultRules;
readonly parserFor: (rules: ParserRules, defaultState?: State | null | undefined) => Parser;
readonly outputFor: <Rule>(rules: OutputRules<Rule>, param: keyof Rule, defaultState?: State | null | undefined) => Output<any>;
readonly ruleOutput: <Rule>(rules: OutputRules<Rule>, param: keyof Rule) => NodeOutput<any>;
readonly htmlFor: (arg1: HtmlNodeOutput) => HtmlOutput;
readonly inlineRegex: (regex: RegExp) => MatchFunction;
readonly blockRegex: (regex: RegExp) => MatchFunction;
readonly anyScopeRegex: (regex: RegExp) => MatchFunction;
readonly parseInline: (parse: Parser, content: string, state: State) => ASTNode;
readonly parseBlock: (parse: Parser, content: string, state: State) => ASTNode;
readonly markdownToHtml: (source: string, state?: State | null | undefined) => string;
readonly defaultRawParse: (source: string, state?: State | null | undefined) => Array<SingleASTNode>;
readonly defaultBlockParse: (source: string, state?: State | null | undefined) => Array<SingleASTNode>;
readonly defaultInlineParse: (source: string, state?: State | null | undefined) => Array<SingleASTNode>;
readonly defaultImplicitParse: (source: string, state?: State | null | undefined) => Array<SingleASTNode>;
readonly defaultHtmlOutput: HtmlOutput;
readonly preprocess: (source: string) => string;
readonly sanitizeText: (text: Attr) => string;
readonly sanitizeUrl: (url?: string | null | undefined) => string | null | undefined;
readonly unescapeUrl: (url: string) => string;
readonly htmlTag: (tagName: string, content: string, attributes?: Partial<Record<any, Attr | null | undefined>> | null | undefined, isClosed?: boolean | null | undefined) => string;
/**
* defaultParse is deprecated, please use `defaultImplicitParse`
* @deprecated
*/
readonly defaultParse: (...args: any[]) => any;
/**
* defaultOutput is deprecated, please use `defaultReactOutput`
* @deprecated
*/
readonly defaultOutput: (...args: any[]) => any;
};
export type { State, Parser, Output, HtmlOutput, Capture, MatchFunction, ParseFunction, NodeOutput, ArrayNodeOutput, ParserRule, HtmlOutputRule, ParserRules, OutputRules, Rules, HtmlRules, SingleASTNode, };
declare var SimpleMarkdown: Exports;
export default SimpleMarkdown;