diff options
Diffstat (limited to 'xP/public/xP.js')
-rw-r--r-- | xP/public/xP.js | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/xP/public/xP.js b/xP/public/xP.js index 13507d6..c0dba93 100644 --- a/xP/public/xP.js +++ b/xP/public/xP.js @@ -115,6 +115,24 @@ let BufferList = { }, } +function linkify(text, attrs, a) { + let re = new RegExp([ + /https?:\/\//, + /([^\[\](){}<>"'\s]|\([^\[\](){}<>"'\s]*\))+/, + /[^\[\](){}<>"'\s,.:]/, + ].map(r => r.source).join(''), 'g') + + let end = 0, match + while ((match = re.exec(text)) !== null) { + if (end < match.index) + a.push(m('span', attrs, text.substring(end, match.index))) + a.push(m('a', {href: match[0], ...attrs}, match[0])) + end = re.lastIndex + } + if (end < text.length) + a.push(m('span', attrs, text.substring(end))) +} + let Content = { view: vnode => { let line = vnode.children[0] @@ -139,11 +157,10 @@ let Content = { line.items.forEach(item => { switch (item.kind) { case 'Text': - // TODO: Detect and transform links. - content.push(m('span', { + linkify(item.text, { class: Array.from(classes.keys()).join(' '), style: applyColor(fg, bg, inverse), - }, item.text)) + }, content) break case 'Reset': classes.clear() |