on master help sign in
Autolink URLs in wiki
8cdc274Liam Cooke
scripts/wiki.js+7−2
@@ -55,11 +55,16 @@ const Wiki = sites => {
55 return `${currentHtml}${newHtml}`
55 return `${currentHtml}${newHtml}`
56 }
56 }
57
57
58+ const formatLinks = text => text.replace(
59+ new RegExp('\\b(https?://[^."\\s]+\\.[^"\\s]+)', 'g'),
60+ '<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>'
61+ )
62+
58 const formatEntry = (definitions, definition) => {
63 const formatEntry = (definitions, definition) => {
59− const formatEntryList = entryList => entryList.reduce((items, item) => `${items}<li>${item}</li>`, '')
64+ const formatEntryList = entryList => entryList.reduce((items, item) => `${items}<li>${formatLinks(item)}</li>`, '')
60
65
61 const newHtml = typeof definition.entry === 'string'
66 const newHtml = typeof definition.entry === 'string'
62− ? `<li>${definition.entry}
67+ ? `<li>${formatLinks(definition.entry)}
63 - <a class='author' target='_blank' href='${definition.url}'> @${definition.author}</a>
68 - <a class='author' target='_blank' href='${definition.url}'> @${definition.author}</a>
64 </li>`
69 </li>`
65 : `<li><ul>${formatEntryList(definition.entry)}
70 : `<li><ul>${formatEntryList(definition.entry)}