oasis/src/pages/models/lib/markdown.js

39 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-07-28 20:49:01 +00:00
'use strict'
const md = require('ssb-markdown')
const ssbMessages = require('ssb-msgs')
const ssbRef = require('ssb-ref')
2019-06-25 00:07:18 +00:00
const toUrl = (mentions = []) => {
2019-06-25 00:07:18 +00:00
var mentionNames = {}
2019-06-27 21:54:32 +00:00
ssbMessages.links(mentions, 'feed').forEach(function (link) {
2019-06-25 00:07:18 +00:00
if (link.name && typeof link.name === 'string') {
var name = (link.name.charAt(0) === '@') ? link.name : '@' + link.name
mentionNames[name] = link.link
}
})
return (ref, isImage) => {
// @mentions
if (ref in mentionNames) {
return '/author/' + encodeURIComponent(mentionNames[ref])
}
if (ssbRef.isFeedId(ref)) {
return '/author/' + encodeURIComponent(ref)
} else if (ssbRef.isMsgId(ref)) {
return '/thread/' + encodeURIComponent(ref)
2019-06-25 00:07:18 +00:00
} else if (ssbRef.isBlobId(ref)) {
return 'http://localhost:8989/blobs/get/' + encodeURIComponent(ref)
} else if (ref && ref[0] === '#') {
return '/hashtag/' + encodeURIComponent(ref.substr(1))
}
return ''
}
}
module.exports = (input, mentions) =>
md.block(input, {
toUrl: toUrl(mentions)
})