mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 09:36:19 -05:00
FIX markdown hotlinked images were not properly pulled
This commit is contained in:
parent
37267f36a6
commit
31fd5b85bc
7 changed files with 96 additions and 16 deletions
|
@ -1,6 +1,6 @@
|
|||
module CommonHelper
|
||||
def render_google_analytics_code
|
||||
if Rails.env == "production" && SiteSetting.ga_tracking_code.present?
|
||||
if Rails.env == "production" && SiteSetting.ga_tracking_code.present?
|
||||
render partial: "common/google_analytics"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
module ComposerMessagesHelper
|
||||
end
|
|
@ -1,6 +1,9 @@
|
|||
require_dependency 'url_helper'
|
||||
|
||||
module Jobs
|
||||
|
||||
class PullHotlinkedImages < Jobs::Base
|
||||
include UrlHelper
|
||||
|
||||
def initialize
|
||||
# maximum size of the file in bytes
|
||||
|
@ -40,11 +43,13 @@ module Jobs
|
|||
if downloaded_urls[src].present?
|
||||
url = downloaded_urls[src]
|
||||
escaped_src = src.gsub("?", "\\?").gsub(".", "\\.").gsub("+", "\\+")
|
||||
# there are 5 ways to insert an image in a post
|
||||
# there are 6 ways to insert an image in a post
|
||||
# HTML tag - <img src="http://...">
|
||||
raw.gsub!(/src=["']#{escaped_src}["']/i, "src='#{url}'")
|
||||
# BBCode tag - [img]http://...[/img]
|
||||
raw.gsub!(/\[img\]#{escaped_src}\[\/img\]/i, "[img]#{url}[/img]")
|
||||
# Markdown linked image - [![alt](http://...)](http://...)
|
||||
raw.gsub!(/\[!\[([^\]]*)\]\(#{escaped_src}\)\]/) { "[<img src='#{url}' alt='#{$1}'>]" }
|
||||
# Markdown inline - ![alt](http://...)
|
||||
raw.gsub!(/!\[([^\]]*)\]\(#{escaped_src}\)/) { "![#{$1}](#{url})" }
|
||||
# Markdown reference - [x]: http://
|
||||
|
|
|
@ -2,9 +2,11 @@
|
|||
# For example, inserting the onebox content, or image sizes/thumbnails.
|
||||
|
||||
require_dependency "oneboxer"
|
||||
require_dependency 'url_helper'
|
||||
|
||||
class CookedPostProcessor
|
||||
include ActionView::Helpers::NumberHelper
|
||||
include UrlHelper
|
||||
|
||||
def initialize(post, opts={})
|
||||
@dirty = false
|
||||
|
@ -124,10 +126,11 @@ class CookedPostProcessor
|
|||
def is_a_hyperlink?(img)
|
||||
parent = img.parent
|
||||
while parent
|
||||
return if parent.name == "a"
|
||||
return true if parent.name == "a"
|
||||
break unless parent.respond_to? :parent
|
||||
parent = parent.parent
|
||||
end
|
||||
false
|
||||
end
|
||||
|
||||
def add_lightbox!(img, original_width, original_height, upload=nil)
|
||||
|
@ -206,17 +209,6 @@ class CookedPostProcessor
|
|||
end
|
||||
end
|
||||
|
||||
def is_local(url)
|
||||
Discourse.store.has_been_uploaded?(url) || url =~ /^\/assets\//
|
||||
end
|
||||
|
||||
def absolute(url)
|
||||
url =~ /^\/[^\/]/ ? (Discourse.asset_host || Discourse.base_url_no_prefix) + url : url
|
||||
end
|
||||
|
||||
def schemaless(url)
|
||||
url.gsub(/^https?:/, "")
|
||||
end
|
||||
|
||||
def pull_hotlinked_images
|
||||
# is the job enabled?
|
||||
|
|
15
lib/url_helper.rb
Normal file
15
lib/url_helper.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
module UrlHelper
|
||||
|
||||
def is_local(url)
|
||||
Discourse.store.has_been_uploaded?(url) || url =~ /^\/assets\//
|
||||
end
|
||||
|
||||
def absolute(url)
|
||||
url =~ /^\/[^\/]/ ? (Discourse.asset_host || Discourse.base_url_no_prefix) + url : url
|
||||
end
|
||||
|
||||
def schemaless(url)
|
||||
url.gsub(/^https?:/, "")
|
||||
end
|
||||
|
||||
end
|
|
@ -340,4 +340,22 @@ describe CookedPostProcessor do
|
|||
|
||||
end
|
||||
|
||||
context ".is_a_hyperlink?" do
|
||||
|
||||
let(:post) { build(:post) }
|
||||
let(:cpp) { CookedPostProcessor.new(post) }
|
||||
let(:doc) { Nokogiri::HTML::fragment('<body><div><a><img id="linked_image"></a><p><img id="standard_image"></p></div></body>') }
|
||||
|
||||
it "is true when the image is inside a link" do
|
||||
img = doc.css("img#linked_image").first
|
||||
cpp.is_a_hyperlink?(img).should be_true
|
||||
end
|
||||
|
||||
it "is false when the image is not inside a link" do
|
||||
img = doc.css("img#standard_image").first
|
||||
cpp.is_a_hyperlink?(img).should be_false
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
52
spec/components/url_helper_spec.rb
Normal file
52
spec/components/url_helper_spec.rb
Normal file
|
@ -0,0 +1,52 @@
|
|||
require 'spec_helper'
|
||||
require_dependency 'url_helper'
|
||||
|
||||
describe UrlHelper do
|
||||
|
||||
class DummyClass
|
||||
include UrlHelper
|
||||
end
|
||||
|
||||
let(:helper) { DummyClass.new }
|
||||
|
||||
describe "#is_local" do
|
||||
|
||||
it "is true when the file has been uploaded" do
|
||||
store = stub
|
||||
store.expects(:has_been_uploaded?).returns(true)
|
||||
Discourse.stubs(:store).returns(store)
|
||||
helper.is_local("http://discuss.site.com/path/to/file.png").should be_true
|
||||
end
|
||||
|
||||
it "is true for relative assets" do
|
||||
store = stub
|
||||
store.expects(:has_been_uploaded?).returns(false)
|
||||
Discourse.stubs(:store).returns(store)
|
||||
helper.is_local("/assets/javascripts/all.js").should be_true
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "#absolute" do
|
||||
|
||||
it "does not change non-relative url" do
|
||||
helper.absolute("http://www.discourse.org").should == "http://www.discourse.org"
|
||||
end
|
||||
|
||||
it "changes a relative url to an absolute one" do
|
||||
helper.absolute("/path/to/file").should == "http://test.localhost/path/to/file"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "#schemaless" do
|
||||
|
||||
it "removes http or https schemas only" do
|
||||
helper.schemaless("http://www.discourse.org").should == "//www.discourse.org"
|
||||
helper.schemaless("https://secure.discourse.org").should == "//secure.discourse.org"
|
||||
helper.schemaless("ftp://ftp.discourse.org").should == "ftp://ftp.discourse.org"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in a new issue