diff --git a/markdown.js b/markdown.js index b549ca7..be5cfbd 100644 --- a/markdown.js +++ b/markdown.js @@ -31,6 +31,12 @@ md.core.ruler.after('linkify', 'pos_counter', function posCounter (state) { if (token.markup) { moveCursor(token.markup); } + if (token.type === 'hardbreak') { + moveCursor(' \n'); + } + if (token.type === 'list_item_open') { + moveCursorAfterMatch(/^\s*\d+\. /g); + } if (token.type === 'link_open') { moveCursor('['); } @@ -71,6 +77,19 @@ md.core.ruler.after('linkify', 'pos_counter', function posCounter (state) { return true; } + function moveCursorAfterMatch (needle) { + var regex = needle instanceof RegExp; + var re = regex ? needle : new RegExp('^\\s*' + escapeForRegExp(needle), 'ig'); + var match = re.exec(partial); + if (!match) { + return false; + } + var diff = match[0].length; + cursor += diff; + partial = partial.slice(diff); + return true; + } + function moveCursorAfterLinkClose () { moveCursor(']'); if (!moveCursor(/^\s*\[[^\]]+\]/g)) {