Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add automatic hyperlinks #1

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ] } ) )
> <div class="article"><p>This is a great website: <a href="<a href="http://example.com/">http://example.com/</a>"><a href="http://example.com/">http://example.com/</a></a></p></div>
*/

assert.equal(
html({ content: [ 'This is a great website: http://example.com/' ] }),
'<div class="article"><p>This is a great website: <a href="http://example.com/">http://example.com/</a></p></div>')
```

Emails should also be linked:

```javascript
assert.equal(
html({ content: [ 'Email [email protected] for more information.' ] }),
'<div class="article"><p>Email <a href="mailto:[email protected]">[email protected]</a> for more information.</a></p></div>')
```
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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 (
'<p>' +
paragraph.content
.map(function(element) {
if (predicate.text(element)) {
return escape(element) }
return linkify(escape(element)) }
else if (predicate.use(element)) {
return (
'<span class="term">' +
Expand All @@ -23,8 +24,8 @@ function renderParagraph(paragraph, blanks, html5) {
return (
'<span class="blank">' +
( element.blank in blanks ?
escape(blanks[element.blank]) :
escape(element.blank) ) +
linkify(escape(blanks[element.blank])) :
linkify(escape(element.blank)) ) +
'</span>' ) }
else if (predicate.reference(element)) {
return (
Expand Down Expand Up @@ -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 ?
'<article class="conspicuous">' :
Expand All @@ -89,4 +90,5 @@ module.exports = function commonformHTML(form, blanks, options) {
'<div class="article">' ) ) +
( title ? ( '<h1>' + escape(title) + '</h1>' ) : '' ) +
renderForm(( title ? 1 : 0 ), form, blanks, html5) +
( html5 ? '</article>' : '</div>' ) ) }
( html5 ? '</article>' : '</div>' ),
{ escape: false } ) ) }
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down