From 8f56bea4cc9f145fbb559586729dc2942d773a1c Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Thu, 10 Aug 2017 01:00:14 +0000 Subject: [PATCH] Resolves #37, basic Discord markdown parsing Adds support for bold, italics, underline, strikethrough, code, and blockcode (no Syntax highlighting yet) --- webapp/titanembeds/static/css/embedstyle.css | 19 +++++++++++++++++-- webapp/titanembeds/static/js/embed.js | 12 ++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/webapp/titanembeds/static/css/embedstyle.css b/webapp/titanembeds/static/css/embedstyle.css index c77fe6b..6348717 100644 --- a/webapp/titanembeds/static/css/embedstyle.css +++ b/webapp/titanembeds/static/css/embedstyle.css @@ -169,6 +169,7 @@ body > div.navbar-fixed > nav > div { #chatcontent > p { display: table; + width: 90%; } #chatcontent > p > span { @@ -391,8 +392,7 @@ a { display: table-header-group; } .chatmessage { - display: table-footer-group; - display: inline-block; + display: inline; color: rgb(195, 196, 197); } @@ -403,4 +403,19 @@ p.mentioned { p.mentioned span.chatmessage { color: #ff5252; +} + +.chatmessage code { + background-color: gray; + color: lightgray; + border-radius: 5px; + padding: 2px; +} + +.chatmessage code.blockcode { + width: 100%; + display: inline-block; + white-space: pre-wrap; + line-height: 15px; + padding: 5px; } \ No newline at end of file diff --git a/webapp/titanembeds/static/js/embed.js b/webapp/titanembeds/static/js/embed.js index 53d50a7..23c3c0b 100644 --- a/webapp/titanembeds/static/js/embed.js +++ b/webapp/titanembeds/static/js/embed.js @@ -603,6 +603,16 @@ } return message; } + + function parse_message_markdown(text) { + text = text.replace(/\*\*(.*?)\*\*/g, "$1"); + text = text.replace(/\*(.*?)\*/g, "$1"); + text = text.replace(/__(.*?)__/g, "$1"); + text = text.replace(/~~(.*?)~~/g, "$1"); + text = text.replace(/\`\`\`([^]+)\`\`\`/g, "$1"); + text = text.replace(/\`(.*?)\`/g, "$1"); + return text; + } function fill_discord_messages(messages, jumpscroll) { if (messages.length == 0) { @@ -619,11 +629,13 @@ message = parse_message_attachments(message); message = parse_channels_in_message(message); message.content = escapeHtml(message.content); + message.content = parse_message_markdown(message.content); message = parse_emoji_in_message(message); var rendered = Mustache.render(template, {"id": message.id, "full_timestamp": message.formatted_timestamp, "time": message.formatted_time, "username": message.author.username, "discriminator": message.author.discriminator, "content": nl2br(message.content)}); $("#chatcontent").append(rendered); last = message.id; handle_last_message_mention(); + $("#chatcontent p:last-child").find(".blockcode").find("br").remove(); // Remove excessive breaks in codeblocks } $("html, body").animate({ scrollTop: $(document).height() }, "slow"); $('#chatcontent').linkify({