diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/README.md b/README.md index 372a898..f3b572f 100644 --- a/README.md +++ b/README.md @@ -106,3 +106,31 @@ assert.equal( .join('') ) ``` +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/

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

Email bob@example.com for more information.

') +``` diff --git a/index.js b/index.js index e7b6932..832e02b 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ var escape = require('escape-html') var group = require('commonform-group-series') var predicate = require('commonform-predicate') +var linkify = require('html-linkify') function renderParagraph(paragraph, blanks, html5) { return ( @@ -8,7 +9,7 @@ function renderParagraph(paragraph, blanks, html5) { paragraph.content .map(function(element) { if (predicate.text(element)) { - return escape(element) } + return linkify(escape(element)) } else if (predicate.use(element)) { return ( '' + @@ -23,8 +24,8 @@ function renderParagraph(paragraph, blanks, html5) { return ( '' + ( element.blank in blanks ? - escape(blanks[element.blank]) : - escape(element.blank) ) + + linkify(escape(blanks[element.blank])) : + linkify(escape(element.blank)) ) + '' ) } else if (predicate.reference(element)) { return ( @@ -79,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 ? '
' : @@ -89,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 c84adc8..5a2934d 100644 --- a/package.json +++ b/package.json @@ -6,16 +6,14 @@ "dependencies": { "commonform-group-series": "^0.5.0", "commonform-predicate": "^0.4.0", - "escape-html": "^1.0.2" + "escape-html": "^1.0.2", + "html-linkify": "^1.2.1" }, "devDependencies": { "defence-cli": "^1.0.1", "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"