var path = require('path');
var pull = require("pull-stream");
var marked = require("ssb-marked");
var htime = require("human-time");
var emojis = require("emoji-named-characters");
var cat = require("pull-cat");
var emojiDir = path.join(require.resolve("emoji-named-characters"), "../pngs");
exports.wrapPage = wrapPage;
exports.MdRenderer = MdRenderer;
exports.renderEmoji = renderEmoji;
exports.formatMsgs = formatMsgs;
exports.renderThread = renderThread;
function MdRenderer(opts) {
marked.Renderer.call(this, {});
this.opts = opts;
}
MdRenderer.prototype = new marked.Renderer();
MdRenderer.prototype.urltransform = function(href) {
if (!href) return false;
switch (href[0]) {
case "#":
return this.opts.base + "channel/" + href.slice(1);
case "%":
return this.opts.msg_base + encodeURIComponent(href);
case "@":
return this.opts.feed_base + encodeURIComponent(href);
case "&":
return this.opts.blob_base + encodeURIComponent(href);
}
if (href.indexOf("javascript:") === 0) return false;
return href;
};
MdRenderer.prototype.image = function(href, title, text) {
return (
'" : ">")
);
};
function renderEmoji(emoji) {
var opts = this.renderer.opts;
return emoji in emojis
? ''
: ":" + emoji + ":";
}
function escape(str) {
return String(str)
.replace(/&/g, "&")
.replace(//g, ">")
.replace(/"/g, """);
}
function formatMsgs(id, ext, opts) {
switch (ext || "html") {
case "html":
return pull(renderThread(opts), wrapPage(id));
case "js":
return pull(renderThread(opts), wrapJSEmbed(opts));
case "json":
return wrapJSON();
default:
return null;
}
}
function wrap(before, after) {
return function(read) {
return cat([pull.once(before), read, pull.once(after)]);
};
}
function renderThread(opts) {
return pull(
pull.map(renderMsg.bind(this, opts)),
wrap(
'You are reading content from ' +
'Scuttlebutt' +
'' +
'',
'' +
'' +
'Join Scuttlebutt now' +
''
)
);
}
function wrapPage(id) {
return wrap(
"
" +
"" +
"" +
id + " | ssb-viewer" +
"" +
'' +
styles +
"",
""
);
}
var styles = `
`;
function wrapJSON() {
var first = true;
return pull(pull.map(JSON.stringify), join(","), wrap("[", "]"));
}
function wrapJSEmbed(opts) {
return pull(
wrap('', ""),
pull.map(docWrite),
opts.base_token && rewriteBase(new RegExp(opts.base_token, "g"))
);
}
function rewriteBase(token) {
// detect the origin of the script and rewrite the js/html to use it
return pull(
replace(token, '" + SSB_VIEWER_ORIGIN + "/'),
wrap(
"var SSB_VIEWER_ORIGIN = (function () {" +
'var scripts = document.getElementsByTagName("script")\n' +
"var script = scripts[scripts.length-1]\n" +
"if (!script) return location.origin\n" +
'return script.src.replace(/\\/%.*$/, "")\n' +
"}())\n",
""
)
);
}
function join(delim) {
var first = true;
return pull.map(function(val) {
if (!first) return delim + String(val);
first = false;
return val;
});
}
function replace(re, rep) {
return pull.map(function(val) {
return String(val).replace(re, rep);
});
}
function docWrite(str) {
return "document.write(" + JSON.stringify(str) + ")\n";
}
function renderMsg(opts, msg) {
var c = msg.value.content || {};
var name = encodeURIComponent(msg.key);
return (
'' +
'' +
render(opts, c) +
""
);
}
function msgTimestamp(msg, name) {
var date = new Date(msg.value.timestamp);
var isoStr = date.toISOString();
return (
''
);
}
function formatDate(date) {
// return date.toISOString().replace('T', ' ')
return htime(date);
}
function render(opts, c) {
var base = opts.base;
if (c.type === "post") {
var channel = c.channel
? '"
: "";
return channel + renderPost(opts, c);
} else if (c.type == "vote" && c.vote.expression == "Dig") {
var channel = c.channel
? ' in #' + c.channel + ""
: "";
var linkedText = "this";
if (typeof c.vote.linkedText != "undefined")
linkedText = c.vote.linkedText.substring(0, 75);
return ('' +
'Liked ' +
'' +
linkedText +
"" +
channel +
''
);
} else if (c.type == "vote") {
var linkedText = "this";
if (typeof c.vote.linkedText != "undefined")
linkedText = c.vote.linkedText.substring(0, 75);
return '' +
'Voted ' + linkedText + "" +
'';
} else if (c.type == "contact" && c.following) {
var name = c.contact;
if (typeof c.contactAbout != "undefined") name = c.contactAbout.name;
return '' +
'Followed ' + name + "" +
'';
} else if (c.type == "contact" && !c.following) {
var name = c.contact;
if (typeof c.contactAbout != "undefined") name = c.contactAbout.name;
return '' +
'Unfollowed ' + name + "" +
'';
} else if (typeof c == "string") {
return '' +
"Wrote something private" +
'';
}
else if (c.type == "about") {
return '' +
"Changed something in about" +
'' +
renderDefault(c);
}
else if (c.type == "issue") {
return '' +
"Created a git issue" +
'' +
renderDefault(c);
}
else if (c.type == "git-update") {
return '' +
"Did a git update" +
'' +
renderDefault(c);
}
else if (c.type == "ssb-dns") {
return '' +
"Updated DNS" +
'' +
renderDefault(c);
}
else if (c.type == "pub") {
return '' +
"Connected to a pub" +
'' +
renderDefault(c);
}
else if (c.type == "channel" && c.subscribed)
return '' +
'Subscribed to channel #' +
c.channel +
"" +
'';
else if (c.type == "channel" && !c.subscribed)
return '' +
'Unsubscribed from channel #' +
c.channel +
"" +
'';
else return renderDefault(c);
}
function renderPost(opts, c) {
return '' + marked(c.text, opts.marked) + "";
}
function renderDefault(c) {
return "" + JSON.stringify(c, 0, 2) + "
";
}