-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
326 lines (269 loc) · 8.93 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
const path = require('path');
const util = require('util');
const vm = require('vm');
const minimist = require('minimist');
const requireLike = require('require-like');
const RESULT_RE = /\/\/\s*RESULT\s*$/;
const LARGE_RESULT_LINES = 2; // Number of lines that constitute a "large" result
const LINK_MARKDOWN = `<a href="https://github.com/broofa/runmd"><image height="13" src="https://camo.githubusercontent.com/5c7c603cd1e6a43370b0a5063d457e0dabb74cf317adc7baba183acb686ee8d0/687474703a2f2f692e696d6775722e636f6d2f634a4b6f3662552e706e67" /></a>`;
/**
* Represents a line of source showing an evaluated expression
*/
class ResultLine {
constructor(id, line) {
this.id = id;
this.line = line;
this._result = util.inspect(undefined);
}
/** Source w/out RESULT comment e */
get bare() {
return this.line.replace(RESULT_RE, '');
}
/** Source w/out indenting */
get undent() {
return this.line.replace(/^\S.*/, '');
}
/** String needed to line up with RESULT comment */
get prefix() {
return this.line.replace(RESULT_RE, '').replace(/./g, ' ') + '// ';
}
/** Executable source needed to produce result */
get scriptLine() {
// Trim string (including trailing ';')
const trimmed = this.line
.replace(RESULT_RE, '')
.replace(/^\s+|[\s;]+$/g, '');
// You can't wrap an expression in ()'s if it also has a var declaration, so
// we do a bit of regex hacking to wrap just the expression part, here
/(^\s*(?:const|let|var)[\w\s,]*=\s)*(.*)/.test(trimmed);
// Script line with code needed to set the result in the context-global
// `__.results` object
return `${RegExp.$1} __.results[${this.id}].result = (${RegExp.$2});`;
}
/** Set result of evaluating line's expression */
set result(val) {
const MAX_LEN = 130; // Github horizontal scrollbars appear at ~140 chars
this._result = util.inspect(val, {
depth: null,
breakLength: Math.max(40, MAX_LEN - this.prefix.length)
});
// If result spans multiple lines, move it to the next line
if (this._result.split('\n').length >= LARGE_RESULT_LINES) {
this._result = util.inspect(val, {
depth: null,
breakLength: MAX_LEN - this.undent.length
});
}
}
/** Fully rendered line output */
toString() {
let lines = this._result.split('\n');
let prefix = this.prefix;
if (lines.length >= LARGE_RESULT_LINES) {
lines.unshift('');
prefix = this.undent + ' // ';
}
lines = lines.map((line, i) =>
i === 0 ? '// \u21e8 ' + line : prefix + line
);
return this.bare + lines.join('\n');
}
}
/**
* Create a VM context factory. Creates a function that creates / returns VM
* contexts.
*/
function _createCache(runmd, inputName, write) {
const contexts = new Map();
/**
* Get a [named] VM execution context
*
* @param {String} [name] of context to reuse / create
*/
return function (name) {
if (name && contexts.has(name)) return contexts.get(name);
// require() function that operates relative to input file path, ignore
// existing require() cache
if (!inputName) inputName = path.join(process.cwd(), '(stdin)');
const contextRequire = requireLike(inputName, true);
// Create console shim that renders to our output file
const log = function (...args) {
// Stringify args with util.inspect
args = args.map((arg) =>
typeof arg === 'string' ? arg : util.inspect(arg, { depth: null })
);
const lines = args
.join(' ')
.split('\n')
.map((line) => '\u21d2 ' + line);
write(...lines);
};
const consoleShim = {
log,
warn: log,
error: log
};
// Crude setTimeout shim. Callbacks are invoked immediately after script
// block(s) run
const _timers = [];
let _time = Date.now();
const timeoutShim = function (callback, delay) {
_timers.push({ callback, time: _time + delay });
_timers.sort((a, b) => (a.time < b.time ? -1 : a.time > b.time ? 1 : 0));
};
timeoutShim.flush = function () {
let timer;
while ((timer = _timers.shift())) {
_time = timer.time;
timer.callback();
}
};
// Create VM context
const context = vm.createContext({
console: consoleShim,
__: { results: [] },
process,
require: (ref) => {
if (runmd.onRequire) ref = runmd.onRequire(ref);
return contextRequire(ref);
},
runmd,
setTimeout: timeoutShim
});
// Named contexts get cached
if (name) contexts.set(name, context);
return context;
};
}
/**
* Render RunMD-compatible markdown file
*
* @param {String} text to transform
* @param {Object} [options]
* @param {String} [options.inputName] name of input file
* @param {String} [options.outputName] name of output file
* @param {Boolean} [options.lame] if true, disables RunMD footer
*/
function render(inputText, options = {}) {
const { inputName, outputName, lame } = options;
// Warn people against editing the output file (only helps about 50% of the
// time but, hey, we tried!)
const outputLines = inputName
? [
'<!--',
` -- This file is auto-generated from ${inputName}. Changes should be made there.`,
' -->',
''
]
: [];
const scriptLines = [];
let lineOffset = 0;
let runArgs;
let runContext = {};
let hide = false;
// Read input file, split into lines
const lines = inputText.split('\n');
// Add line(s) to output (when --hide flag not active)
function _write(...lines) {
if (hide) return;
outputLines.push(...lines.filter((line) => line != null));
}
// State available to all contexts as `runmd`
const runmd = {
// Hook for transforming output lines
onOutputLine: null,
// Hook for transforming require()'s
onRequire: null,
// Make Date available for duck-patching
Date
};
// Clear VM context cache
const getContext = _createCache(runmd, inputName, _write);
// Process each line of input
lines.forEach((line, lineNo) => {
if (!runArgs) {
// Look for start of script block with '--(arg)' arguments
runArgs = /^```javascript\s+(--.*)?/i.test(line) && RegExp.$1;
if (runArgs) {
runArgs = minimist(runArgs.split(/\s+/));
// Get runContext for this script block
runContext = getContext(runArgs.run === true ? '' : runArgs.run);
hide = !!runArgs.hide;
lineOffset = lineNo + 1;
line = line.replace(/\s.*/, '');
}
} else if (runArgs && /^```/.test(line)) {
// End of script block. Process the script lines we gathered
let script = scriptLines.join('\n');
// Limited support for ES6-style imports. Transforms some "import"
// patterns into CommonJS require()'s via regex.
script = script
// "import { X as Y } from 'Z'"
.replace(
/import\s*\{\s*([^\s]+)\s+as\s+([^\s]+)\s*\}\s*from\s*['"]([^']+)['"]/g,
'const { $1: $2 } = require("$3")'
)
// "import { X } from 'Z'"
.replace(
/import\s*\{\s*([^\s]+)\s*\}\s*from\s*['"]([^']+)['"]/g,
'const { $1 } = require("$2")'
)
// ES6: "import X from 'Z'"
.replace(
/import\s([^\s]+)\sfrom\s*['"]([^']+)['"]/g,
'const $1 = require("$2")'
);
// Clear out script lines
scriptLines.length = 0;
// Replace setTimeout globally
// (is this necessary since we define it in each context?!? I've
// forgotten :-p )
const _timeout = setTimeout;
setTimeout = runContext.setTimeout;
// Run the script (W00t!)
try {
vm.runInContext(script, runContext, {
lineOffset,
filename: inputName || '(input string)'
});
} finally {
// Flush pending timers
runContext.setTimeout.flush();
// Restore system setTimeout
setTimeout = _timeout;
}
runContext = null;
runArgs = false;
if (hide) line = null;
hide = false;
} else if (runArgs) {
// Line is part of a script block
// If line has a RESULT comment, turn it into a ResultLine that injects
// the value of the expression into the comment
if (!hide && RESULT_RE.test(line)) {
const resultId = runContext.__.results.length;
line = runContext.__.results[resultId] = new ResultLine(resultId, line);
}
scriptLines.push(line.scriptLine || line);
}
// Add line to output
if (!hide) {
// onOutputLine for user script transforms
if (line != null && runmd.onOutputLine)
line = runmd.onOutputLine(line, !!runArgs);
_write(line);
}
});
if (!lame) {
_write('---');
_write('');
_write(
outputName
? `Markdown generated from [${inputName}](${inputName}) by ${LINK_MARKDOWN}`
: `Markdown generated by ${LINK_MARKDOWN}`
);
_write('');
}
return outputLines.join('\n');
}
module.exports = { render };