From 3ba0cfacbc129f9184c6c7c4b7d995d81ebc061f Mon Sep 17 00:00:00 2001 From: Ansel Halliburton Date: Fri, 25 Sep 2015 23:48:41 -0700 Subject: [PATCH 1/8] WIP: Add automatic hyperlinking --- index.js | 22 +++++++++++++++++++--- package.json | 3 ++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index e7b6932..369e4f8 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,22 @@ var escape = require('escape-html') var group = require('commonform-group-series') var predicate = require('commonform-predicate') +var urlRegex = require('url-regex') + +function linkifyOne(text, link) { + return text.replace(link, "" + link + "") +} + +function linkify(text) { + console.log("linkify() called") + if (urlRegex().test(text)) { + var matches = text.match(urlRegex()) + matches.forEach( function(link) { text = linkifyOne(text, link) } ) + return text + } else { + return text + } +} function renderParagraph(paragraph, blanks, html5) { return ( @@ -8,7 +24,7 @@ function renderParagraph(paragraph, blanks, html5) { paragraph.content .map(function(element) { if (predicate.text(element)) { - return escape(element) } + return escape(linkify(element)) } else if (predicate.use(element)) { return ( '' + @@ -23,8 +39,8 @@ function renderParagraph(paragraph, blanks, html5) { return ( '' + ( element.blank in blanks ? - escape(blanks[element.blank]) : - escape(element.blank) ) + + escape(blanks[linkify(element.blank)]) : + escape(linkify(element.blank)) ) + '' ) } else if (predicate.reference(element)) { return ( diff --git a/package.json b/package.json index c84adc8..a2eb3ea 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "dependencies": { "commonform-group-series": "^0.5.0", "commonform-predicate": "^0.4.0", - "escape-html": "^1.0.2" + "escape-html": "^1.0.2", + "url-regex": "^3.0.0" }, "devDependencies": { "defence-cli": "^1.0.1", From 79c0c0814ed7f1b8fdec506e8c450d7bc7775f0b Mon Sep 17 00:00:00 2001 From: Ansel Halliburton Date: Sat, 26 Sep 2015 00:00:23 -0700 Subject: [PATCH 2/8] Add test for hyperlinks --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 372a898..641cc63 100644 --- a/README.md +++ b/README.md @@ -106,3 +106,10 @@ assert.equal( .join('') ) ``` +Hyperlinks will be encoded automatically: + +```javascript +assert.equal( + html({ content: [ 'This is a great website: http://example.com/' ] }), + '

Just a great website:http://example.com/

') +``` From 9b1567c7d36cde6f3b1287014078b4309fdff950 Mon Sep 17 00:00:00 2001 From: Ansel Halliburton Date: Sat, 26 Sep 2015 00:04:20 -0700 Subject: [PATCH 3/8] Reorder escape() and linkify() calls --- index.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 369e4f8..3af3159 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,6 @@ function linkifyOne(text, link) { } function linkify(text) { - console.log("linkify() called") if (urlRegex().test(text)) { var matches = text.match(urlRegex()) matches.forEach( function(link) { text = linkifyOne(text, link) } ) @@ -24,7 +23,7 @@ function renderParagraph(paragraph, blanks, html5) { paragraph.content .map(function(element) { if (predicate.text(element)) { - return escape(linkify(element)) } + return linkify(escape(element)) } else if (predicate.use(element)) { return ( '' + @@ -39,8 +38,8 @@ function renderParagraph(paragraph, blanks, html5) { return ( '' + ( element.blank in blanks ? - escape(blanks[linkify(element.blank)]) : - escape(linkify(element.blank)) ) + + linkify(escape(blanks[element.blank])) : + linkify(escape(element.blank)) ) + '' ) } else if (predicate.reference(element)) { return ( From 8c58df917a7e44294aff444a7a0b48ccd3546848 Mon Sep 17 00:00:00 2001 From: Ansel Halliburton Date: Sat, 26 Sep 2015 00:06:05 -0700 Subject: [PATCH 4/8] Fix hyperlink test in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 641cc63..c50016c 100644 --- a/README.md +++ b/README.md @@ -111,5 +111,5 @@ Hyperlinks will be encoded automatically: ```javascript assert.equal( html({ content: [ 'This is a great website: http://example.com/' ] }), - '

Just a great website:http://example.com/

') + '

This is a great website: http://example.com/

') ``` From d36e3d61c6e9bc754bcd567f4bdbfdf77bbcf0e8 Mon Sep 17 00:00:00 2001 From: Ansel Halliburton Date: Wed, 7 Oct 2015 15:47:47 -0700 Subject: [PATCH 5/8] Add .gitignore Ignore node_modules/! --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ From 1e09c099536b063771744c5236e28648ae2d7ddc Mon Sep 17 00:00:00 2001 From: Ansel Halliburton Date: Wed, 7 Oct 2015 16:43:33 -0700 Subject: [PATCH 6/8] Remove peerDependencies --- package.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/package.json b/package.json index a2eb3ea..153aba4 100644 --- a/package.json +++ b/package.json @@ -14,9 +14,6 @@ "replace-require-self": "^1.0.0" }, "license": "Apache-2.0", - "peerDependencies": { - "commonform": "1.x" - }, "repository": "commonform/commonform-html", "scripts": { "test": "defence README.md | replace-require-self | node" From 26d410c430edf36d5267353d72c147f8f900a4ba Mon Sep 17 00:00:00 2001 From: Ansel Halliburton Date: Wed, 7 Oct 2015 16:44:02 -0700 Subject: [PATCH 7/8] WIP: Use html-linkify instead of url-regex (Fails!) --- README.md | 13 +++++++++++++ index.js | 21 ++++----------------- package.json | 2 +- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index c50016c..9f448fc 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,19 @@ assert.equal( Hyperlinks will be encoded automatically: ```javascript + +/* +BUG: This doesn't work! + +var plaintext = 'This is a great website: http://example.com/' + +console.log(plaintext) + > This is a great website: http://example.com/ + +console.log( html( { content: [ plaintext ] } ) ) + >

This is a great website: http://example.com/">http://example.com/

+*/ + assert.equal( html({ content: [ 'This is a great website: http://example.com/' ] }), '

This is a great website: http://example.com/

') diff --git a/index.js b/index.js index 3af3159..832e02b 100644 --- a/index.js +++ b/index.js @@ -1,21 +1,7 @@ var escape = require('escape-html') var group = require('commonform-group-series') var predicate = require('commonform-predicate') -var urlRegex = require('url-regex') - -function linkifyOne(text, link) { - return text.replace(link, "" + link + "") -} - -function linkify(text) { - if (urlRegex().test(text)) { - var matches = text.match(urlRegex()) - matches.forEach( function(link) { text = linkifyOne(text, link) } ) - return text - } else { - return text - } -} +var linkify = require('html-linkify') function renderParagraph(paragraph, blanks, html5) { return ( @@ -94,7 +80,7 @@ module.exports = function commonformHTML(form, blanks, options) { var html5 = ( 'html5' in options && options.html5 === true ) var title = ( 'title' in options ? options.title : false ) - return ( + return ( linkify( ( html5 ? ( form.conspicuous ? '
' : @@ -104,4 +90,5 @@ module.exports = function commonformHTML(form, blanks, options) { '
' ) ) + ( title ? ( '

' + escape(title) + '

' ) : '' ) + renderForm(( title ? 1 : 0 ), form, blanks, html5) + - ( html5 ? '
' : '' ) ) } + ( html5 ? '' : '' ), + { escape: false } ) ) } diff --git a/package.json b/package.json index 153aba4..5a2934d 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "commonform-group-series": "^0.5.0", "commonform-predicate": "^0.4.0", "escape-html": "^1.0.2", - "url-regex": "^3.0.0" + "html-linkify": "^1.2.1" }, "devDependencies": { "defence-cli": "^1.0.1", From 1fdb62ca90ba2caf8fb7851c64e6adf0baca114a Mon Sep 17 00:00:00 2001 From: Ansel Halliburton Date: Wed, 7 Oct 2015 16:50:54 -0700 Subject: [PATCH 8/8] WIP: Add email linking test (Fails!) --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 9f448fc..f3b572f 100644 --- a/README.md +++ b/README.md @@ -126,3 +126,11 @@ assert.equal( html({ content: [ 'This is a great website: http://example.com/' ] }), '

This is a great website: http://example.com/

') ``` + +Emails should also be linked: + +```javascript +assert.equal( + html({ content: [ 'Email bob@example.com for more information.' ] }), + '

Email bob@example.com for more information.

') +```