2017-03-18 09:21:18 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const Handlebars = require("handlebars/runtime");
|
2017-03-18 08:35:17 +00:00
|
|
|
const parseStyle = require("./ircmessageparser/parseStyle");
|
2017-11-21 22:54:20 +00:00
|
|
|
const anyIntersection = require("./ircmessageparser/anyIntersection");
|
2017-03-18 08:35:17 +00:00
|
|
|
const findChannels = require("./ircmessageparser/findChannels");
|
2017-04-18 03:28:35 +00:00
|
|
|
const findLinks = require("./ircmessageparser/findLinks");
|
2017-08-23 14:19:04 +00:00
|
|
|
const findEmoji = require("./ircmessageparser/findEmoji");
|
2017-11-14 22:36:45 +00:00
|
|
|
const findNames = require("./ircmessageparser/findNames");
|
2017-03-18 08:35:17 +00:00
|
|
|
const merge = require("./ircmessageparser/merge");
|
2017-11-14 22:36:45 +00:00
|
|
|
const colorClass = require("./colorClass");
|
2016-10-09 19:14:02 +00:00
|
|
|
|
2017-04-04 04:36:03 +00:00
|
|
|
// Create an HTML `span` with styling information for a given fragment
|
2017-03-18 08:35:17 +00:00
|
|
|
function createFragment(fragment) {
|
2017-04-08 12:34:31 +00:00
|
|
|
const classes = [];
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-03-18 08:35:17 +00:00
|
|
|
if (fragment.bold) {
|
2017-04-18 04:53:12 +00:00
|
|
|
classes.push("irc-bold");
|
2017-03-18 08:35:17 +00:00
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-03-18 08:35:17 +00:00
|
|
|
if (fragment.textColor !== undefined) {
|
2017-04-18 04:53:12 +00:00
|
|
|
classes.push("irc-fg" + fragment.textColor);
|
2017-03-18 08:35:17 +00:00
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-03-18 08:35:17 +00:00
|
|
|
if (fragment.bgColor !== undefined) {
|
2017-04-18 04:53:12 +00:00
|
|
|
classes.push("irc-bg" + fragment.bgColor);
|
2017-03-18 08:35:17 +00:00
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-03-18 08:35:17 +00:00
|
|
|
if (fragment.italic) {
|
2017-04-18 04:53:12 +00:00
|
|
|
classes.push("irc-italic");
|
2017-03-18 08:35:17 +00:00
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-03-18 08:35:17 +00:00
|
|
|
if (fragment.underline) {
|
2017-04-18 04:53:12 +00:00
|
|
|
classes.push("irc-underline");
|
2017-03-18 08:35:17 +00:00
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-12-03 02:52:49 +00:00
|
|
|
if (fragment.strikethrough) {
|
|
|
|
classes.push("irc-strikethrough");
|
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-12-03 15:00:35 +00:00
|
|
|
if (fragment.monospace) {
|
|
|
|
classes.push("irc-monospace");
|
|
|
|
}
|
2017-04-28 05:28:45 +00:00
|
|
|
|
|
|
|
let attributes = classes.length ? ` class="${classes.join(" ")}"` : "";
|
2017-03-18 08:35:17 +00:00
|
|
|
const escapedText = Handlebars.Utils.escapeExpression(fragment.text);
|
2017-04-28 05:28:45 +00:00
|
|
|
|
|
|
|
if (fragment.hexColor) {
|
|
|
|
attributes += ` style="color:#${fragment.hexColor}`;
|
|
|
|
|
|
|
|
if (fragment.hexBgColor) {
|
|
|
|
attributes += `;background-color:#${fragment.hexBgColor}`;
|
|
|
|
}
|
|
|
|
|
2017-11-26 22:34:28 +00:00
|
|
|
attributes += '"';
|
2017-03-18 08:35:17 +00:00
|
|
|
}
|
2017-04-28 05:28:45 +00:00
|
|
|
|
|
|
|
if (attributes.length) {
|
|
|
|
return `<span${attributes}>${escapedText}</span>`;
|
|
|
|
}
|
|
|
|
|
2017-03-18 08:35:17 +00:00
|
|
|
return escapedText;
|
2014-12-11 03:35:17 +00:00
|
|
|
}
|
2014-12-10 11:30:45 +00:00
|
|
|
|
2017-11-14 22:36:45 +00:00
|
|
|
// Transform an IRC message potentially filled with styling control codes, URLs,
|
|
|
|
// nicknames, and channels into a string of HTML elements to display on the client.
|
|
|
|
module.exports = function parse(text, users) {
|
|
|
|
// if it's not the users we're expecting, but rather is passed from Handlebars (occurs when users passed to template is null or undefined)
|
|
|
|
if (users && users.hash) {
|
|
|
|
users = [];
|
|
|
|
}
|
|
|
|
|
2017-04-04 04:36:03 +00:00
|
|
|
// Extract the styling information and get the plain text version from it
|
2017-03-18 08:35:17 +00:00
|
|
|
const styleFragments = parseStyle(text);
|
2017-04-08 12:34:31 +00:00
|
|
|
const cleanText = styleFragments.map((fragment) => fragment.text).join("");
|
2014-12-10 11:30:45 +00:00
|
|
|
|
2017-04-04 04:36:03 +00:00
|
|
|
// On the plain text, find channels and URLs, returned as "parts". Parts are
|
|
|
|
// arrays of objects containing start and end markers, as well as metadata
|
|
|
|
// depending on what was found (channel or link).
|
|
|
|
const channelPrefixes = ["#", "&"]; // TODO Channel prefixes should be RPL_ISUPPORT.CHANTYPES
|
|
|
|
const userModes = ["!", "@", "%", "+"]; // TODO User modes should be RPL_ISUPPORT.PREFIX
|
2017-03-18 08:35:17 +00:00
|
|
|
const channelParts = findChannels(cleanText, channelPrefixes, userModes);
|
|
|
|
const linkParts = findLinks(cleanText);
|
2017-08-23 14:19:04 +00:00
|
|
|
const emojiParts = findEmoji(cleanText);
|
2017-11-14 22:36:45 +00:00
|
|
|
const nameParts = findNames(cleanText, (users || []));
|
2014-12-10 11:30:45 +00:00
|
|
|
|
2017-04-04 04:36:03 +00:00
|
|
|
// Sort all parts identified based on their position in the original text
|
2017-03-18 08:35:17 +00:00
|
|
|
const parts = channelParts
|
|
|
|
.concat(linkParts)
|
2017-08-23 14:19:04 +00:00
|
|
|
.concat(emojiParts)
|
2017-11-14 22:36:45 +00:00
|
|
|
.concat(nameParts)
|
2017-11-21 22:54:20 +00:00
|
|
|
.sort((a, b) => a.start - b.start || b.end - a.end)
|
|
|
|
.reduce((prev, curr) => {
|
|
|
|
const intersection = prev.some((p) => anyIntersection(p, curr));
|
|
|
|
|
|
|
|
if (intersection) {
|
|
|
|
return prev;
|
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-11-21 22:54:20 +00:00
|
|
|
return prev.concat([curr]);
|
|
|
|
}, []);
|
2016-05-12 13:07:15 +00:00
|
|
|
|
2017-11-14 22:36:45 +00:00
|
|
|
// Merge the styling information with the channels / URLs / nicks / text objects and
|
2017-04-04 04:36:03 +00:00
|
|
|
// generate HTML strings with the resulting fragments
|
2017-04-08 12:34:31 +00:00
|
|
|
return merge(parts, styleFragments).map((textPart) => {
|
2017-04-04 04:36:03 +00:00
|
|
|
// Create HTML strings with styling information
|
2017-03-18 08:35:17 +00:00
|
|
|
const fragments = textPart.fragments.map(createFragment).join("");
|
2014-12-10 11:30:45 +00:00
|
|
|
|
2017-04-04 04:36:03 +00:00
|
|
|
// Wrap these potentially styled fragments with links and channel buttons
|
2017-03-18 08:35:17 +00:00
|
|
|
if (textPart.link) {
|
|
|
|
const escapedLink = Handlebars.Utils.escapeExpression(textPart.link);
|
2017-04-18 04:53:12 +00:00
|
|
|
return `<a href="${escapedLink}" target="_blank" rel="noopener">${fragments}</a>`;
|
2017-03-18 08:35:17 +00:00
|
|
|
} else if (textPart.channel) {
|
|
|
|
const escapedChannel = Handlebars.Utils.escapeExpression(textPart.channel);
|
2017-04-18 04:53:12 +00:00
|
|
|
return `<span class="inline-channel" role="button" tabindex="0" data-chan="${escapedChannel}">${fragments}</span>`;
|
2017-08-23 14:19:04 +00:00
|
|
|
} else if (textPart.emoji) {
|
|
|
|
return `<span class="emoji">${fragments}</span>`;
|
2017-11-14 22:36:45 +00:00
|
|
|
} else if (textPart.nick) {
|
|
|
|
const nick = Handlebars.Utils.escapeExpression(textPart.nick);
|
|
|
|
return `<span role="button" class="user ${colorClass(nick)}" data-name="${nick}">${fragments}</span>`;
|
2017-03-18 08:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fragments;
|
|
|
|
}).join("");
|
|
|
|
};
|