Skip inline onebox for domain only
This commit is contained in:
parent
65d5cd7239
commit
5942ad83c0
|
@ -4,6 +4,12 @@ import { cachedInlineOnebox } from 'pretty-text/inline-oneboxer';
|
|||
const ONEBOX = 1;
|
||||
const INLINE = 2;
|
||||
|
||||
function isTopLevel(href) {
|
||||
let split = href.split(/https?:\/\/[^\/]+[\/?]/i);
|
||||
let hasExtra = split && split[1] && split[1].length > 0;
|
||||
return !hasExtra;
|
||||
}
|
||||
|
||||
function applyOnebox(state, silent) {
|
||||
if (silent || !state.tokens) {
|
||||
return;
|
||||
|
@ -87,17 +93,20 @@ function applyOnebox(state, silent) {
|
|||
attrs.push(["target", "_blank"]);
|
||||
}
|
||||
} else if (mode === INLINE) {
|
||||
let onebox = cachedInlineOnebox(href);
|
||||
|
||||
let options = state.md.options.discourse;
|
||||
if (options.lookupInlineOnebox) {
|
||||
onebox = options.lookupInlineOnebox(href);
|
||||
}
|
||||
if (!isTopLevel(href)) {
|
||||
let onebox = cachedInlineOnebox(href);
|
||||
|
||||
if (onebox) {
|
||||
text.content = onebox.title;
|
||||
} else if (state.md.options.discourse.previewing) {
|
||||
attrs.push(["class", "inline-onebox-loading"]);
|
||||
let options = state.md.options.discourse;
|
||||
if (options.lookupInlineOnebox) {
|
||||
onebox = options.lookupInlineOnebox(href);
|
||||
}
|
||||
|
||||
if (onebox) {
|
||||
text.content = onebox.title;
|
||||
} else if (state.md.options.discourse.previewing) {
|
||||
attrs.push(["class", "inline-onebox-loading"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -840,15 +840,43 @@ HTML
|
|||
|
||||
it 'handles mini onebox' do
|
||||
SiteSetting.enable_inline_onebox_on_all_domains = true
|
||||
InlineOneboxer.purge("http://cnn.com")
|
||||
InlineOneboxer.purge("http://cnn.com/a")
|
||||
|
||||
stub_request(:head, "http://cnn.com").to_return(status: 200)
|
||||
stub_request(:head, "http://cnn.com/a").to_return(status: 200)
|
||||
|
||||
stub_request(:get, "http://cnn.com").
|
||||
stub_request(:get, "http://cnn.com/a").
|
||||
to_return(status: 200, body: "<html><head><title>news</title></head></html>", headers: {})
|
||||
|
||||
expect(PrettyText.cook("- http://cnn.com\n- a http://cnn.com").split("news").length).to eq(3)
|
||||
expect(PrettyText.cook("- http://cnn.com\n - a http://cnn.com").split("news").length).to eq(3)
|
||||
expect(PrettyText.cook("- http://cnn.com/a\n- a http://cnn.com/a").split("news").length).to eq(3)
|
||||
expect(PrettyText.cook("- http://cnn.com/a\n - a http://cnn.com/a").split("news").length).to eq(3)
|
||||
end
|
||||
|
||||
it 'handles mini onebox with query param' do
|
||||
SiteSetting.enable_inline_onebox_on_all_domains = true
|
||||
InlineOneboxer.purge("http://cnn.com?a")
|
||||
|
||||
stub_request(:head, "http://cnn.com?a").to_return(status: 200)
|
||||
|
||||
stub_request(:get, "http://cnn.com?a").
|
||||
to_return(status: 200, body: "<html><head><title>news</title></head></html>", headers: {})
|
||||
|
||||
expect(PrettyText.cook("- http://cnn.com?a\n- a http://cnn.com?a").split("news").length).to eq(3)
|
||||
expect(PrettyText.cook("- http://cnn.com?a\n - a http://cnn.com?a").split("news").length).to eq(3)
|
||||
end
|
||||
|
||||
it 'skips mini onebox for primary domain' do
|
||||
|
||||
# we only include mini onebox if there is something in path or query params
|
||||
|
||||
SiteSetting.enable_inline_onebox_on_all_domains = true
|
||||
InlineOneboxer.purge("http://cnn.com/")
|
||||
|
||||
stub_request(:head, "http://cnn.com/").to_return(status: 200)
|
||||
stub_request(:get, "http://cnn.com/").
|
||||
to_return(status: 200, body: "<html><head><title>news</title></head></html>", headers: {})
|
||||
|
||||
expect(PrettyText.cook("- http://cnn.com/\n- a http://cnn.com/").split("news").length).to eq(1)
|
||||
expect(PrettyText.cook("- cnn.com\n - a http://cnn.com/").split("news").length).to eq(1)
|
||||
end
|
||||
|
||||
it "can handle bbcode" do
|
||||
|
|
Loading…
Reference in New Issue