Skip to content

Commit

Permalink
Display newlines in thread view
Browse files Browse the repository at this point in the history
Fixes #582
  • Loading branch information
veloce committed Apr 26, 2017
1 parent 3cc087c commit f793667
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/js/ui/inbox/thread/threadView.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as h from 'mithril/hyperscript'
import * as helper from '../../helper'
import { header as headerWidget, backButton, userStatus } from '../../shared/common'
import layout from '../../layout'
import i18n from '../../../i18n'
import { escapeHtml } from '../../../utils'
import redraw from '../../../utils/redraw'
import { ThreadState, Post, ThreadAttrs } from '../interfaces'

Expand Down Expand Up @@ -58,7 +60,9 @@ function renderPost(post: Post, index: number, posts: Array<Post>) {
&nbsp;–&nbsp;
{postDateFormat(post.createdAt)}
</div>
<div className="text">{post.text}</div>
<div className="text">
{h.trust(escapeHtml(post.text).replace(/\n/g, '<br>'))}
</div>
</div>
)
}
Expand Down
20 changes: 20 additions & 0 deletions src/js/utils/html.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Implementation originally from Twitter's Hogan.js:
// https://github.com/twitter/hogan.js/blob/master/lib/template.js#L325-L335
const rAmp = /&/g
const rLt = /</g
const rApos = /\'/g
const rQuot = /\"/g
const hChars = /[&<>\"\']/

export function escape(str: string) {
if (hChars.test(String(str))) {
return str
.replace(rAmp, '&amp;')
.replace(rLt, '&lt;')
.replace(rApos, '&apos;')
.replace(rQuot, '&quot;')
}
else {
return str
}
}
20 changes: 20 additions & 0 deletions src/js/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,23 @@ export function mapObject<K extends string, T, U>(obj: Record<K, T>, f: (x: T) =
export function lichessAssetSrc(path: string) {
return `${window.lichess.apiEndPoint}/assets/${path}`
}

// Implementation originally from Twitter's Hogan.js:
// https://github.com/twitter/hogan.js/blob/master/lib/template.js#L325-L335
const rAmp = /&/g
const rLt = /</g
const rApos = /\'/g
const rQuot = /\"/g
const hChars = /[&<>\"\']/
export function escapeHtml(str: string) {
if (hChars.test(String(str))) {
return str
.replace(rAmp, '&amp;')
.replace(rLt, '&lt;')
.replace(rApos, '&apos;')
.replace(rQuot, '&quot;')
}
else {
return str
}
}
2 changes: 1 addition & 1 deletion src/styl/inbox.styl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ table.threadList
>.postWrapper
width 90%
margin 0 auto
padding 5px 0px 5px 0px
padding 1em 0
border-top 1px solid #444
&.first
border-top none
Expand Down

0 comments on commit f793667

Please sign in to comment.