|
1 | 1 | 'use strict'; |
2 | 2 |
|
| 3 | +Object.defineProperty(exports, '__esModule', { value: true }); |
| 4 | + |
3 | 5 | /*! (c) 2013-2018 Andrea Giammarchi (ISC) */ |
4 | 6 | /** |
5 | 7 | * Fully inspired by the work of John Gruber |
@@ -253,9 +255,41 @@ for (var |
253 | 255 | return original.apply(console, parse.apply(null, arguments)); |
254 | 256 | } : |
255 | 257 | function () { |
256 | | - return arguments.length === 1 && typeof arguments[0] === 'string' ? |
257 | | - original.apply(console, parse(arguments[0])) : |
258 | | - original.apply(console, arguments); |
| 258 | + var singleStringArg = arguments.length === 1 && typeof arguments[0] === 'string'; |
| 259 | + var args = singleStringArg ? parse(arguments[0]) : arguments; |
| 260 | + |
| 261 | + // Todo: We might expose more to the reporter (e.g., returning |
| 262 | + // `what` and `match` from the `parse`->`match` function) so |
| 263 | + // the user could, e.g., build spans with classes rather than |
| 264 | + // inline styles |
| 265 | + if (_reporter) { |
| 266 | + var |
| 267 | + lastIndex, resultInfo, |
| 268 | + msg = args[0], |
| 269 | + formattingArgs = args.slice(1), |
| 270 | + formatRegex = /%c(.*?)(?=%c|$)/g, |
| 271 | + tmpIndex = 0; |
| 272 | + _reporter.init(); |
| 273 | + while ((resultInfo = formatRegex.exec(msg)) !== null) { |
| 274 | + var lastIndex = formatRegex.lastIndex; |
| 275 | + var result = resultInfo[0]; |
| 276 | + if (result.length > 2) { // Ignore any empty %c's |
| 277 | + var beginningResultIdx = lastIndex - result.length; |
| 278 | + if (beginningResultIdx > tmpIndex) { |
| 279 | + var text = msg.slice(tmpIndex, beginningResultIdx); |
| 280 | + _reporter.report(text); |
| 281 | + } |
| 282 | + _reporter.report(result.slice(2), formattingArgs.splice(0, 1)[0]); |
| 283 | + } |
| 284 | + tmpIndex = lastIndex; |
| 285 | + } |
| 286 | + if (tmpIndex < msg.length) { |
| 287 | + var text = msg.slice(tmpIndex); |
| 288 | + _reporter.report(text); |
| 289 | + } |
| 290 | + _reporter.done(args); |
| 291 | + } |
| 292 | + return original.apply(console, args); |
259 | 293 | }).raw = function () { |
260 | 294 | return original.apply(console, arguments); |
261 | 295 | }; |
@@ -289,4 +323,12 @@ try { |
289 | 323 | } |
290 | 324 | } |
291 | 325 |
|
292 | | -module.exports = consolemd; |
| 326 | +var _reporter = null; |
| 327 | +var addReporter = function (reporter) { |
| 328 | + _reporter = reporter; |
| 329 | +}; |
| 330 | + |
| 331 | +exports.addReporter = addReporter; |
| 332 | +exports.default = consolemd; |
| 333 | + |
| 334 | +module.exports = exports.default; |
0 commit comments