1 | type Maybe<T> = void | T;
|
2 | type MaybeArray<T> = T | T[];
|
3 |
|
4 | declare namespace PostHTML {
|
5 | type StringMatcher = string | RegExp;
|
6 | type AttrMatcher = Record<string, StringMatcher>;
|
7 | type ContentMatcher =
|
8 | | StringMatcher[]
|
9 | | {
|
10 | tag?: StringMatcher;
|
11 | attrs?: AttrMatcher;
|
12 | content?: ContentMatcher[];
|
13 | };
|
14 |
|
15 | export type Matcher<
|
16 | TTag extends StringMatcher,
|
17 | TAttrs extends Maybe<AttrMatcher>
|
18 | > =
|
19 | | StringMatcher
|
20 | | {
|
21 | tag?: TTag;
|
22 | attrs?: TAttrs;
|
23 | content?: ContentMatcher[];
|
24 | };
|
25 |
|
26 | export type Expression<
|
27 | TTag extends StringMatcher,
|
28 | TAttrs extends Maybe<AttrMatcher>
|
29 | > = MaybeArray<Matcher<TTag, TAttrs>>;
|
30 |
|
31 | export type NodeCallback<
|
32 | TTag extends Maybe<string> = Maybe<string>,
|
33 | TAttrs extends Maybe<NodeAttributes> = Maybe<NodeAttributes>
|
34 | > = (node: Node<TTag, TAttrs>) => MaybeArray<Node | RawNode>;
|
35 |
|
36 | export type NodeAttributes = Record<string, Maybe<string>>;
|
37 |
|
38 | interface NodeAPI {
|
39 | walk: (cb: NodeCallback) => Node;
|
40 | match: <
|
41 | TTag extends StringMatcher,
|
42 | TAttrs extends Maybe<AttrMatcher>,
|
43 | TTagResult extends Maybe<string> = TTag extends string
|
44 | ? TTag
|
45 | : TTag extends void
|
46 | ? Maybe<string>
|
47 | : string,
|
48 | TAttrResult extends Maybe<NodeAttributes> = TAttrs extends void
|
49 | ? Maybe<NodeAttributes>
|
50 | : {
|
51 | [P in keyof TAttrs]: string;
|
52 | } &
|
53 | NodeAttributes
|
54 | >(
|
55 | expression: Expression<TTag, TAttrs>,
|
56 | cb: NodeCallback<TTagResult, TAttrResult>
|
57 | ) => Node<TTagResult, TAttrResult>[];
|
58 | }
|
59 |
|
60 | export interface RawNode<
|
61 | TTag extends Maybe<string> = Maybe<string>,
|
62 | TAttrs extends Maybe<NodeAttributes> = Maybe<NodeAttributes>
|
63 | > {
|
64 | tag: TTag;
|
65 | attrs: TAttrs;
|
66 | content?: Array<string | RawNode>;
|
67 | }
|
68 |
|
69 | export interface Node<
|
70 | TTag extends Maybe<string> = Maybe<string>,
|
71 | TAttrs extends Maybe<NodeAttributes> = Maybe<NodeAttributes>
|
72 | > extends NodeAPI, RawNode<TTag, TAttrs> {
|
73 | content?: Array<string | Node>;
|
74 | }
|
75 |
|
76 | export interface Options {
|
77 | sync?: boolean;
|
78 | parser?: Function;
|
79 | render?: Function;
|
80 | skipParse?: boolean;
|
81 | }
|
82 |
|
83 | export type Plugin<TThis> = (
|
84 | tree: Node
|
85 | ) => void | Node | RawNode | ThisType<TThis>;
|
86 |
|
87 | export interface Result<TMessage> {
|
88 | html: string;
|
89 | tree: Node;
|
90 | messages: TMessage[];
|
91 | }
|
92 |
|
93 | export interface PostHTML<TThis, TMessage> {
|
94 | version: string;
|
95 | name: "";
|
96 | plugins: Plugin<TThis>[];
|
97 | messages: TMessage[];
|
98 | use<TThis>(plugins: MaybeArray<Plugin<TThis>>): this;
|
99 | process(html: string, options?: Options): Promise<Result<TMessage>>;
|
100 | }
|
101 | }
|
102 |
|
103 | declare function PostHTML<TThis, TMessage>(
|
104 | plugins?: PostHTML.Plugin<TThis>[]
|
105 | ): PostHTML.PostHTML<TThis, TMessage>;
|
106 |
|
107 | export = PostHTML;
|