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