diff --git a/lib/lexer_block/paragraph.js b/lib/lexer_block/paragraph.js index fb5d316..7d37dbc 100644 --- a/lib/lexer_block/paragraph.js +++ b/lib/lexer_block/paragraph.js @@ -39,7 +39,7 @@ module.exports = function paragraph(state, startLine/*, endLine*/) { while ((ref = state.lexer.inline.parse_reference(content, state.options, state.env))) { t = state.env.references; t[ref.label] = t[ref.label] || { title: ref.title, href: ref.href }; - content = ref.remaining; + content = ref.remaining.trim(); } if (content) { diff --git a/lib/lexer_inline.js b/lib/lexer_inline.js index d54d26c..435c75f 100644 --- a/lib/lexer_inline.js +++ b/lib/lexer_inline.js @@ -205,6 +205,7 @@ LexerInline.prototype.parse_reference = function (str, options) { pos = state.pos; } else { title = ''; + pos = start; } // ensure that the end of the line is empty diff --git a/lib/lexer_inline/links.js b/lib/lexer_inline/links.js index 7735eb4..9b3fdae 100644 --- a/lib/lexer_inline/links.js +++ b/lib/lexer_inline/links.js @@ -77,7 +77,7 @@ function parseLinkDestination(state, pos) { state.pos = pos + 1; return href; } - if (code === 0x5C /* \ */) { + if (code === 0x5C /* \ */ && pos + 1 < max) { pos++; href += state.src[pos++]; continue; @@ -99,9 +99,9 @@ function parseLinkDestination(state, pos) { if (code === 0x20) { break; } // ascii control characters - if (code < 0x20 || code === 0x7F) { return null; } + if (code < 0x20 || code === 0x7F) { break; } - if (code === 0x5C /* \ */) { + if (code === 0x5C /* \ */ && pos + 1 < max) { pos++; href += state.src[pos++]; continue; @@ -109,14 +109,12 @@ function parseLinkDestination(state, pos) { if (code === 0x28 /* ( */) { level++; - if (level > 1) { return null; } + if (level > 1) { break; } } if (code === 0x29 /* ) */) { level--; - if (level < 0) { - break; - } + if (level < 0) { break; } } href += state.src[pos++]; @@ -152,7 +150,7 @@ function parseLinkTitle(state, pos) { state.pos = pos + 1; return title; } - if (code === 0x5C /* \ */) { + if (code === 0x5C /* \ */ && pos + 1 < max) { pos++; title += state.src[pos++]; continue; @@ -237,6 +235,7 @@ function links(state) { state.pos = labelStart - 1; return false; } + pos++; } else { // // Link reference @@ -252,7 +251,11 @@ function links(state) { if (pos < max && state.src.charCodeAt(pos) === 0x5B/* [ */) { start = pos + 1; pos = parseLinkLabel(state, pos); - label = state.src.slice(start, pos); + if (pos >= 0) { + label = state.src.slice(start, pos++); + } else { + pos = start - 1; + } } // covers label === '' and label === undefined @@ -260,7 +263,10 @@ function links(state) { if (!label) { label = state.src.slice(labelStart, labelEnd); } ref = state.env.references[normalizeReference(label)]; - if (!ref) { return false; } + if (!ref) { + state.pos = labelStart - 1; + return false; + } href = ref.href; title = ref.title; } @@ -284,7 +290,7 @@ function links(state) { state.push({ type: 'link_close' }); } - state.pos = pos + 1; + state.pos = pos; state.posMax = max; return true; } diff --git a/lib/renderer.js b/lib/renderer.js index ca06587..dc11ce0 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -111,7 +111,7 @@ rules.link_close = function (/*tokens, idx, options*/) { rules.image = function (tokens, idx, options) { var src = ' src="' + escapeHtmlKeepEntities(tokens[idx].src) + '"'; var title = tokens[idx].title ? (' title="' + escapeHtmlKeepEntities(tokens[idx].title) + '"') : ''; - var alt = tokens[idx].alt ? (' alt="' + escapeHtmlKeepEntities(tokens[idx].alt) + '"') : ''; + var alt = ' alt="' + (tokens[idx].alt ? escapeHtmlKeepEntities(tokens[idx].alt) : '') + '"'; var suffix = options.xhtml ? ' /' : ''; return ''; };