summaryrefslogtreecommitdiff
path: root/node_modules/posthtml/types/posthtml.d.ts
blob: 0088dd2fd1b3232c775bc2db76cd3ad376581fa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
type Maybe<T> = void | T;
type MaybeArray<T> = T | T[];

declare namespace PostHTML {
  type StringMatcher = string | RegExp;
  type AttrMatcher = Record<string, StringMatcher>;
  type ContentMatcher =
    | StringMatcher[]
    | {
        tag?: StringMatcher;
        attrs?: AttrMatcher;
        content?: ContentMatcher[];
      };

  export type Matcher<
    TTag extends StringMatcher,
    TAttrs extends Maybe<AttrMatcher>
  > =
    | StringMatcher
    | {
        tag?: TTag;
        attrs?: TAttrs;
        content?: ContentMatcher[];
      };

  export type Expression<
    TTag extends StringMatcher,
    TAttrs extends Maybe<AttrMatcher>
  > = MaybeArray<Matcher<TTag, TAttrs>>;

  export type NodeCallback<
    TTag extends Maybe<string> = Maybe<string>,
    TAttrs extends Maybe<NodeAttributes> = Maybe<NodeAttributes>
  > = (node: Node<TTag, TAttrs>) => MaybeArray<Node | RawNode>;

  export type NodeAttributes = Record<string, Maybe<string>>;

  interface NodeAPI {
    walk: (cb: NodeCallback) => Node;
    match: <
      TTag extends StringMatcher,
      TAttrs extends Maybe<AttrMatcher>,
      TTagResult extends Maybe<string> = TTag extends string
        ? TTag
        : TTag extends void
        ? Maybe<string>
        : string,
      TAttrResult extends Maybe<NodeAttributes> = TAttrs extends void
        ? Maybe<NodeAttributes>
        : {
            [P in keyof TAttrs]: string;
          } &
            NodeAttributes
    >(
      expression: Expression<TTag, TAttrs>,
      cb: NodeCallback<TTagResult, TAttrResult>
    ) => Node<TTagResult, TAttrResult>[];
  }

  export interface RawNode<
    TTag extends Maybe<string> = Maybe<string>,
    TAttrs extends Maybe<NodeAttributes> = Maybe<NodeAttributes>
  > {
    tag: TTag;
    attrs: TAttrs;
    content?: Array<string | RawNode>;
  }

  export interface Node<
    TTag extends Maybe<string> = Maybe<string>,
    TAttrs extends Maybe<NodeAttributes> = Maybe<NodeAttributes>
  > extends NodeAPI, RawNode<TTag, TAttrs> {
    content?: Array<string | Node>;
    options?: Options;
  }

  export interface Options {
    sync?: boolean;
    parser?: Function;
    render?: Function;
    skipParse?: boolean;
  }

  export type Plugin<TThis> = (
    tree: Node
  ) => void | Node | RawNode | ThisType<TThis>;

  export interface Result<TMessage> {
    html: string;
    tree: Node;
    messages: TMessage[];
  }

  export interface PostHTML<TThis, TMessage> {
    version: string;
    name: "";
    plugins: Plugin<TThis>[];
    messages: TMessage[];
    use<TThis>(plugins: MaybeArray<Plugin<TThis>>): this;
    process(html: string, options?: Options): Promise<Result<TMessage>>;
  }
}

declare function PostHTML<TThis, TMessage>(
  plugins?: PostHTML.Plugin<TThis>[]
): PostHTML.PostHTML<TThis, TMessage>;

export = PostHTML;