on master help sign in
Added date formatting
commit20c9a812947856d373f9e40db4f78413d770a044 authorDevine Lu Linvega <aliceffekt@gmail.com> dateTue 2 Jul 2019, 09:48 (7 years ago) parent111d52e
2 files changed+34−19
links/main.css+4−4 scripts/hallway.js+30−15
links/main.css+4−4
@@ -11,13 +11,13 @@ body div p.readme { max-width:500px; }
1111
1212/* Hallway */
1313
14−body #hallway .entry { line-height: 16px; display: block; position: relative; padding-left:200px; }
15−body #hallway .entry .date { color: #555;display: block;position: absolute;left: 0px; }
16−body #hallway .entry .date:after { content: ")" }
17−body #hallway .entry .author { font-family: 'input_mono_medium'; position: absolute;left: 60px; }
14+body #hallway .entry { line-height: 16px; display: block; position: relative; padding-left:140px; }
15+body #hallway .entry .date { color: #555;display: block;float:right; margin-left:20px;}
16+body #hallway .entry .author { font-family: 'input_mono_medium'; position: absolute;left: 0px; }
1817body #hallway .entry .author:after { content:":"; }
1918body #hallway .entry .body { max-width: 400px }
2019body #hallway .entry .body a { text-decoration: underline; }
20+body #hallway p { padding-left: 60px;background-image: url(../icon.white.svg);background-size: 40px;background-repeat: no-repeat;background-position: 0px 0px; }
2121
2222/* Portal */
2323
scripts/hallway.js+30−15
@@ -42,11 +42,6 @@ function Hallway (sites) {
4242 }
4343
4444 this.templateEntry = function (entry) {
45− const now = new Date()
46− const then = Date.parse(entry.date)
47− const diff = (now - then)
48− const date = Math.floor(diff / 86400000)
49−
5045 // Find mention
5146 if (entry.body.indexOf('@<') > -1) {
5247 const data = entry.body.split('@<').pop().split('>')[0]
@@ -56,7 +51,7 @@ function Hallway (sites) {
5651 entry.body = entry.body.replace(`@<${data}>`, `<a href='${path}'>${name}</a>`)
5752 }
5853
59− return `<li class='entry'><span class='date'>${ago(date)}</span> <span class='author'>${entry.author}</span> <span class='body'>${entry.body}</span></li>`
54+ return `<li class='entry'><span class='date'>${timeAgo(Date.parse(entry.date))}</span> <span class='author'>${entry.author}</span> <span class='body'>${entry.body}</span></li>`
6055 }
6156
6257 // Feeds
@@ -81,7 +76,7 @@ function Hallway (sites) {
8176
8277 this.fetchFeed = function (id, feed) {
8378 console.log(`Fetching ${id}(${feed.path})..`)
84− Promise.all([ fetch(feed.path, { cache: 'no-store' }).then(x => x.text()) ]).then(([content]) => {
79+ Promise.all([ fetch(feed.path + '?v=1', { cache: 'no-store' }).then(x => x.text()) ]).then(([content]) => {
8580 feeds[id].content = parseFeed(id, content)
8681 this.refresh()
8782 })
@@ -107,13 +102,33 @@ function Hallway (sites) {
107102 return entries
108103 }
109104
110− function ago (days, cap = 9999) {
111− if (-days > cap) { return `${this.toString(true)}` }
112− if (days === -1) { return `yesterday` }
113− if (days === 1) { return 'tomorrow' }
114− if (days === 0) { return 'today' }
115− if (days < -365) { return `${Math.floor(days / -365)} years ago` }
116− if (days < 1) { return `${days * -1} days ago` }
117− return `in ${days} days`
105+ function timeAgo (dateParam) {
106+ if (!dateParam) {
107+ return null
108+ }
109+
110+ const date = typeof dateParam === 'object' ? dateParam : new Date(dateParam)
111+ const DAY_IN_MS = 86400000 // 24 * 60 * 60 * 1000
112+ const today = new Date()
113+ const yesterday = new Date(today - DAY_IN_MS)
114+ const seconds = Math.round((today - date) / 1000)
115+ const minutes = Math.round(seconds / 60)
116+ const isToday = today.toDateString() === date.toDateString()
117+ const isYesterday = yesterday.toDateString() === date.toDateString()
118+
119+ if (seconds < 5) {
120+ return 'now'
121+ } else if (seconds < 60) {
122+ return `${seconds} seconds ago`
123+ } else if (seconds < 90) {
124+ return 'about a minute ago'
125+ } else if (minutes < 60) {
126+ return `${minutes} minutes ago`
127+ } else if (isToday) {
128+ return `${Math.floor(minutes / 60)} hours ago`
129+ } else if (isYesterday) {
130+ return 'yesterday'
131+ }
132+ return `${Math.floor(minutes / 1440)} days ago`
118133 }
119134}