Markdown parser, done right. 100% CommonMark support, extensions, syntax plugins & high speed
https://markdown-it.github.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
3.1 KiB
114 lines
3.1 KiB
10 years ago
|
// Convert straight quotation marks to typographic ones
|
||
|
//
|
||
|
'use strict';
|
||
|
|
||
|
|
||
10 years ago
|
var QUOTE_TEST_RE = /['"]/;
|
||
|
var QUOTE_RE = /['"]/g;
|
||
|
var PUNCT_RE = /[-\s()\[\]]/;
|
||
|
var APOSTROPHE = '’';
|
||
10 years ago
|
|
||
|
// This function returns true if the character at `pos`
|
||
|
// could be inside a word.
|
||
|
function isLetter(str, pos) {
|
||
|
if (pos < 0 || pos >= str.length) { return false; }
|
||
10 years ago
|
return !PUNCT_RE.test(str[pos]);
|
||
10 years ago
|
}
|
||
|
|
||
|
|
||
10 years ago
|
function replaceAt(str, index, ch) {
|
||
|
return str.substr(0, index) + ch + str.substr(index + 1);
|
||
10 years ago
|
}
|
||
|
|
||
|
|
||
10 years ago
|
module.exports = function smartquotes(state) {
|
||
10 years ago
|
/*eslint max-depth:0*/
|
||
10 years ago
|
var i, token, text, t, pos, max, thisLevel, lastSpace, nextSpace, item,
|
||
10 years ago
|
canOpen, canClose, j, isSingle, blkIdx, tokens,
|
||
|
stack;
|
||
10 years ago
|
|
||
10 years ago
|
if (!state.options.typographer) { return; }
|
||
|
|
||
|
stack = [];
|
||
10 years ago
|
|
||
10 years ago
|
for (blkIdx = state.tokens.length - 1; blkIdx >= 0; blkIdx--) {
|
||
10 years ago
|
|
||
10 years ago
|
if (state.tokens[blkIdx].type !== 'inline') { continue; }
|
||
10 years ago
|
|
||
10 years ago
|
tokens = state.tokens[blkIdx].children;
|
||
|
stack.length = 0;
|
||
10 years ago
|
|
||
10 years ago
|
for (i = 0; i < tokens.length; i++) {
|
||
|
token = tokens[i];
|
||
|
|
||
|
if (token.type !== 'text' || QUOTE_TEST_RE.test(token.text)) { continue; }
|
||
|
|
||
|
thisLevel = tokens[i].level;
|
||
|
|
||
|
for (j = stack.length - 1; j >= 0; j--) {
|
||
|
if (stack[j].level <= thisLevel) { break; }
|
||
10 years ago
|
}
|
||
10 years ago
|
stack.length = j + 1;
|
||
|
|
||
|
text = token.content;
|
||
|
pos = 0;
|
||
|
max = text.length;
|
||
|
|
||
|
/*eslint no-labels:0,block-scoped-var:0*/
|
||
|
OUTER:
|
||
|
while (pos < max) {
|
||
|
QUOTE_RE.lastIndex = pos;
|
||
|
t = QUOTE_RE.exec(text);
|
||
|
if (!t) { break; }
|
||
|
|
||
|
lastSpace = !isLetter(text, t.index - 1);
|
||
|
pos = t.index + 1;
|
||
|
isSingle = (t[0] === "'");
|
||
|
nextSpace = !isLetter(text, pos);
|
||
|
|
||
|
if (!nextSpace && !lastSpace) {
|
||
|
// middle of word
|
||
|
if (isSingle) {
|
||
|
token.content = replaceAt(token.content, t.index, APOSTROPHE);
|
||
|
}
|
||
|
continue;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
canOpen = !nextSpace;
|
||
|
canClose = !lastSpace;
|
||
10 years ago
|
|
||
10 years ago
|
if (canClose) {
|
||
|
// this could be a closing quote, rewind the stack to get a match
|
||
|
for (j = stack.length - 1; j >= 0; j--) {
|
||
10 years ago
|
item = stack[j];
|
||
10 years ago
|
if (stack[j].level < thisLevel) { break; }
|
||
|
if (item.single === isSingle && stack[j].level === thisLevel) {
|
||
|
item = stack[j];
|
||
10 years ago
|
if (isSingle) {
|
||
|
tokens[item.token].content = replaceAt(tokens[item.token].content, item.pos, state.options.quotes[2]);
|
||
|
token.content = replaceAt(token.content, t.index, state.options.quotes[3]);
|
||
|
} else {
|
||
|
tokens[item.token].content = replaceAt(tokens[item.token].content, item.pos, state.options.quotes[0]);
|
||
|
token.content = replaceAt(token.content, t.index, state.options.quotes[1]);
|
||
10 years ago
|
}
|
||
|
stack.length = j;
|
||
|
continue OUTER;
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
if (canOpen) {
|
||
|
stack.push({
|
||
|
token: i,
|
||
|
pos: t.index,
|
||
|
single: isSingle,
|
||
|
level: thisLevel
|
||
|
});
|
||
|
} else if (canClose && isSingle) {
|
||
|
token.content = replaceAt(token.content, t.index, APOSTROPHE);
|
||
|
}
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
};
|