UNPKG

13.5 kBTypeScriptView Raw
1import LinkifyIt = require('linkify-it');
2
3import utils = require('./common/utils');
4import helpers = require('./helpers');
5import ParserBlock = require('./parser_block');
6import ParserCore = require('./parser_core');
7import ParserInline = require('./parser_inline');
8import Renderer = require('./renderer');
9import Token = require('./token');
10
11declare namespace MarkdownIt {
12 /**
13 * MarkdownIt provides named presets as a convenience to quickly
14 * enable/disable active syntax rules and options for common use cases.
15 *
16 * - ["commonmark"](https://github.com/markdown-it/markdown-it/blob/master/lib/presets/commonmark.js) -
17 * configures parser to strict [CommonMark](http://commonmark.org/) mode.
18 * - [default](https://github.com/markdown-it/markdown-it/blob/master/lib/presets/default.js) -
19 * similar to GFM, used when no preset name given. Enables all available rules,
20 * but still without html, typographer & autolinker.
21 * - ["zero"](https://github.com/markdown-it/markdown-it/blob/master/lib/presets/zero.js) -
22 * all rules disabled. Useful to quickly setup your config via `.enable()`.
23 * For example, when you need only `bold` and `italic` markup and nothing else.
24 */
25 type PresetName = 'default' | 'zero' | 'commonmark';
26
27 interface Options {
28 /**
29 * Set `true` to enable HTML tags in source. Be careful!
30 * That's not safe! You may need external sanitizer to protect output from XSS.
31 * It's better to extend features via plugins, instead of enabling HTML.
32 * @default false
33 */
34 html?: boolean | undefined;
35
36 /**
37 * Set `true` to add '/' when closing single tags
38 * (`<br />`). This is needed only for full CommonMark compatibility. In real
39 * world you will need HTML output.
40 * @default false
41 */
42 xhtmlOut?: boolean | undefined;
43
44 /**
45 * Set `true` to convert `\n` in paragraphs into `<br>`.
46 * @default false
47 */
48 breaks?: boolean | undefined;
49
50 /**
51 * CSS language class prefix for fenced blocks.
52 * Can be useful for external highlighters.
53 * @default 'language-'
54 */
55 langPrefix?: string | undefined;
56
57 /**
58 * Set `true` to autoconvert URL-like text to links.
59 * @default false
60 */
61 linkify?: boolean | undefined;
62
63 /**
64 * Set `true` to enable [some language-neutral replacement](https://github.com/markdown-it/markdown-it/blob/master/lib/rules_core/replacements.js) +
65 * quotes beautification (smartquotes).
66 * @default false
67 */
68 typographer?: boolean | undefined;
69
70 /**
71 * Double + single quotes replacement
72 * pairs, when typographer enabled and smartquotes on. For example, you can
73 * use `'«»„“'` for Russian, `'„“‚‘'` for German, and
74 * `['«\xA0', '\xA0»', '‹\xA0', '\xA0›']` for French (including nbsp).
75 * @default '“”‘’'
76 */
77 quotes?: string | string[];
78
79 /**
80 * Highlighter function for fenced code blocks.
81 * Highlighter `function (str, lang, attrs)` should return escaped HTML. It can
82 * also return empty string if the source was not changed and should be escaped
83 * externally. If result starts with <pre... internal wrapper is skipped.
84 * @default null
85 */
86 highlight?: ((str: string, lang: string, attrs: string) => string) | null | undefined;
87 }
88
89 type PluginSimple = (md: MarkdownIt) => void;
90 type PluginWithOptions<T = any> = (md: MarkdownIt, options?: T) => void;
91 type PluginWithParams = (md: MarkdownIt, ...params: any[]) => void;
92}
93
94interface MarkdownItConstructor {
95 new (): MarkdownIt;
96 new (presetName: MarkdownIt.PresetName, options?: MarkdownIt.Options): MarkdownIt;
97 new (options: MarkdownIt.Options): MarkdownIt;
98 (): MarkdownIt;
99 (presetName: MarkdownIt.PresetName, options?: MarkdownIt.Options): MarkdownIt;
100 (options: MarkdownIt.Options): MarkdownIt;
101}
102
103interface MarkdownIt {
104 /**
105 * Instance of [[ParserInline]]. You may need it to add new rules when
106 * writing plugins. For simple rules control use [[MarkdownIt.disable]] and
107 * [[MarkdownIt.enable]].
108 */
109 readonly inline: ParserInline;
110
111 /**
112 * Instance of [[ParserBlock]]. You may need it to add new rules when
113 * writing plugins. For simple rules control use [[MarkdownIt.disable]] and
114 * [[MarkdownIt.enable]].
115 */
116 readonly block: ParserBlock;
117
118 /**
119 * Instance of [[Core]] chain executor. You may need it to add new rules when
120 * writing plugins. For simple rules control use [[MarkdownIt.disable]] and
121 * [[MarkdownIt.enable]].
122 */
123 readonly core: ParserCore;
124
125 /**
126 * Instance of [[Renderer]]. Use it to modify output look. Or to add rendering
127 * rules for new token types, generated by plugins.
128 *
129 * ##### Example
130 *
131 * ```javascript
132 * var md = require('markdown-it')();
133 *
134 * function myToken(tokens, idx, options, env, self) {
135 * //...
136 * return result;
137 * };
138 *
139 * md.renderer.rules['my_token'] = myToken
140 * ```
141 *
142 * See [[Renderer]] docs and [source code](https://github.com/markdown-it/markdown-it/blob/master/lib/renderer.js).
143 */
144 readonly renderer: Renderer;
145
146 /**
147 * [linkify-it](https://github.com/markdown-it/linkify-it) instance.
148 * Used by [linkify](https://github.com/markdown-it/markdown-it/blob/master/lib/rules_core/linkify.js)
149 * rule.
150 */
151 readonly linkify: LinkifyIt.LinkifyIt;
152
153 /**
154 * Link validation function. CommonMark allows too much in links. By default
155 * we disable `javascript:`, `vbscript:`, `file:` schemas, and almost all `data:...` schemas
156 * except some embedded image types.
157 *
158 * You can change this behaviour:
159 *
160 * ```javascript
161 * var md = require('markdown-it')();
162 * // enable everything
163 * md.validateLink = function () { return true; }
164 * ```
165 */
166 validateLink(url: string): boolean;
167
168 /**
169 * Function used to encode link url to a machine-readable format,
170 * which includes url-encoding, punycode, etc.
171 */
172 normalizeLink(url: string): string;
173
174 /**
175 * Function used to decode link url to a human-readable format`
176 */
177 normalizeLinkText(url: string): string;
178
179 readonly utils: typeof utils;
180
181 readonly helpers: typeof helpers;
182
183 readonly options: MarkdownIt.Options;
184
185 /**
186 * *chainable*
187 *
188 * Set parser options (in the same format as in constructor). Probably, you
189 * will never need it, but you can change options after constructor call.
190 *
191 * ##### Example
192 *
193 * ```javascript
194 * var md = require('markdown-it')()
195 * .set({ html: true, breaks: true })
196 * .set({ typographer: true });
197 * ```
198 *
199 * __Note:__ To achieve the best possible performance, don't modify a
200 * `markdown-it` instance options on the fly. If you need multiple configurations
201 * it's best to create multiple instances and initialize each with separate
202 * config.
203 */
204 set(options: MarkdownIt.Options): this;
205
206 /**
207 * *chainable*, *internal*
208 *
209 * Batch load of all options and compenent settings. This is internal method,
210 * and you probably will not need it. But if you with - see available presets
211 * and data structure [here](https://github.com/markdown-it/markdown-it/tree/master/lib/presets)
212 *
213 * We strongly recommend to use presets instead of direct config loads. That
214 * will give better compatibility with next versions.
215 */
216 configure(presets: MarkdownIt.PresetName): this;
217
218 /**
219 * *chainable*
220 *
221 * Enable list or rules. It will automatically find appropriate components,
222 * containing rules with given names. If rule not found, and `ignoreInvalid`
223 * not set - throws exception.
224 *
225 * ##### Example
226 *
227 * ```javascript
228 * var md = require('markdown-it')()
229 * .enable(['sub', 'sup'])
230 * .disable('smartquotes');
231 * ```
232 *
233 * @param list rule name or list of rule names to enable
234 * @param ignoreInvalid set `true` to ignore errors when rule not found.
235 */
236 enable(list: string | string[], ignoreInvalid?: boolean): this;
237
238 /**
239 * *chainable*
240 *
241 * The same as [[MarkdownIt.enable]], but turn specified rules off.
242 *
243 * @param list rule name or list of rule names to disable.
244 * @param ignoreInvalid set `true` to ignore errors when rule not found.
245 */
246 disable(list: string | string[], ignoreInvalid?: boolean): this;
247
248 /**
249 * *chainable*
250 *
251 * Load specified plugin with given params into current parser instance.
252 * It's just a sugar to call `plugin(md, params)` with curring.
253 *
254 * ##### Example
255 *
256 * ```javascript
257 * var iterator = require('markdown-it-for-inline');
258 * var md = require('markdown-it')()
259 * .use(iterator, 'foo_replace', 'text', function (tokens, idx) {
260 * tokens[idx].content = tokens[idx].content.replace(/foo/g, 'bar');
261 * });
262 * ```
263 */
264 use(plugin: MarkdownIt.PluginSimple): this;
265 use<T = any>(plugin: MarkdownIt.PluginWithOptions<T>, options?: T): this;
266 use(plugin: MarkdownIt.PluginWithParams, ...params: any[]): this;
267
268 /**
269 * *internal*
270 *
271 * Parse input string and returns list of block tokens (special token type
272 * "inline" will contain list of inline tokens). You should not call this
273 * method directly, until you write custom renderer (for example, to produce
274 * AST).
275 *
276 * `env` is used to pass data between "distributed" rules and return additional
277 * metadata like reference info, needed for the renderer. It also can be used to
278 * inject data in specific cases. Usually, you will be ok to pass `{}`,
279 * and then pass updated object to renderer.
280 *
281 * @param src source string
282 * @param env environment sandbox
283 */
284 parse(src: string, env: any): Token[];
285
286 /**
287 * Render markdown string into html. It does all magic for you :).
288 *
289 * `env` can be used to inject additional metadata (`{}` by default).
290 * But you will not need it with high probability. See also comment
291 * in [[MarkdownIt.parse]].
292 *
293 * @param src source string
294 * @param env environment sandbox
295 */
296 render(src: string, env?: any): string;
297
298 /**
299 * *internal*
300 *
301 * The same as [[MarkdownIt.parse]] but skip all block rules. It returns the
302 * block tokens list with the single `inline` element, containing parsed inline
303 * tokens in `children` property. Also updates `env` object.
304 *
305 * @param src source string
306 * @param env environment sandbox
307 */
308 parseInline(src: string, env: any): Token[];
309
310 /**
311 * Similar to [[MarkdownIt.render]] but for single paragraph content. Result
312 * will NOT be wrapped into `<p>` tags.
313 *
314 * @param src source string
315 * @param env environment sandbox
316 */
317 renderInline(src: string, env?: any): string;
318}
319
320/**
321 * Main parser/renderer class.
322 *
323 * ##### Usage
324 *
325 * ```javascript
326 * // node.js, "classic" way:
327 * var MarkdownIt = require('markdown-it'),
328 * md = new MarkdownIt();
329 * var result = md.render('# markdown-it rulezz!');
330 *
331 * // node.js, the same, but with sugar:
332 * var md = require('markdown-it')();
333 * var result = md.render('# markdown-it rulezz!');
334 *
335 * // browser without AMD, added to "window" on script load
336 * // Note, there are no dash.
337 * var md = window.markdownit();
338 * var result = md.render('# markdown-it rulezz!');
339 * ```
340 *
341 * Single line rendering, without paragraph wrap:
342 *
343 * ```javascript
344 * var md = require('markdown-it')();
345 * var result = md.renderInline('__markdown-it__ rulezz!');
346 * ```
347 *
348 * ##### Example
349 *
350 * ```javascript
351 * // commonmark mode
352 * var md = require('markdown-it')('commonmark');
353 *
354 * // default mode
355 * var md = require('markdown-it')();
356 *
357 * // enable everything
358 * var md = require('markdown-it')({
359 * html: true,
360 * linkify: true,
361 * typographer: true
362 * });
363 * ```
364 *
365 * ##### Syntax highlighting
366 *
367 * ```js
368 * var hljs = require('highlight.js') // https://highlightjs.org/
369 *
370 * var md = require('markdown-it')({
371 * highlight: function (str, lang) {
372 * if (lang && hljs.getLanguage(lang)) {
373 * try {
374 * return hljs.highlight(lang, str, true).value;
375 * } catch (__) {}
376 * }
377 *
378 * return ''; // use external default escaping
379 * }
380 * });
381 * ```
382 *
383 * Or with full wrapper override (if you need assign class to `<pre>`):
384 *
385 * ```javascript
386 * var hljs = require('highlight.js') // https://highlightjs.org/
387 *
388 * // Actual default values
389 * var md = require('markdown-it')({
390 * highlight: function (str, lang) {
391 * if (lang && hljs.getLanguage(lang)) {
392 * try {
393 * return '<pre class="hljs"><code>' +
394 * hljs.highlight(lang, str, true).value +
395 * '</code></pre>';
396 * } catch (__) {}
397 * }
398 *
399 * return '<pre class="hljs"><code>' + md.utils.escapeHtml(str) + '</code></pre>';
400 * }
401 * });
402 * ```
403 */
404declare const MarkdownIt: MarkdownItConstructor;
405
406export = MarkdownIt;