git ssb

30+

cel / git-ssb-web



Commit 20a18df438a0929c10fcd025ca475c2ed6f3b0e9

Render emoji in markdown

cel committed on 11/20/2016, 6:56:39 PM
Parent: 7a409b06ac2da63a7cb40bf010ed89e2954f32f3

Files changed

index.jschanged
lib/markdown.jschanged
package.jsonchanged
static/styles.csschanged
index.jsView
@@ -25,8 +25,9 @@
2525 var mime = require('mime-types')
2626 var moment = require('moment')
2727
2828 var hlCssPath = path.resolve(require.resolve('highlight.js'), '../../styles')
29 +var emojiPath = path.resolve(require.resolve('emoji-named-characters'), '../pngs')
2930
3031 function ParamError(msg) {
3132 var err = Error.call(this, msg)
3233 err.name = ParamError.name
@@ -236,8 +237,10 @@
236237 else if (dir == 'static')
237238 return this.serveFile(req, dirs)
238239 else if (dir == 'highlight')
239240 return this.serveFile(req, [hlCssPath].concat(dirs.slice(1)), true)
241 + else if (dir == 'emoji')
242 + return this.serveFile(req, [emojiPath].concat(dirs.slice(1)), true)
240243 else
241244 return this.serve404(req)
242245 }
243246
lib/markdown.jsView
@@ -2,8 +2,9 @@
22 var url = require('url')
33 var marked = require('ssb-marked')
44 var ref = require('ssb-ref')
55 var u = require('./util')
6 +var emojiNamedCharacters = require('emoji-named-characters')
67
78 // render links to git objects and ssb objects
89 var blockRenderer = new marked.Renderer()
910 blockRenderer.urltransform = function (href) {
@@ -47,8 +48,17 @@
4748 (checked === undefined ? '' : '<i>' + (checked ? '☑' : '☐') + '</i> ') +
4849 text + '</li>\n'
4950 }
5051
52 +function renderEmoji(emoji) {
53 + return emoji in emojiNamedCharacters ?
54 + '<img src="/emoji/' + encodeURI(emoji) + '.png"'
55 + + ' alt=":' + escape(emoji) + ':"'
56 + + ' title=":' + escape(emoji) + ':"'
57 + + ' class="emoji" height="16" width="16">'
58 + : ':' + emoji + ':'
59 +}
60 +
5161 marked.setOptions({
5262 gfm: true,
5363 mentions: true,
5464 tables: true,
@@ -56,17 +66,18 @@
5666 pedantic: false,
5767 sanitize: true,
5868 smartLists: true,
5969 smartypants: false,
70 + emoji: renderEmoji,
6071 highlight: u.highlight,
6172 renderer: blockRenderer
6273 })
6374
6475 // hack to make git link mentions work
6576 var mdRules = new marked.InlineLexer(1, marked.defaults).rules
6677 mdRules.mention =
6778 /^(\s)?([@%&][A-Za-z0-9\._\-+=\/]*[A-Za-z0-9_\-+=\/]|[0-9a-f]{40})/
68-mdRules.text = /^[\s\S]+?(?=[\\<!\[_*`]| {2,}\n| [@%&]|[0-9a-f]{40}|$)/
79 +mdRules.text = /^[\s\S]+?(?=[\\<!\[_*`:~]|https?:\/\/| {2,}\n| [@%&]|[0-9a-f]{40}|$)/
6980
7081 module.exports = function (text, options, cb) {
7182 if (!text) return ''
7283 if (typeof text != 'string') text = String(text)
package.jsonView
@@ -5,8 +5,9 @@
55 "bin": "server.js",
66 "dependencies": {
77 "asyncmemo": "^0.1.0",
88 "diff": "^2.2.3",
9 + "emoji-named-characters": "^1.0.2",
910 "highlight.js": "^9.2.0",
1011 "mime-types": "^2.1.12",
1112 "moment": "^2.14.1",
1213 "multicb": "^1.2.1",
static/styles.cssView
@@ -144,8 +144,14 @@
144144 section img {
145145 max-width: 100%;
146146 }
147147
148 +.emoji {
149 + width: 1em;
150 + height: 1em;
151 + vertical-align: middle;
152 +}
153 +
148154 /* repo page */
149155
150156 .repo-title {
151157 height: 2em;

Built with git-ssb-web