From ee0d3f15c1914bd494ebe88a5831d40be88bbcae Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 1 Feb 2018 13:22:38 +1100 Subject: [PATCH] FEATURE: allow better fidelity for auto linkify, disable most tlds based linkify New site settings: enable_markdown_linkify: which is default on, auto links https:// and http:// and mail:// markdown_linkify_tlds: which allows control of what tlds get autolinked for cases such as www.site.com, default is com|net|gov --- .../engines/discourse-markdown-it.js.es6 | 4 +- config/locales/server.en.yml | 2 + config/site_settings.yml | 7 ++++ lib/pretty_text.rb | 1 - spec/components/pretty_text_spec.rb | 40 +++++++++++++++++++ spec/integrity/common_mark_spec.rb | 3 +- 6 files changed, 54 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown-it.js.es6 b/app/assets/javascripts/pretty-text/engines/discourse-markdown-it.js.es6 index 4af5b67f08b..0565004f93c 100644 --- a/app/assets/javascripts/pretty-text/engines/discourse-markdown-it.js.es6 +++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown-it.js.es6 @@ -215,10 +215,12 @@ export function setup(opts, siteSettings, state) { html: true, breaks: opts.discourse.features.newline, xhtmlOut: false, - linkify: opts.discourse.features.linkify, + linkify: siteSettings.enable_markdown_linkify, typographer: siteSettings.enable_markdown_typographer }); + opts.engine.linkify.tlds(siteSettings.markdown_linkify_tlds.split('|')); + setupUrlDecoding(opts.engine); setupHoister(opts.engine); setupImageDimensions(opts.engine); diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 7b162be31ee..cce71de7975 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1066,6 +1066,8 @@ en: traditional_markdown_linebreaks: "Use traditional linebreaks in Markdown, which require two trailing spaces for a linebreak." enable_markdown_typographer: "Use basic typography rules to improve text readability of paragraphs of text, replaces (c) (tm) etc, with symbols, reduces number of question marks and so on" + enable_markdown_linkify: "Automatically treat text that looks like a link as a link: www.site.com and http://site.com will be automatically linked" + markdown_linkify_tlds: "List of top level domains that get automatically treated as links" post_undo_action_window_mins: "Number of minutes users are allowed to undo recent actions on a post (like, flag, etc)." must_approve_users: "Staff must approve all new user accounts before they are allowed to access the site. WARNING: enabling this for a live site will revoke access for existing non-staff users!" pending_users_reminder_delay: "Notify moderators if new users have been waiting for approval for longer than this many hours. Set to -1 to disable notifications." diff --git a/config/site_settings.yml b/config/site_settings.yml index 1b19897afcc..8d686b52aed 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -531,6 +531,13 @@ posting: enable_markdown_typographer: client: true default: true + enable_markdown_linkify: + client: true + default: true + markdown_linkify_tlds: + client: true + type: list + default: 'com|gov|net' enable_rich_text_paste: client: true default: false diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb index 7655c76c8d5..fafc6c7eb40 100644 --- a/lib/pretty_text.rb +++ b/lib/pretty_text.rb @@ -167,7 +167,6 @@ module PrettyText __optInput.emojiUnicodeReplacer = __emojiUnicodeReplacer; __optInput.lookupInlineOnebox = __lookupInlineOnebox; __optInput.lookupImageUrls = __lookupImageUrls; - #{opts[:linkify] == false ? "__optInput.linkify = false;" : ""} __optInput.censoredWords = #{WordWatcher.words_for_action(:censor).join('|').to_json}; JS diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index 6e0ee7e0458..971d7dd6005 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -1237,4 +1237,44 @@ HTML end + it "You can disable linkify" do + md = "www.cnn.com test.it http://test.com https://test.ab https://a" + cooked = PrettyText.cook(md) + + html = <<~HTML +

www.cnn.com test.it http://test.com https://test.ab https://a

+ HTML + + expect(cooked).to eq(html.strip) + + # notice how cnn.com is no longer linked but it is + SiteSetting.markdown_linkify_tlds = "not_com|it" + + cooked = PrettyText.cook(md) + html = <<~HTML +

www.cnn.com test.it http://test.com https://test.ab https://a

+ HTML + + expect(cooked).to eq(html.strip) + + # no tlds anymore + SiteSetting.markdown_linkify_tlds = "" + + cooked = PrettyText.cook(md) + html = <<~HTML +

www.cnn.com test.it http://test.com https://test.ab https://a

+ HTML + + expect(cooked).to eq(html.strip) + + # lastly ... what about no linkify + SiteSetting.enable_markdown_linkify = false + + cooked = PrettyText.cook(md) + + html = <<~HTML +

www.cnn.com test.it http://test.com https://test.ab https://a

+ HTML + end + end diff --git a/spec/integrity/common_mark_spec.rb b/spec/integrity/common_mark_spec.rb index 063e6ddff92..89009866619 100644 --- a/spec/integrity/common_mark_spec.rb +++ b/spec/integrity/common_mark_spec.rb @@ -27,7 +27,8 @@ describe "CommonMark" do html.gsub!('
', '
') html.gsub!(/]+) \/>/, "") - cooked = PrettyText.markdown(md, sanitize: false, linkify: false) + SiteSetting.enable_markdown_linkify = false + cooked = PrettyText.markdown(md, sanitize: false) cooked.strip! cooked.gsub!(" class=\"lang-auto\"", '') cooked.gsub!(/(.*)<\/span>/, "\\1")