Browse Source

Take into account adjacent tokens in smartquotes

close https://github.com/markdown-it/markdown-it/issues/181
pull/183/merge
Alex Kocharin 9 years ago
parent
commit
946b1a1961
  1. 34
      lib/rules_core/smartquotes.js
  2. 13
      test/fixtures/markdown-it/smartquotes.txt

34
lib/rules_core/smartquotes.js

@ -50,9 +50,37 @@ function process_inlines(tokens, state) {
pos = t.index + 1;
isSingle = (t[0] === "'");
// treat begin/end of the line as a whitespace
lastChar = t.index - 1 >= 0 ? text.charCodeAt(t.index - 1) : 0x20;
nextChar = pos < max ? text.charCodeAt(pos) : 0x20;
// Find previous character,
// default to space if it's the beginning of the line
//
lastChar = 0x20;
if (t.index - 1 >= 0) {
lastChar = text.charCodeAt(t.index - 1);
} else {
for (j = i - 1; j >= 0; j--) {
if (tokens[j].type !== 'text') { continue; }
lastChar = tokens[j].content.charCodeAt(tokens[j].content.length - 1);
break;
}
}
// Find next character,
// default to space if it's the end of the line
//
nextChar = 0x20;
if (pos < max) {
nextChar = text.charCodeAt(pos);
} else {
for (j = i + 1; j < tokens.length; j++) {
if (tokens[j].type !== 'text') { continue; }
nextChar = tokens[j].content.charCodeAt(0);
break;
}
}
isLastPunctChar = isMdAsciiPunct(lastChar) || isPunctChar(String.fromCharCode(lastChar));
isNextPunctChar = isMdAsciiPunct(nextChar) || isPunctChar(String.fromCharCode(nextChar));

13
test/fixtures/markdown-it/smartquotes.txt

@ -96,3 +96,16 @@ Quotes between punctuation chars:
.
<p>“(hai)”.</p>
.
Quotes at the start/end of the tokens:
.
"*foo* bar"
"foo *bar*"
"*foo bar*"
.
<p>“<em>foo</em> bar”</p>
<p>“foo <em>bar</em>”</p>
<p>“<em>foo bar</em>”</p>
.

Loading…
Cancel
Save