Skip to content

Commit f7844c1

Browse files
committed
3.2.3
1 parent 688d4c4 commit f7844c1

File tree

6 files changed

+273
-145
lines changed

6 files changed

+273
-145
lines changed

dist/logdown.js

Lines changed: 135 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ return /******/ (function(modules) { // webpackBootstrap
7979

8080
var Logdown = __webpack_require__(1)()
8181
var markdown = __webpack_require__(3)
82-
var isColorSupported = __webpack_require__(6)
83-
var globalObject = __webpack_require__(9)()
82+
var isColorSupported = __webpack_require__(5)
83+
var globalObject = __webpack_require__(8)()
8484

8585
//
8686
// Static
@@ -346,30 +346,42 @@ module.exports = function toArray (arg) {
346346
/* 3 */
347347
/***/ (function(module, exports, __webpack_require__) {
348348

349-
var rules = __webpack_require__(4)
350-
var getNextMatch = __webpack_require__(5)
349+
var Markdown = __webpack_require__(4)
351350

352-
function parse (text) {
353-
var styles = []
354-
var match = getNextMatch(text, rules)
351+
var styles = []
355352

356-
while (match) {
357-
styles.push(match.rule.style)
353+
function createStyledRenderer (style) {
354+
return function (input) {
355+
styles.push(style)
358356
styles.push('') // Empty string resets style.
359357

360-
text = text.replace(match.rule.regexp, match.rule.replacer)
361-
match = getNextMatch(text, rules)
358+
return '%c' + input + '%c'
359+
}
360+
}
361+
362+
var markdown = new Markdown({
363+
renderer: {
364+
'*': createStyledRenderer('font-weight:bold;'),
365+
'_': createStyledRenderer('font-style:italic;'),
366+
'`': createStyledRenderer(
367+
'background-color:rgba(255,204,102, 0.1);' +
368+
'color:#FFCC66;' +
369+
'padding:2px 5px;' +
370+
'border-radius:2px;'
371+
)
362372
}
373+
})
363374

364-
return {
365-
text: text,
366-
styles: styles
375+
function parse (text) {
376+
var result = {
377+
text: markdown.parse(text),
378+
styles: [].concat(styles)
367379
}
368-
}
369380

370-
//
371-
// API
372-
//
381+
styles.length = 0
382+
383+
return result
384+
}
373385

374386
module.exports = {
375387
parse: parse
@@ -380,79 +392,131 @@ module.exports = {
380392
/* 4 */
381393
/***/ (function(module, exports) {
382394

383-
module.exports = [
384-
{
385-
regexp: /\*([^*]+)\*/,
386-
replacer: function (match, submatch1) {
387-
return '%c' + submatch1 + '%c'
388-
},
389-
style: 'font-weight:bold;'
390-
},
391-
{
392-
regexp: /_([^_]+)_/,
393-
replacer: function (match, submatch1) {
394-
return '%c' + submatch1 + '%c'
395-
},
396-
style: 'font-style:italic;'
397-
},
398-
{
399-
regexp: /`([^`]+)`/,
400-
replacer: function (match, submatch1) {
401-
return '%c' + submatch1 + '%c'
402-
},
403-
style:
404-
'background-color:rgba(255,204,102, 0.1);' +
405-
'color:#FFCC66;' +
406-
'padding:2px 5px;' +
407-
'border-radius:2px;'
395+
var lexemeRe = /([_*`\\]|[^_*`\\]+)/g
396+
var mdTagRe = /[_*`]/
397+
398+
function Markdown (options) {
399+
this.renderer = options.renderer
400+
}
401+
402+
function isMdTag (lexeme) {
403+
return mdTagRe.test(lexeme)
404+
}
405+
406+
Markdown.prototype.parse = function (text) {
407+
var lexemes = text.match(lexemeRe)
408+
var render = this.renderer
409+
410+
var formattedText = ''
411+
var currentScope
412+
var scopesStack = []
413+
var activeScopes = {}
414+
var buffer
415+
var lexeme
416+
var cursor = 0
417+
418+
function drainTillLexeme (lexeme) {
419+
var buffer = ''
420+
421+
while (currentScope && currentScope.tag !== lexeme) {
422+
buffer = currentScope.tag + currentScope.text + buffer
423+
activeScopes[currentScope.tag] = false
424+
currentScope = scopesStack.pop()
425+
}
426+
427+
return buffer
408428
}
409-
]
410429

430+
while (lexeme = lexemes[cursor]) { // eslint-disable-line
431+
buffer = ''
432+
cursor++
433+
434+
if (isMdTag(lexeme)) {
435+
if (activeScopes[lexeme]) {
436+
// we've found matching closing tag
437+
// if we have some other unclosed tags in-between - treat them as a plain text
438+
buffer = drainTillLexeme(lexeme)
439+
440+
// now currentScope holds the scope for the tag (`lexeme`) we are trying to close
441+
buffer = render[currentScope.tag](currentScope.text + buffer)
442+
activeScopes[lexeme] = false
443+
currentScope = scopesStack.pop()
444+
} else {
445+
var initialText = ''
446+
447+
if (lexeme === '`') {
448+
// `code` according to spec has the highest priority
449+
// and does not allow nesting
450+
// looking if we can find the closing delimiter
451+
452+
// NOTE: we have already incremented cursor, so we do not need to add 1
453+
var newCursor = lexemes.indexOf(lexeme, cursor)
454+
455+
if (newCursor !== -1) {
456+
formattedText += drainTillLexeme() // if we already have activeScopes, treat them as plain text
457+
initialText = lexemes.slice(cursor, newCursor).join('')
458+
cursor = newCursor // set cursor to the closing backticks
459+
}
460+
}
411461

412-
/***/ }),
413-
/* 5 */
414-
/***/ (function(module, exports) {
462+
if (currentScope) {
463+
scopesStack.push(currentScope)
464+
}
415465

416-
module.exports = function getNextMatch (text, rules) {
417-
var matches = []
466+
activeScopes[lexeme] = true
467+
currentScope = {
468+
tag: lexeme,
469+
text: initialText
470+
}
471+
}
472+
} else {
473+
buffer = lexeme
418474

419-
rules.forEach(function (rule) {
420-
var match = text.match(rule.regexp)
475+
if (lexeme === '\\') { // process escaping
476+
var nextLexeme = lexemes[cursor]
421477

422-
if (match) {
423-
matches.push({
424-
rule: rule,
425-
match: match
426-
})
478+
if (isMdTag(nextLexeme) || nextLexeme === '\\') {
479+
// ignore next md tag, because it is escaped
480+
buffer = nextLexeme
481+
cursor++
482+
}
483+
}
427484
}
428-
})
429485

430-
if (matches.length === 0) {
431-
return null
486+
if (buffer) {
487+
if (currentScope) {
488+
currentScope.text += buffer
489+
} else {
490+
formattedText += buffer
491+
}
492+
493+
buffer = ''
494+
}
432495
}
433496

434-
matches.sort(function (a, b) {
435-
return a.match.index - b.match.index
436-
})
497+
// get the rest of the unclosed tags
498+
formattedText += drainTillLexeme()
437499

438-
return matches[0]
500+
return formattedText
439501
}
440502

503+
module.exports = Markdown
504+
441505

442506
/***/ }),
443-
/* 6 */
507+
/* 5 */
444508
/***/ (function(module, exports, __webpack_require__) {
445509

446-
var isWebkit = __webpack_require__(7)
447-
var isFirefox = __webpack_require__(8)
510+
var isWebkit = __webpack_require__(6)
511+
var isFirefox = __webpack_require__(7)
448512

449513
module.exports = function isColorSupported () {
450514
return (isWebkit() || isFirefox())
451515
}
452516

453517

454518
/***/ }),
455-
/* 7 */
519+
/* 6 */
456520
/***/ (function(module, exports) {
457521

458522
// Is webkit? http://stackoverflow.com/a/16459606/376773
@@ -466,7 +530,7 @@ module.exports = function isWebkit () {
466530

467531

468532
/***/ }),
469-
/* 8 */
533+
/* 7 */
470534
/***/ (function(module, exports) {
471535

472536
module.exports = function isFirefox () {
@@ -479,7 +543,7 @@ module.exports = function isFirefox () {
479543

480544

481545
/***/ }),
482-
/* 9 */
546+
/* 8 */
483547
/***/ (function(module, exports, __webpack_require__) {
484548

485549
/* WEBPACK VAR INJECTION */(function(global) {/* eslint-disable no-new-func */
@@ -499,10 +563,10 @@ function getGlobal (slf, glb) {
499563
module.exports = getGlobal.bind(this, self, global)
500564
module.exports.getGlobal = getGlobal
501565

502-
/* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(10)))
566+
/* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(9)))
503567

504568
/***/ }),
505-
/* 10 */
569+
/* 9 */
506570
/***/ (function(module, exports) {
507571

508572
var g;

dist/logdown.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/logdown.min.js.gzip

189 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)