-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
43 lines (35 loc) · 1.27 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
var repeat = require('lodash.repeat');
module.exports = function(md, opts) {
// default to being two spaces wide
var tabWidth;
if (!opts || typeof opts.tabWidth === 'undefined') {
tabWidth = 2;
} else {
tabWidth = Math.max(0, opts.tabWidth);
}
// patch the current rule, don't replace it completely
var originalRule = md.renderer.rules.fence;
md.renderer.rules.fence = function(tokens, idx, options, env, self) {
tokens[idx].content = expandTabs(tokens[idx].content, tabWidth);
return originalRule.call(this, tokens, idx, options, env, self);
};
// do the tab/space replacement
function expandTabs(content, tabWidth) {
var idx = 0;
// while we're not at the end of the string:
// - is the character at the current position a tab?
// - yes, replace with spaces and move the current position forward
// - no, jump to the character after the next newline
while (idx > -1 && idx < content.length) {
while (content[idx] === '\t') {
content = content.substring(0, idx) + repeat(' ', tabWidth) + content.substring(idx+1);
idx += tabWidth;
}
idx = content.indexOf('\n', idx);
// if there are no `\n` characters, break
if (idx === -1) { break; }
idx += 1;
}
return content;
}
};