UNPKG

1.17 kBTypeScriptView Raw
1export interface NodeAttributes {
2 [attribute: string]: string;
3}
4
5export type NodeContent = Array<Node | PostHTMLTree | PostHTMLTree[]>;
6
7// T - Tag name
8// A - Attributes
9export interface Node<T = string, A = NodeAttributes> {
10 tag?: T;
11 attrs?: A;
12 content?: NodeContent;
13}
14
15type Matcher = string | RegExp | object;
16type Expression = Matcher | Matcher[];
17type CallbackNode = (node: Node) => Node | Node[];
18
19export interface PostHTMLTree {
20 walk: (cb: CallbackNode) => PostHTMLTree;
21 match: (expression: Expression, cb: CallbackNode) => CallbackNode;
22}
23
24export interface PostHTMLOptions {
25 sync?: boolean;
26 parser?: Function;
27 render?: Function;
28 skipParse?: boolean;
29}
30
31type Plugin<T> = (tree: PostHTMLTree) => void | PostHTMLTree | ThisType<T>;
32
33interface Result<M> {
34 html: string;
35 tree: PostHTMLTree;
36 messages: M[];
37}
38
39declare class PostHTML<T, M> {
40 version: string;
41 name: 'posthtml';
42 plugins: Plugin<T>[];
43 messages: M[];
44 use<T>(plugins: Plugin<T> | Plugin<T>[]): this;
45 process(html: string, options?: PostHTMLOptions): Promise<Result<M>>;
46}
47
48declare function posthtml<T, M>(plugins?: Plugin<T>[]): PostHTML<T, M>;
49
50export default posthtml;