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");
|
|
|
|
const findChannels = require("./ircmessageparser/findChannels");
|
2017-04-18 03:28:35 +00:00
|
|
|
const findLinks = require("./ircmessageparser/findLinks");
|
2017-03-18 08:35:17 +00:00
|
|
|
const merge = require("./ircmessageparser/merge");
|
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 = [];
|
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
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
if (fragment.italic) {
|
2017-04-18 04:53:12 +00:00
|
|
|
classes.push("irc-italic");
|
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
|
|
|
}
|
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}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-04 04:36:03 +00:00
|
|
|
// Transform an IRC message potentially filled with styling control codes, URLs
|
|
|
|
// and channels into a string of HTML elements to display on the client.
|
2017-03-18 08:35:17 +00:00
|
|
|
module.exports = function parse(text) {
|
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);
|
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)
|
|
|
|
.sort((a, b) => a.start - b.start);
|
2016-05-12 13:07:15 +00:00
|
|
|
|
2017-04-04 04:36:03 +00:00
|
|
|
// Merge the styling information with the channels / URLs / text objects and
|
|
|
|
// 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-03-18 08:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fragments;
|
|
|
|
}).join("");
|
|
|
|
};
|