From 59053cff4b89be249dedb822672ccca5d6bfba33 Mon Sep 17 00:00:00 2001 From: Christian Faber Date: Mon, 12 Feb 2024 17:04:14 +0100 Subject: [PATCH 1/2] Add httpsAutoLinks option to use https as default when creating links with missing protocol information --- src/options.js | 5 +++++ src/subParsers/makehtml/links.js | 4 ++-- .../makehtml/cases/features/#806.https-auto-link.html | 2 ++ .../makehtml/cases/features/#806.https-auto-link.md | 3 +++ test/functional/makehtml/testsuite.features.js | 2 ++ 5 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 test/functional/makehtml/cases/features/#806.https-auto-link.html create mode 100644 test/functional/makehtml/cases/features/#806.https-auto-link.md diff --git a/src/options.js b/src/options.js index 19deb6672..68105685f 100644 --- a/src/options.js +++ b/src/options.js @@ -51,6 +51,11 @@ function getDefaultOpts (simple) { describe: 'Turn on/off GFM autolink style', type: 'boolean' }, + httpsAutoLinks: { + defaultValue: false, + describe: 'Use \'https://\' for auto-generated links', + type: 'string' + }, literalMidWordUnderscores: { defaultValue: false, describe: 'Parse midword underscores as literal underscores', diff --git a/src/subParsers/makehtml/links.js b/src/subParsers/makehtml/links.js index cac80da7b..4c7f53057 100644 --- a/src/subParsers/makehtml/links.js +++ b/src/subParsers/makehtml/links.js @@ -302,7 +302,7 @@ var urlRgx = /<(((?:https?|ftp):\/\/|www\.)[^'">\s]+)>/gi; text = text.replace(urlRgx, function (wholeMatch, url, urlStart) { var text = url; - url = (urlStart === 'www.') ? 'http://' + url : url; + url = (urlStart === 'www.') ? (options.httpsAutoLinks ? 'https://' : 'http://') + url : url; var evt = createEvent(urlRgx, evtRootName + '.captureStart', wholeMatch, text, null, url, null, options, globals); return writeAnchorTag(evt, options, globals); }); @@ -392,7 +392,7 @@ // we copy the treated url to the text variable var text = url; // finally, if it's a www shortcut, we prepend http - url = (urlPrefix === 'www.') ? 'http://' + url : url; + url = (urlPrefix === 'www.') ? (options.httpsAutoLinks ? 'https://' : 'http://') + url : url; // url part is done so let's take care of text now // we need to escape the text (because of links such as www.example.com/foo__bar__baz) diff --git a/test/functional/makehtml/cases/features/#806.https-auto-link.html b/test/functional/makehtml/cases/features/#806.https-auto-link.html new file mode 100644 index 000000000..7a14598df --- /dev/null +++ b/test/functional/makehtml/cases/features/#806.https-auto-link.html @@ -0,0 +1,2 @@ +

www.foobar.com

+

This is a link www.foobar.com

\ No newline at end of file diff --git a/test/functional/makehtml/cases/features/#806.https-auto-link.md b/test/functional/makehtml/cases/features/#806.https-auto-link.md new file mode 100644 index 000000000..42306f125 --- /dev/null +++ b/test/functional/makehtml/cases/features/#806.https-auto-link.md @@ -0,0 +1,3 @@ +www.foobar.com + +This is a link \ No newline at end of file diff --git a/test/functional/makehtml/testsuite.features.js b/test/functional/makehtml/testsuite.features.js index 2ef6c38ea..90a85f30a 100644 --- a/test/functional/makehtml/testsuite.features.js +++ b/test/functional/makehtml/testsuite.features.js @@ -64,6 +64,8 @@ describe('makeHtml() features testsuite', function () { converter = new showdown.Converter({simpleLineBreaks: true}); } else if (testsuite[i].name === '#318.simpleLineBreaks-does-not-work-with-chinese-characters') { converter = new showdown.Converter({simpleLineBreaks: true}); + } else if (testsuite[i].name === '#806.https-auto-link') { + converter = new showdown.Converter({simplifiedAutoLink: true, httpsAutoLinks: true}); } else if (testsuite[i].name === 'simpleLineBreaks-handle-html-pre') { converter = new showdown.Converter({simpleLineBreaks: true}); } else if (testsuite[i].name === 'excludeTrailingPunctuationFromURLs-option') { From 452f2e4ee9482cee78dbd4bf372667ab5c0af6e6 Mon Sep 17 00:00:00 2001 From: Christian Faber Date: Tue, 20 Feb 2024 12:33:17 +0100 Subject: [PATCH 2/2] Fix incorrect options type for httpsAutoLinks --- src/options.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/options.js b/src/options.js index 68105685f..30e346db7 100644 --- a/src/options.js +++ b/src/options.js @@ -54,7 +54,7 @@ function getDefaultOpts (simple) { httpsAutoLinks: { defaultValue: false, describe: 'Use \'https://\' for auto-generated links', - type: 'string' + type: 'boolean' }, literalMidWordUnderscores: { defaultValue: false,