on master help sign in
Merge pull request #190 from ckipp01/master

Twtxt user regex attempt two

commita73feed356a647c223571ef9f83864740089a7bc authorChris <ckipp@pm.me> dateMon 22 Jul 2019, 04:13 (7 years ago) parent8cace4c55ef5bc
1 file changed+16−9
scripts/hallway.js+16−9
scripts/hallway.js+16−9
@@ -1,7 +1,7 @@
11'use strict'
22
33const re_channel = /(\s|^)\/([a-zA-Z0-9]+)/g
4−const re_user = /(\s|^)\@([a-zA-Z0-9]+)/g
4+const re_user = /((\s|^)@&lt;[a-zA-Z0-9./:\-_\+~#= ]*&gt;)/g
55const re_tag = /([^&]|^)\#([a-zA-Z0-9]+)/g
66const re_url = /((https?):\/\/(([-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b)([-a-zA-Z0-9@:%_\+.~#?&//=]*)))/g
77
@@ -31,18 +31,18 @@ function Hallway (sites) {
3131
3232 this.el.innerHTML = `
3333 <ul id='entries'>
34− ${entries.filter((val, id) => { return !this.filter || (val.author === this.filter || val.channel === this.filter || val.tags.includes(this.filter)) }).filter((val, id) => { return id < 50 }).reduce((acc, val, id) => { return acc + this.templateEntry(val) + '\n' }, '')}
34+ ${entries.filter((val) => { return !this.filter || (val.author === this.filter || val.channel === this.filter || val.tags.includes(this.filter)) }).filter((_, id) => { return id < 50 }).reduce((acc, val) => { return acc + this.templateEntry(val) + '\n' }, '')}
3535 </ul>
3636 <div id='sidebar'>
3737 <ul id='channels'>
3838 <li onclick='filter("")' class='${hallway.filter === '' ? 'selected' : ''}'><a href='#'>hallway <span class='right'>${entries.length}</span></a></li>
39− ${Object.keys(channels).reduce((acc, val, id) => { return acc + `<li onclick='filter("${val}")' class='${hallway.filter === val ? 'selected' : ''}'><a href='#${val}'>${val} <span class='right'>${channels[val]}</span></a></li>\n` }, '')}
39+ ${Object.keys(channels).reduce((acc, val) => { return acc + `<li onclick='filter("${val}")' class='${hallway.filter === val ? 'selected' : ''}'><a href='#${val}'>${val} <span class='right'>${channels[val]}</span></a></li>\n` }, '')}
4040 </ul>
4141 <ul id='users'>
42− ${Object.keys(users).reduce((acc, val, id) => { return acc + `<li onclick='filter("${val}")' class='${hallway.filter === val ? 'selected' : ''}' href='#${val}'>${val} <span class='right'>${users[val]}</span></li>\n` }, '')}
42+ ${Object.keys(users).reduce((acc, val) => { return acc + `<li onclick='filter("${val}")' class='${hallway.filter === val ? 'selected' : ''}' href='#${val}'>${val} <span class='right'>${users[val]}</span></li>\n` }, '')}
4343 </ul>
4444 <ul id='tags'>
45− ${Object.keys(tags).reduce((acc, val, id) => { return acc + `<li onclick='filter("${val}")' class='${hallway.filter === val ? 'selected' : ''}' href='#${val}'>#${val} <span class='right'>${tags[val]}</span></li>\n` }, '')}
45+ ${Object.keys(tags).reduce((acc, val) => { return acc + `<li onclick='filter("${val}")' class='${hallway.filter === val ? 'selected' : ''}' href='#${val}'>#${val} <span class='right'>${tags[val]}</span></li>\n` }, '')}
4646 </ul>
4747 </div>
4848 <p>The <b>Hallway</b> is a decentralized forum, to join the conversation, simply create yourself a <a href="https://twtxt.readthedocs.io/en/stable/user/twtxtfile.html">twtxt</a> feed and <a href="https://github.com/XXIIVV/Webring/">add it</a> to your entry in the <a href="index.html">webring</a>.</p>`
@@ -72,7 +72,7 @@ function Hallway (sites) {
7272 }
7373 return users
7474 }
75−
75+
7676 this.findTags = function (entries){
7777 const tags = {}
7878 for (const id in entries) {
@@ -94,20 +94,27 @@ function Hallway (sites) {
9494 return a.sort( (a, b) => a.offset - b.offset )
9595 }
9696
97+ this.findMention = function (found) {
98+ const mention = Object.keys(feeds).filter(user => found.indexOf(feeds[user].path) > -1)
99+ return mention.length === 1
100+ ? ` <span class='user'>@${mention[0]}</span>`
101+ : ` <span class='user'>${found}</span>`
102+ }
103+
97104 this.templateEntry = function (entry) {
98105 entry.html = entry.body
99106 .replace(re_channel, `$1<span class='channel'>/$2</span>`)
100− .replace(re_user, `$1<span class='user'>@$2</span>`)
107+ .replace(re_user, this.findMention)
101108 .replace(re_tag, `$1<span class='tag'>#$2</span>`)
102109 .replace(re_url, `<a target="_blank" href='$1'>$1</a>`)
103−
110+
104111 const filter = window.location.hash.substr(1).replace(/\+/g, ' ').toLowerCase()
105112 const highlight = filter === entry.author.toLowerCase()
106113 const origin = feeds[entry.author].path
107114
108115 return `<li class='entry ${highlight ? 'highlight' : ''}'><span class='date'>${timeAgo(Date.parse(entry.date))}</span> <a class='author' href='${origin}' target='_blank'>${entry.author}</a> <span class='body'>${entry.html}</span></li>`
109116 }
110−
117+
111118 // Feeds
112119
113120 this.findFeeds = function () {