UNPKG

5.14 kBTypeScriptView Raw
1declare namespace Ruler {
2 interface RuleOptions {
3 /**
4 * array with names of "alternate" chains.
5 */
6 alt: string[];
7 }
8}
9
10/**
11 * class Ruler
12 *
13 * Helper class, used by [[MarkdownIt#core]], [[MarkdownIt#block]] and
14 * [[MarkdownIt#inline]] to manage sequences of functions (rules):
15 *
16 * - keep rules in defined order
17 * - assign the name to each rule
18 * - enable/disable rules
19 * - add/replace rules
20 * - allow assign rules to additional named chains (in the same)
21 * - cacheing lists of active rules
22 *
23 * You will not need use this class directly until write plugins. For simple
24 * rules control use [[MarkdownIt.disable]], [[MarkdownIt.enable]] and
25 * [[MarkdownIt.use]].
26 */
27declare class Ruler<T> {
28 /**
29 * Replace rule by name with new function & options. Throws error if name not
30 * found.
31 *
32 * ##### Example
33 *
34 * Replace existing typographer replacement rule with new one:
35 *
36 * ```javascript
37 * var md = require('markdown-it')();
38 *
39 * md.core.ruler.at('replacements', function replace(state) {
40 * //...
41 * });
42 * ```
43 *
44 * @param name rule name to replace.
45 * @param fn new rule function.
46 * @param options new rule options (not mandatory).
47 */
48 at(name: string, fn: T, options?: Ruler.RuleOptions): void;
49
50 /**
51 * Add new rule to chain before one with given name. See also
52 * [[Ruler.after]], [[Ruler.push]].
53 *
54 * ##### Example
55 *
56 * ```javascript
57 * var md = require('markdown-it')();
58 *
59 * md.block.ruler.before('paragraph', 'my_rule', function replace(state) {
60 * //...
61 * });
62 * ```
63 *
64 * @param beforeName new rule will be added before this one.
65 * @param ruleName name of added rule.
66 * @param fn rule function.
67 * @param options rule options (not mandatory).
68 */
69 before(beforeName: string, ruleName: string, fn: T, options?: Ruler.RuleOptions): void;
70
71 /**
72 * Add new rule to chain after one with given name. See also
73 * [[Ruler.before]], [[Ruler.push]].
74 *
75 * ##### Options:
76 *
77 * - __alt__ - array with names of "alternate" chains.
78 *
79 * ##### Example
80 *
81 * ```javascript
82 * var md = require('markdown-it')();
83 *
84 * md.inline.ruler.after('text', 'my_rule', function replace(state) {
85 * //...
86 * });
87 * ```
88 *
89 * @param afterName new rule will be added after this one.
90 * @param ruleName name of added rule.
91 * @param fn rule function.
92 * @param options rule options (not mandatory).
93 */
94 after(afterName: string, ruleName: string, fn: T, options?: Ruler.RuleOptions): void;
95
96 /**
97 * Push new rule to the end of chain. See also
98 * [[Ruler.before]], [[Ruler.after]].
99 *
100 * ##### Options:
101 *
102 * - __alt__ - array with names of "alternate" chains.
103 *
104 * ##### Example
105 *
106 * ```javascript
107 * var md = require('markdown-it')();
108 *
109 * md.core.ruler.push('my_rule', function replace(state) {
110 * //...
111 * });
112 * ```
113 *
114 * @param ruleName name of added rule.
115 * @param fn rule function.
116 * @param options rule options (not mandatory).
117 */
118 push(ruleName: string, fn: T, options?: Ruler.RuleOptions): void;
119
120 /**
121 * Enable rules with given names. If any rule name not found - throw Error.
122 * Errors can be disabled by second param.
123 *
124 * Returns list of found rule names (if no exception happened).
125 *
126 * See also [[Ruler.disable]], [[Ruler.enableOnly]].
127 *
128 * @param list list of rule names to enable.
129 * @param ignoreInvalid set `true` to ignore errors when rule not found.
130 */
131 enable(list: string | string[], ignoreInvalid?: boolean): string[];
132
133 /**
134 * Enable rules with given names, and disable everything else. If any rule name
135 * not found - throw Error. Errors can be disabled by second param.
136 *
137 * See also [[Ruler.disable]], [[Ruler.enable]].
138 *
139 * @param list list of rule names to enable (whitelist).
140 * @param ignoreInvalid set `true` to ignore errors when rule not found.
141 */
142 enableOnly(list: string | string[], ignoreInvalid?: boolean): string[];
143
144 /**
145 * Disable rules with given names. If any rule name not found - throw Error.
146 * Errors can be disabled by second param.
147 *
148 * Returns list of found rule names (if no exception happened).
149 *
150 * See also [[Ruler.enable]], [[Ruler.enableOnly]].
151 *
152 * @param list list of rule names to disable.
153 * @param ignoreInvalid set `true` to ignore errors when rule not found.
154 */
155 disable(list: string | string[], ignoreInvalid?: boolean): string[];
156
157 /**
158 * Return array of active functions (rules) for given chain name. It analyzes
159 * rules configuration, compiles caches if not exists and returns result.
160 *
161 * Default chain name is `''` (empty string). It can't be skipped. That's
162 * done intentionally, to keep signature monomorphic for high speed.
163 */
164 getRules(chainName: string): T[];
165}
166
167export = Ruler;