Browse Source
Tokenize first, and replace tokens with emphasis tags on a second pass using an algorithm close to one used in CM.pull/158/merge

16 changed files with 421 additions and 298 deletions
@ -0,0 +1,18 @@ |
|||
Migration to v5 |
|||
=============== |
|||
|
|||
v5 has the same external API as v4, only internals were changed. Plugins that introduce block elements might need an update. |
|||
|
|||
## For users |
|||
|
|||
External API did not change. |
|||
|
|||
- If you use `markdown-it` with plugins, make sure to update them. |
|||
|
|||
|
|||
## For plugin developers |
|||
|
|||
- added `stateBlock.sCount` to calculate indents instead of `stateBlock.tShift`, it only differs if tabs are present: |
|||
- `stateBlock.tShift` is used to calculate a number of *characters* (tab is 1 character) |
|||
- `stateBlock.sCount` is used to calculate the block *offset* (tab is 1-4 characters depending on position) |
|||
|
@ -0,0 +1,36 @@ |
|||
// For each opening emphasis-like marker find a matching closing one
|
|||
//
|
|||
'use strict'; |
|||
|
|||
|
|||
module.exports = function link_pairs(state) { |
|||
var i, j, lastDelim, currDelim, |
|||
delimiters = state.delimiters, |
|||
max = state.delimiters.length; |
|||
|
|||
for (i = 0; i < max; i++) { |
|||
lastDelim = delimiters[i]; |
|||
|
|||
if (!lastDelim.close) { continue; } |
|||
|
|||
j = i - lastDelim.jump - 1; |
|||
|
|||
while (j >= 0) { |
|||
currDelim = delimiters[j]; |
|||
|
|||
if (currDelim.open && |
|||
currDelim.marker === lastDelim.marker && |
|||
currDelim.end < 0 && |
|||
currDelim.level === lastDelim.level) { |
|||
|
|||
lastDelim.jump = i - j; |
|||
lastDelim.open = false; |
|||
currDelim.end = i; |
|||
currDelim.jump = 0; |
|||
break; |
|||
} |
|||
|
|||
j -= currDelim.jump + 1; |
|||
} |
|||
} |
|||
}; |
@ -0,0 +1,33 @@ |
|||
// Merge adjacent text nodes into one, and re-calculate all token levels
|
|||
//
|
|||
'use strict'; |
|||
|
|||
|
|||
module.exports = function text_collapse(state) { |
|||
var curr, last, |
|||
level = 0, |
|||
tokens = state.tokens, |
|||
max = state.tokens.length; |
|||
|
|||
for (curr = last = 0; curr < max; curr++) { |
|||
// re-calculate levels
|
|||
level += tokens[curr].nesting; |
|||
tokens[curr].level = level; |
|||
|
|||
if (tokens[curr].type === 'text' && |
|||
curr + 1 < max && |
|||
tokens[curr + 1].type === 'text') { |
|||
|
|||
// collapse two adjacent text nodes
|
|||
tokens[curr + 1].content = tokens[curr].content + tokens[curr + 1].content; |
|||
} else { |
|||
if (curr !== last) { tokens[last] = tokens[curr]; } |
|||
|
|||
last++; |
|||
} |
|||
} |
|||
|
|||
if (curr !== last) { |
|||
tokens.length = last; |
|||
} |
|||
}; |
@ -1,14 +0,0 @@ |
|||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|||
src line: 6241 |
|||
|
|||
. |
|||
*foo __bar *baz bim__ bam* |
|||
. |
|||
<p><em>foo <strong>bar *baz bim</strong> bam</em></p> |
|||
. |
|||
|
|||
error: |
|||
|
|||
<p>*foo <strong>bar *baz bim</strong> bam*</p> |
|||
|
|||
|
Loading…
Reference in new issue