Skip to content

Commit 641e5df

Browse files
authored
docs: explain partial extensions in use (#4033)
1 parent 452f1ed commit 641e5df

2 files changed

Lines changed: 73 additions & 5 deletions

File tree

src/Instance.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,20 @@ export class Marked<ParserOutput = string, RendererOutput = string> {
1818
defaults = _getDefaults<ParserOutput, RendererOutput>();
1919
options = this.setOptions;
2020

21+
/**
22+
* Compiles markdown to HTML.
23+
*
24+
* To configure hooks, a renderer, or a tokenizer, create a separate `Marked`
25+
* instance and use `Marked#use` instead of passing them on each call.
26+
*/
2127
parse = this.parseMarkdown(true);
28+
29+
/**
30+
* Compiles inline markdown to HTML.
31+
*
32+
* To configure hooks, a renderer, or a tokenizer, create a separate `Marked`
33+
* instance and use `Marked#use` instead of passing them on each call.
34+
*/
2235
parseInline = this.parseMarkdown(false);
2336

2437
Parser = _Parser<ParserOutput, RendererOutput>;
@@ -73,6 +86,15 @@ export class Marked<ParserOutput = string, RendererOutput = string> {
7386
return values;
7487
}
7588

89+
/**
90+
* Registers extensions with this Marked instance.
91+
*
92+
* The `renderer`, `tokenizer`, and `hooks` objects may each contain only the
93+
* methods that should be overridden. Their methods are merged with built-in
94+
* behavior and extensions registered by earlier calls.
95+
*
96+
* Use this method when supplying only some hook methods.
97+
*/
7698
use(...args: MarkedExtension<ParserOutput, RendererOutput>[]) {
7799
const extensions: MarkedOptions<ParserOutput, RendererOutput>['extensions'] = this.defaults.extensions || { renderers: {}, childTokens: {} };
78100

src/marked.ts

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ const markedInstance = new Marked();
1919
/**
2020
* Compiles markdown to HTML asynchronously.
2121
*
22+
* To configure hooks, a renderer, or a tokenizer, create a `Marked` instance
23+
* and use `Marked#use` instead of passing them as options on each call.
24+
*
2225
* @param src String of markdown source to be compiled
2326
* @param options Hash of options, having async: true
2427
* @return Promise of string of compiled HTML
@@ -28,6 +31,9 @@ export function marked(src: string, options: MarkedOptions & { async: true }): P
2831
/**
2932
* Compiles markdown to HTML.
3033
*
34+
* To configure hooks, a renderer, or a tokenizer, create a `Marked` instance
35+
* and use `Marked#use` instead of passing them as options on each call.
36+
*
3137
* @param src String of markdown source to be compiled
3238
* @param options Optional hash of options
3339
* @return String of compiled HTML. Will be a Promise of string if async is set to true by any extensions.
@@ -39,6 +45,27 @@ export function marked(src: string, opt?: MarkedOptions | null): string | Promis
3945
return markedInstance.parse(src, opt);
4046
}
4147

48+
export declare namespace marked {
49+
/**
50+
* Compiles inline markdown to HTML.
51+
*
52+
* To configure hooks, a renderer, or a tokenizer, create a `Marked` instance
53+
* and use `Marked#use` instead of passing them as options on each call.
54+
*/
55+
let parseInline: typeof markedInstance.parseInline;
56+
57+
/**
58+
* Registers extensions with the default Marked instance.
59+
*
60+
* The `renderer`, `tokenizer`, and `hooks` objects may each contain only the
61+
* methods that should be overridden. Their methods are merged with built-in
62+
* behavior and extensions registered by earlier calls.
63+
*
64+
* Use this function when supplying only some hook methods.
65+
*/
66+
let use: (...args: MarkedExtension[]) => typeof marked;
67+
}
68+
4269
/**
4370
* Sets the default options.
4471
*
@@ -60,15 +87,25 @@ marked.getDefaults = _getDefaults;
6087
marked.defaults = _defaults;
6188

6289
/**
63-
* Use Extension
90+
* Registers extensions with the default Marked instance.
91+
*
92+
* The `renderer`, `tokenizer`, and `hooks` objects may each contain only the
93+
* methods that should be overridden. Their methods are merged with built-in
94+
* behavior and extensions registered by earlier calls.
95+
*
96+
* Use this function when supplying only some hook methods.
97+
*
98+
* @param args Extensions to register
99+
* @return The `marked` function
64100
*/
65-
66-
marked.use = function(...args: MarkedExtension[]) {
101+
function useExtension(...args: MarkedExtension[]) {
67102
markedInstance.use(...args);
68103
marked.defaults = markedInstance.defaults;
69104
changeDefaults(marked.defaults);
70105
return marked;
71-
};
106+
}
107+
108+
marked.use = useExtension;
72109

73110
/**
74111
* Run callback for every token
@@ -81,6 +118,9 @@ marked.walkTokens = function(tokens: Token[] | TokensList, callback: (token: Tok
81118
/**
82119
* Compiles markdown to HTML without enclosing `p` tag.
83120
*
121+
* To configure hooks, a renderer, or a tokenizer, create a `Marked` instance
122+
* and use `Marked#use` instead of passing them as options on each call.
123+
*
84124
* @param src String of markdown source to be compiled
85125
* @param options Hash of options
86126
* @return String of compiled HTML
@@ -102,8 +142,13 @@ marked.parse = marked;
102142

103143
export const options = marked.options;
104144
export const setOptions = marked.setOptions;
105-
export const use = marked.use;
106145
export const walkTokens = marked.walkTokens;
146+
/**
147+
* Compiles inline markdown to HTML.
148+
*
149+
* To configure hooks, a renderer, or a tokenizer, create a `Marked` instance
150+
* and use `Marked#use` instead of passing them as options on each call.
151+
*/
107152
export const parseInline = marked.parseInline;
108153
export const parse = marked;
109154
export const parser = _Parser.parse;
@@ -116,5 +161,6 @@ export { _Renderer as Renderer } from './Renderer.ts';
116161
export { _TextRenderer as TextRenderer } from './TextRenderer.ts';
117162
export { _Hooks as Hooks } from './Hooks.ts';
118163
export { Marked } from './Instance.ts';
164+
export { useExtension as use };
119165
export type * from './MarkedOptions.ts';
120166
export type * from './Tokens.ts';

0 commit comments

Comments
 (0)