Space prior to onebox stops onebox resolution

This commit is contained in:
Sam 2017-06-27 16:16:17 -04:00
parent 232de90607
commit 0d62420cbe
5 changed files with 144 additions and 3 deletions

View File

@ -10,3 +10,5 @@
//= require ./pretty-text/engines/markdown-it/category-hashtag
//= require ./pretty-text/engines/markdown-it/censored
//= require ./pretty-text/engines/markdown-it/table
//= require ./pretty-text/engines/markdown-it/paragraph
//= require ./pretty-text/engines/markdown-it/newline

View File

@ -0,0 +1,53 @@
// see: https://github.com/markdown-it/markdown-it/issues/375
//
// we use a custom paragraph rule cause we have to signal when a
// link starts with a space, so we can bypass a onebox
// this is a freedom patch, so careful, may break on updates
function newline(state, silent) {
var token, pmax, max, pos = state.pos;
if (state.src.charCodeAt(pos) !== 0x0A/* \n */) { return false; }
pmax = state.pending.length - 1;
max = state.posMax;
// ' \n' -> hardbreak
// Lookup in pending chars is bad practice! Don't copy to other rules!
// Pending string is stored in concat mode, indexed lookups will cause
// convertion to flat mode.
if (!silent) {
if (pmax >= 0 && state.pending.charCodeAt(pmax) === 0x20) {
if (pmax >= 1 && state.pending.charCodeAt(pmax - 1) === 0x20) {
state.pending = state.pending.replace(/ +$/, '');
token = state.push('hardbreak', 'br', 0);
} else {
state.pending = state.pending.slice(0, -1);
token = state.push('softbreak', 'br', 0);
}
} else {
token = state.push('softbreak', 'br', 0);
}
}
pos++;
// skip heading spaces for next line
while (pos < max && state.md.utils.isSpace(state.src.charCodeAt(pos))) {
if (token) {
token.leading_space = true;
}
pos++;
}
state.pos = pos;
return true;
};
export function setup(helper) {
helper.registerPlugin(md => {
md.inline.ruler.at('newline', newline);
});
}

View File

@ -19,9 +19,14 @@ function applyOnebox(state, silent) {
if (child.type === "link_open" && child.markup === 'linkify' && child.info === 'auto') {
// look behind for soft or hard break
if (j > 0 && token.children[j-1].tag !== 'br') {
if (j === 0 && token.leading_space) {
continue;
} else if (j > 0) {
let prevSibling = token.children[j-1];
if (prevSibling.tag !== 'br' || prevSibling.leading_space) {
continue;
}
}
// look ahead for soft or hard break

View File

@ -0,0 +1,80 @@
// see: https://github.com/markdown-it/markdown-it/issues/375
//
// we use a custom paragraph rule cause we have to signal when a
// link starts with a space, so we can bypass a onebox
// this is a freedom patch, so careful, may break on updates
function paragraph(state, startLine/*, endLine*/) {
var content, terminate, i, l, token, oldParentType,
nextLine = startLine + 1,
terminatorRules = state.md.block.ruler.getRules('paragraph'),
endLine = state.lineMax,
hasLeadingSpace = false;
oldParentType = state.parentType;
state.parentType = 'paragraph';
// jump line-by-line until empty one or EOF
for (; nextLine < endLine && !state.isEmpty(nextLine); nextLine++) {
// this would be a code block normally, but after paragraph
// it's considered a lazy continuation regardless of what's there
if (state.sCount[nextLine] - state.blkIndent > 3) { continue; }
// quirk for blockquotes, this line should already be checked by that rule
if (state.sCount[nextLine] < 0) { continue; }
// Some tags can terminate paragraph without empty line.
terminate = false;
for (i = 0, l = terminatorRules.length; i < l; i++) {
if (terminatorRules[i](state, nextLine, endLine, true)) {
terminate = true;
break;
}
}
if (terminate) { break; }
}
// START CUSTOM CODE
content = state.getLines(startLine, nextLine, state.blkIndent, false);
i=0;
let contentLength = content.length;
while (i < contentLength) {
let chr = content.charCodeAt(i);
if (chr === 0x0A) {
hasLeadingSpace = false;
} else if (state.md.utils.isWhiteSpace(chr)) {
hasLeadingSpace = true;
} else {
break;
}
i++;
}
content = content.trim();
// END CUSTOM CODE
state.line = nextLine;
token = state.push('paragraph_open', 'p', 1);
token.map = [ startLine, state.line ];
// CUSTOM
token.leading_space = hasLeadingSpace;
token = state.push('inline', '', 0);
token.content = content;
token.map = [ startLine, state.line ];
token.children = [];
// CUSTOM
token.leading_space = hasLeadingSpace;
token = state.push('paragraph_close', 'p', -1);
state.parentType = oldParentType;
return true;
};
export function setup(helper) {
helper.registerPlugin(md => {
md.block.ruler.at('paragraph', paragraph);
});
}

View File

@ -619,7 +619,6 @@ HTML
# we expect 2 oneboxes
expect(PrettyText.cook("http://a.com\nhttp://b.com").split("onebox").length).to eq(3)
expect(PrettyText.cook("http://a.com\n\nhttp://b.com").split("onebox").length).to eq(3)
expect(PrettyText.cook("a\nhttp://a.com")).to include('onebox')
expect(PrettyText.cook("> http://a.com")).not_to include('onebox')
expect(PrettyText.cook("a\nhttp://a.com a")).not_to include('onebox')
@ -630,6 +629,8 @@ HTML
expect(PrettyText.cook("http://a.com a")).not_to include('onebox')
expect(PrettyText.cook("- http://a.com")).not_to include('onebox')
expect(PrettyText.cook("<http://a.com>")).not_to include('onebox')
expect(PrettyText.cook(" http://a.com")).not_to include('onebox')
expect(PrettyText.cook("a\n http://a.com")).not_to include('onebox')
end
it "can handle bbcode" do