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
This commit is contained in:
Sam 2018-02-01 13:22:38 +11:00
parent f2e7b74d88
commit ee0d3f15c1
6 changed files with 54 additions and 3 deletions

View File

@ -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);

View File

@ -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."

View File

@ -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

View File

@ -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

View File

@ -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
<p><a href="http://www.cnn.com" rel="nofollow noopener">www.cnn.com</a> test.it <a href="http://test.com" rel="nofollow noopener">http://test.com</a> <a href="https://test.ab" rel="nofollow noopener">https://test.ab</a> <a href="https://a" rel="nofollow noopener">https://a</a></p>
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
<p>www.cnn.com <a href="http://test.it" rel="nofollow noopener">test.it</a> <a href="http://test.com" rel="nofollow noopener">http://test.com</a> <a href="https://test.ab" rel="nofollow noopener">https://test.ab</a> <a href="https://a" rel="nofollow noopener">https://a</a></p>
HTML
expect(cooked).to eq(html.strip)
# no tlds anymore
SiteSetting.markdown_linkify_tlds = ""
cooked = PrettyText.cook(md)
html = <<~HTML
<p>www.cnn.com test.it <a href="http://test.com" rel="nofollow noopener">http://test.com</a> <a href="https://test.ab" rel="nofollow noopener">https://test.ab</a> <a href="https://a" rel="nofollow noopener">https://a</a></p>
HTML
expect(cooked).to eq(html.strip)
# lastly ... what about no linkify
SiteSetting.enable_markdown_linkify = false
cooked = PrettyText.cook(md)
html = <<~HTML
<p>www.cnn.com test.it http://test.com https://test.ab https://a</p>
HTML
end
end

View File

@ -27,7 +27,8 @@ describe "CommonMark" do
html.gsub!('<hr />', '<hr>')
html.gsub!(/<img([^>]+) \/>/, "<img\\1>")
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 class="hashtag">(.*)<\/span>/, "\\1")