Correct logic so hashtags are properly ignored in links

This commit is contained in:
Sam 2017-06-29 12:48:04 -04:00
parent 6ff7bf7505
commit c6b83f4714
2 changed files with 45 additions and 10 deletions

View File

@ -17,10 +17,10 @@ export function inlineRegexRule(md, options) {
const start = options.start.charCodeAt(0); const start = options.start.charCodeAt(0);
const maxLength = (options.maxLength || 500) + 1; const maxLength = (options.maxLength || 500) + 1;
return function(state) { return function(state, silent) {
const pos = state.pos; const pos = state.pos;
if (state.src.charCodeAt(pos) !== start) { if (state.src.charCodeAt(pos) !== start || silent) {
return false; return false;
} }
@ -34,18 +34,19 @@ export function inlineRegexRule(md, options) {
// skip if in a link // skip if in a link
if (options.skipInLink && state.tokens) { if (options.skipInLink && state.tokens) {
let last = state.tokens[state.tokens.length-1]; let i;
if (last) { for(i=state.tokens.length-1;i>=0;i--) {
if (last.type === 'link_open') { let token = state.tokens[i];
let type = token.type;
if (type === 'link_open' || (type === 'html_inline' && token.content.substr(0,2) === "<a")) {
return false; return false;
} }
if (last.type === 'html_inline' && last.content.substr(0,2) === "<a") { if (type.block || type === 'link_close' || (type === 'html_inline' && token.content.substr(0,3).toLowerCase() === "</a>")) {
return false; break;
} }
} }
} }
const substr = state.src.slice(pos, Math.min(pos + maxLength,state.posMax)); const substr = state.src.slice(pos, Math.min(pos + maxLength,state.posMax));
const matches = options.matcher.exec(substr); const matches = options.matcher.exec(substr);

View File

@ -473,7 +473,14 @@ HTML
describe "tag and category links" do describe "tag and category links" do
it "produces tag links" do it "produces tag links" do
Fabricate(:topic, {tags: [Fabricate(:tag, name: 'known')]}) Fabricate(:topic, {tags: [Fabricate(:tag, name: 'known')]})
expect(PrettyText.cook(" #unknown::tag #known::tag")).to match_html("<p> <span class=\"hashtag\">#unknown::tag</span> <a class=\"hashtag\" href=\"http://test.localhost/tags/known\">#<span>known</span></a></p>")
cooked = PrettyText.cook(" #unknown::tag #known::tag")
html = <<~HTML
<p> <span class=\"hashtag\">#unknown::tag</span> <a class=\"hashtag\" href=\"http://test.localhost/tags/known\">#<span>known</span></a></p>
HTML
expect(cooked).to match_html(html)
end end
# TODO does it make sense to generate hashtags for tags that are missing in action? # TODO does it make sense to generate hashtags for tags that are missing in action?
@ -527,7 +534,33 @@ HTML
it "produces tag links" do it "produces tag links" do
Fabricate(:topic, {tags: [Fabricate(:tag, name: 'known')]}) Fabricate(:topic, {tags: [Fabricate(:tag, name: 'known')]})
expect(PrettyText.cook("x #unknown::tag #known::tag")).to match_html("<p>x <span class=\"hashtag\">#unknown::tag</span> <a class=\"hashtag\" href=\"http://test.localhost/tags/known\">#<span>known</span></a></p>")
cooked = PrettyText.cook(" #unknown::tag #known::tag")
html = <<~HTML
<p><span class=\"hashtag\">#unknown::tag</span> <a class=\"hashtag\" href=\"http://test.localhost/tags/known\">#<span>known</span></a></p>
HTML
expect(cooked).to eq(html.strip)
cooked = PrettyText.cook("[`a` #known::tag here](http://somesite.com)")
html = <<~HTML
<p><a href="http://somesite.com" rel="nofollow noopener"><code>a</code> #known::tag here</a></p>
HTML
expect(cooked).to eq(html.strip)
cooked = PrettyText.cook("<a href='http://somesite.com'>`a` #known::tag here</a>")
expect(cooked).to eq(html.strip)
cooked = PrettyText.cook("<A href='/a'>test</A> #known::tag")
html = <<~HTML
<p><a href="/a">test</a> <a class="hashtag" href="http://test.localhost/tags/known">#<span>known</span></a></p>
HTML
expect(cooked).to eq(html.strip)
end end
it "can handle mixed lists" do it "can handle mixed lists" do
@ -680,6 +713,7 @@ HTML
end end
it "supports tables" do it "supports tables" do
markdown = <<~MD markdown = <<~MD
| Tables | Are | Cool | | Tables | Are | Cool |
| ------------- |:-------------:| -----:| | ------------- |:-------------:| -----:|