Interface Ruler<Args, Result>

Source
Expand description

Helper class, used by MarkdownIt.core, MarkdownIt.block and MarkdownIt.inline to manage sequences of functions (rules):

  • keep rules in defined order
  • assign the name to each rule
  • enable/disable rules
  • add/replace rules
  • allow assign rules to additional named chains (in the same)
  • cacheing lists of active rules

You will not need use this class directly until write plugins. For simple rules control use MarkdownIt.disable, MarkdownIt.enable and MarkdownIt.use.


interface Ruler<Args extends unknown[], Result> {
    __rules__: {
        name: string;
        enabled: boolean;
        fn: (...args: Args) => Result;
        alt: string[];
    }[];
    __cache__: Record<string, ((...args: Args) => Result)[]>
    | null;
    __find__(name: string): number;
    __compile__(): void;
    at(
        name: string,
        fn: (...args: Args) => Result,
        options?: { alt?: string[] },
    ): void;
    before(
        beforeName: string,
        ruleName: string,
        fn: (...args: Args) => Result,
        options?: { alt?: string[] },
    ): void;
    after(
        afterName: string,
        ruleName: string,
        fn: (...args: Args) => Result,
        options?: { alt?: string[] },
    ): void;
    push(
        ruleName: string,
        fn: (...args: Args) => Result,
        options?: { alt?: string[] },
    ): void;
    enable(list: string | string[], ignoreInvalid?: boolean): string[];
    enableOnly(list: string | string[], ignoreInvalid?: boolean): void;
    disable(list: string | string[], ignoreInvalid?: boolean): string[];
    getRules(chainName: string): ((...args: Args) => Result)[];
}

Properties§

§__rules__: { name: string; enabled: boolean; fn: (...args: Args) => Result; alt: string[] }[] = []
§__cache__: Record<string, ((...args: Args) => Result)[]> | null = null

Methods§

Source§

__find__(name: string): number

Source§

__compile__(): void

Source§

at(
    name: string,
    fn: (...args: Args) => Result,
    options?: { alt?: string[] },
): void

Replace rule by name with new function & options. Throws error if name not found.

Replace existing typographer replacement rule with new one
import MarkdownIt from 'markdown-it'
const md = new MarkdownIt()

md.core.ruler.at('replacements', function replace(state) {
//...
});
Source§

before(
    beforeName: string,
    ruleName: string,
    fn: (...args: Args) => Result,
    options?: { alt?: string[] },
): void

Add new rule to chain before one with given name. See also Ruler.after, Ruler.push.

example
import MarkdownIt from 'markdown-it'
const md = new MarkdownIt()

md.block.ruler.before('paragraph', 'my_rule', function replace(state) {
//...
});
Source§

after(
    afterName: string,
    ruleName: string,
    fn: (...args: Args) => Result,
    options?: { alt?: string[] },
): void

Add new rule to chain after one with given name. See also Ruler.before, Ruler.push.

example
import MarkdownIt from 'markdown-it'
const md = new MarkdownIt()

md.inline.ruler.after('text', 'my_rule', function replace(state) {
//...
});
Source§

push(
    ruleName: string,
    fn: (...args: Args) => Result,
    options?: { alt?: string[] },
): void

Push new rule to the end of chain. See also Ruler.before, Ruler.after.

example
import MarkdownIt from 'markdown-it'
const md = new MarkdownIt()

md.core.ruler.push('my_rule', function replace(state) {
//...
});
Source§

enable(list: string | string[], ignoreInvalid?: boolean): string[]

Enable rules with given names. If any rule name not found - throw Error. Errors can be disabled by second param.

See also Ruler.disable, Ruler.enableOnly.

Source§

enableOnly(list: string | string[], ignoreInvalid?: boolean): void

Enable rules with given names, and disable everything else. If any rule name not found - throw Error. Errors can be disabled by second param.

See also Ruler.disable, Ruler.enable.

Source§

disable(list: string | string[], ignoreInvalid?: boolean): string[]

Disable rules with given names. If any rule name not found - throw Error. Errors can be disabled by second param.

See also Ruler.enable, Ruler.enableOnly.

Source§

getRules(chainName: string): ((...args: Args) => Result)[]

Return array of active functions (rules) for given chain name. It analyzes rules configuration, compiles caches if not exists and returns result.

Default chain name is '' (empty string). It can't be skipped. That's done intentionally, to keep signature monomorphic for high speed.