oasis/src/models/markdown.js

43 lines
1.0 KiB
JavaScript
Raw Normal View History

2020-01-22 00:22:19 +00:00
"use strict";
2019-08-15 01:10:22 +00:00
2020-01-22 00:22:19 +00:00
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 = []) => {
2020-01-22 00:22:19 +00:00
const mentionNames = {};
2019-06-25 00:07:18 +00:00
2020-01-22 00:22:19 +00:00
ssbMessages.links(mentions, "feed").forEach(link => {
if (link.name && typeof link.name === "string") {
const name = link.name.charAt(0) === "@" ? link.name : `@${link.name}`;
mentionNames[name] = link.link;
2019-06-25 00:07:18 +00:00
}
2020-01-22 00:22:19 +00:00
});
2019-06-25 00:07:18 +00:00
2020-01-22 00:22:19 +00:00
return ref => {
2019-06-25 00:07:18 +00:00
// @mentions
if (ref in mentionNames) {
2020-01-22 00:22:19 +00:00
return `/author/${encodeURIComponent(mentionNames[ref])}`;
2019-06-25 00:07:18 +00:00
}
if (ssbRef.isFeedId(ref)) {
2020-01-22 00:22:19 +00:00
return `/author/${encodeURIComponent(ref)}`;
2019-06-25 00:07:18 +00:00
}
2020-01-22 00:22:19 +00:00
if (ssbRef.isMsgId(ref)) {
return `/thread/${encodeURIComponent(ref)}`;
}
if (ssbRef.isBlobId(ref)) {
return `/blob/${encodeURIComponent(ref)}`;
}
if (ref && ref[0] === "#") {
return `/hashtag/${encodeURIComponent(ref.substr(1))}`;
}
return "";
};
};
2020-01-22 00:22:19 +00:00
module.exports = (input, mentions) =>
md.block(input, {
toUrl: toUrl(mentions)
});