Skip to content
This repository has been archived by the owner on Jul 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #10 from moose-team/updates
Browse files Browse the repository at this point in the history
make it work better
  • Loading branch information
ungoldman committed Mar 25, 2016
2 parents 1cdcce5 + 5a98f4f commit c6295bb
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 81 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
language: node_js
node_js:
- 'iojs'
sudo: false
- '4'
- '5'
cache:
directories:
- node_modules
script:
- npm test
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# rich-message change log

All notable changes to this project will be documented in this file.

This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased](https://github.com/moose-team/rich-message/compare/v1.0.1...HEAD)

* fix channel name parsing bug ([#8](https://github.com/moose-team/rich-message/issues/8))
* add tests ([#9](https://github.com/moose-team/rich-message/pull/9))
* simplify internal directory structure
* move `cat.png` into `rich-message` module
* update `.travis.yml` to target node 4 & 5, remove outdated optimizations
* add usage example

## [1.0.1](https://github.com/moose-team/rich-message/compare/v1.0.0...v1.0.1)

* fix channel link formatting ([#6](https://github.com/moose-team/rich-message/pull/6))
* use MOOSE Team MIT License ([#7](https://github.com/moose-team/rich-message/pull/7))

Expand Down
4 changes: 2 additions & 2 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)
# [MIT License](https://spdx.org/licenses/MIT)

Copyright (c) MOOSE Team
Copyright (c) 2015-2016 [MOOSE Team](http://moose-team.github.io)

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# rich-message

> Turn a plain message into a rich HTML message
[![npm][npm-image]][npm-url]
[![travis][travis-image]][travis-url]

Expand All @@ -8,8 +10,6 @@
[travis-image]: https://img.shields.io/travis/moose-team/rich-message.svg?style=flat-square
[travis-url]: https://travis-ci.org/moose-team/rich-message



## Install

```
Expand All @@ -20,8 +20,25 @@ npm install rich-message

```js
var richMessage = require('rich-message')
var basicMessage = {
text: 'hi maxogden', // text entered by a user
username: 'mafintosh', // github username
timestamp: Date.now()
}
var username = 'maxogden' // current user's github username (used for highlighting)

var output = richMessage(basicMessage, username)
// { text: 'hi maxogden',
// username: 'mafintosh',
// timestamp: 1458939703123,
// anon: false,
// avatar: 'https://github.com/mafintosh.png',
// timeago: '2:01 PM',
// html: '<div class="highlight"><p>hi maxogden</p><p></p></div>' }
```

This module is currently very tightly coupled with [friends](https://github.com/moose-team/friends) (sorry). If you'd like to help make it more usable for other projects, please fork and contribute!

## Contributing

Contributions welcome! Please read the [contributing guidelines](CONTRIBUTING.md) first.
Expand Down
Binary file added cat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 64 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1,64 @@
module.exports = require('./lib/rich-message.js')
module.exports = makeRichMessage
module.exports.mergeMessages = mergeMessages
module.exports.timeago = timeago

var emoji = require('markdown-it-emoji')
var ghlink = require('ghlink')
var higlight = require('highlight.js')
var MarkdownIt = require('markdown-it')
var twemoji = require('twemoji')
var moment = require('moment')

var CAT_URL = 'https://cdn.rawgit.com/moose-team/rich-message/master/cat.png'

var md = new MarkdownIt({
linkify: true,
highlight: function (str, lang) {
if (lang && higlight.getLanguage(lang)) {
try {
return higlight.highlight(lang, str).value
} catch (err) {}
}

try {
return higlight.highlightAuto(str).value
} catch (err) {}

return '' // use external default escaping
}
}).use(emoji)

md.renderer.rules.emoji = function (token, index) {
return twemoji.parse(token[index].content)
}

function makeRichMessage (message, username) {
message.anon = /Anonymous/i.test(message.username)
message.avatar = message.anon
? CAT_URL
: `https://github.com/${message.username}.png`
message.timeago = timeago(message.timestamp)

message.text = message.text.replace(/(^|\s)(#[a-zA-Z0-9]+)(?=$|\s)/g, '$1[$2]($2)')
message.html = md.render(message.text)
message.html = message.html.replace(/\n/g, '<p></p>')
message.html = ghlink(message.html, { format: 'html' })

var highlight = (message.text.indexOf(username) !== -1)
var classStr = highlight ? ' class="highlight"' : ''
message.html = `<div${classStr}>${message.html}</div>`

return message
}

function mergeMessages (message1, message2) {
message1.text += '\n' + message2.text
message1.html += '<p></p>' + message2.html
return message1
}

function timeago (milliseconds) {
var timeago = moment(milliseconds).calendar()
if (timeago.indexOf('Today at ') === 0) return timeago.substring(9)
return timeago
}
56 changes: 0 additions & 56 deletions lib/rich-message.js

This file was deleted.

7 changes: 0 additions & 7 deletions lib/util.js

This file was deleted.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "rich-message",
"description": "Turn boring text into rich HTML messages.",
"description": "Turn a plain message into a rich HTML message.",
"version": "1.0.1",
"author": "moose-team",
"bugs": {
"url": "https://github.com/moose-team/rich-message/issues"
},
"devDependencies": {
"standard": "^3.7.2",
"tap-dot": "^1.0.0",
"tape": "^4.0.0"
"standard": "^6.0.8",
"tap-spec": "^4.1.1",
"tape": "^4.5.1"
},
"homepage": "https://github.com/moose-team/rich-message",
"keywords": [
Expand All @@ -30,7 +30,7 @@
"url": "https://github.com/moose-team/rich-message.git"
},
"scripts": {
"test": "standard && tape test/*.js | tap-dot"
"test": "standard && tape test.js | tap-spec"
},
"dependencies": {
"ghlink": "^0.1.2",
Expand Down
35 changes: 31 additions & 4 deletions test/index.js → test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
var test = require('tape')
var richMessage = require('../')
var richMessage = require('./')
var mergeMessages = richMessage.mergeMessages
var CAT_URL = 'https://cdn.rawgit.com/moose-team/rich-message/master/cat.png'

test('usage example', function (t) {
var message = {
text: 'hi maxogden', // text entered by a user
username: 'mafintosh', // user's github name
timestamp: Date.now()
}
var username = 'maxogden' // current user's github name (used for highlighting)

var output = richMessage(message, username)
var expected = {
text: 'hi maxogden',
username: 'mafintosh',
anon: false,
avatar: 'https://github.com/mafintosh.png',
timeago: richMessage.timeago(message.timestamp),
html: '<div class="highlight"><p>hi maxogden</p><p></p></div>'
}

t.equal(output.html, expected.html, 'html is correctly formatted')
t.false(output.anon, 'user is not anon')
t.equal(output.timeago, expected.timeago, 'timeago works as expected')
t.end()
})

test('link replacement', function (t) {
var message = {
Expand All @@ -15,7 +40,7 @@ test('link replacement', function (t) {
'<a href="#cats">#cats</a> not#cat #cat!%$' +
'</p><p></p></div>'

t.equal(output.html, expected)
t.equal(output.html, expected, 'links are replaced in output html')
t.end()
})

Expand All @@ -39,7 +64,7 @@ test('anon avatars', function (t) {
}

var output = richMessage(message, 'other_cat')
t.equal(output.avatar, 'static/cat.png')
t.equal(output.avatar, CAT_URL)
t.end()
})

Expand Down Expand Up @@ -92,7 +117,9 @@ test('username highlight', function (t) {
t.end()
})

test('timeago', function (t) {
// bug: moment does not calculate time reliably in different environments
// on travis, 0 returns 1970, on mac os x, 1969
test.skip('timeago', function (t) {
var message = {
text: 'cat',
username: 'cat',
Expand Down

0 comments on commit c6295bb

Please sign in to comment.