markdown-it - v15.0.2
    Preparing search index...

    Interface Ruler<Args, Result>

    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> {
        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)[];
    }

    Type Parameters

    • Args extends unknown[]
    • Result
    Index
    • Replace rule by name with new function & options. Throws error if name not found.

      Parameters

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

      Returns void

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

      md.core.ruler.at('replacements', function replace(state) {
      //...
      });
    • Add new rule to chain before one with given name. See also Ruler.after, Ruler.push.

      Parameters

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

      Returns void

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

      md.block.ruler.before('paragraph', 'my_rule', function replace(state) {
      //...
      });
    • Add new rule to chain after one with given name. See also Ruler.before, Ruler.push.

      Parameters

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

      Returns void

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

      md.inline.ruler.after('text', 'my_rule', function replace(state) {
      //...
      });
    • Push new rule to the end of chain. See also Ruler.before, Ruler.after.

      Parameters

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

      Returns void

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

      md.core.ruler.push('my_rule', function replace(state) {
      //...
      });
    • 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.

      Returns list of found rule names (if no exception happened).

      Parameters

      • list: string | string[]
      • ignoreInvalid: boolean = false

      Returns string[]

    • 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.

      Parameters

      • list: string | string[]
      • ignoreInvalid: boolean = false

      Returns void

    • 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.

      Returns list of found rule names (if no exception happened).

      Parameters

      • list: string | string[]
      • ignoreInvalid: boolean = false

      Returns string[]

    • 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.

      Parameters

      • chainName: string

      Returns ((...args: Args) => Result)[]