forked from alexseman/remarkable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
197 lines (169 loc) · 4.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import * as utils from './common/utils';
import Renderer from './renderer';
import ParserCore from './parser_core';
import ParserBlock from './parser_block';
import ParserInline from './parser_inline';
import Ruler from './ruler';
import defaultConfig from './configs/default';
import fullConfig from './configs/full';
import commonmarkConfig from './configs/commonmark';
/**
* Expose `utils`, Useful helper functions for custom
* rendering.
*/
export {
utils
}
/**
* Preset configs
*/
var config = {
'default': defaultConfig,
'full': fullConfig,
'commonmark': commonmarkConfig
};
/**
* The `StateCore` class manages state.
*
* @param {Object} `instance` Remarkable instance
* @param {String} `str` Markdown string
* @param {Object} `env`
*/
function StateCore(instance, str, env) {
this.src = str;
this.env = env;
this.options = instance.options;
this.tokens = [];
this.inlineMode = false;
this.inline = instance.inline;
this.block = instance.block;
this.renderer = instance.renderer;
this.typographer = instance.typographer;
}
/**
* The main `Remarkable` class. Create an instance of
* `Remarkable` with a `preset` and/or `options`.
*
* @param {String} `preset` If no preset is given, `default` is used.
* @param {Object} `options`
*/
export function Remarkable(preset, options) {
if (typeof preset !== 'string') {
options = preset;
preset = 'default';
}
if (options && options.linkify != null) {
console.warn(
'linkify option is removed. Use linkify plugin instead:\n\n' +
'import Remarkable from \'remarkable\';\n' +
'import linkify from \'remarkable/linkify\';\n' +
'new Remarkable().use(linkify)\n'
);
}
this.inline = new ParserInline();
this.block = new ParserBlock();
this.core = new ParserCore();
this.renderer = new Renderer();
this.ruler = new Ruler();
this.options = {};
this.configure(config[preset]);
this.set(options || {});
}
/**
* Set options as an alternative to passing them
* to the constructor.
*
* ```js
* md.set({typographer: true});
* ```
* @param {Object} `options`
* @api public
*/
Remarkable.prototype.set = function (options) {
utils.assign(this.options, options);
};
/**
* Batch loader for components rules states, and options
*
* @param {Object} `presets`
*/
Remarkable.prototype.configure = function (presets) {
var self = this;
if (!presets) { throw new Error('Wrong `remarkable` preset, check name/content'); }
if (presets.options) { self.set(presets.options); }
if (presets.components) {
Object.keys(presets.components).forEach(function (name) {
if (presets.components[name].rules) {
self[name].ruler.enable(presets.components[name].rules, true);
}
});
}
};
/**
* Use a plugin.
*
* ```js
* var md = new Remarkable();
*
* md.use(plugin1)
* .use(plugin2, opts)
* .use(plugin3);
* ```
*
* @param {Function} `plugin`
* @param {Object} `options`
* @return {Object} `Remarkable` for chaining
*/
Remarkable.prototype.use = function (plugin, options) {
plugin(this, options);
return this;
};
/**
* Parse the input `string` and return a tokens array.
* Modifies `env` with definitions data.
*
* @param {String} `string`
* @param {Object} `env`
* @return {Array} Array of tokens
*/
Remarkable.prototype.parse = function (str, env) {
var state = new StateCore(this, str, env);
this.core.process(state);
return state.tokens;
};
/**
* The main `.render()` method that does all the magic :)
*
* @param {String} `string`
* @param {Object} `env`
* @return {String} Rendered HTML.
*/
Remarkable.prototype.render = function (str, env) {
env = env || {};
return this.renderer.render(this.parse(str, env), this.options, env);
};
/**
* Parse the given content `string` as a single string.
*
* @param {String} `string`
* @param {Object} `env`
* @return {Array} Array of tokens
*/
Remarkable.prototype.parseInline = function (str, env) {
var state = new StateCore(this, str, env);
state.inlineMode = true;
this.core.process(state);
return state.tokens;
};
/**
* Render a single content `string`, without wrapping it
* to paragraphs
*
* @param {String} `str`
* @param {Object} `env`
* @return {String}
*/
Remarkable.prototype.renderInline = function (str, env) {
env = env || {};
return this.renderer.render(this.parseInline(str, env), this.options, env);
};