HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Custom Parser for Rich Text?
I am trying to figure out how to implement my own custom parser for the Richtext editor in 3.3, basically, I would like some old BBCode emojis to parse and display as their gifs. I would also like to implement some sort of short code for James Bond film titles, i.e. [GE] would change to Goldeneye, [NTTD] would become No Time To Die.
I don't mind if this happens before being saved (maybe better for search having the full film name anyway) or at render time but I am struggling to see how I can achieve it.
The nearest event seems to be "BeforeCommentBody" however this is deprecated.
If I upgrade to the latest release does this make the job any easier?
Thanks
Simon
Tagged:
0
Comments
You might get an idea of how to from the link in this post
https://open.vanillaforums.com/discussion/comment/259466/#Comment_259466
The main decision would be if you would like to do it in the frontend or in the backend: JavaScript or PHP.
The way you describe it, the JavaScript approach would be more intuitive for the users. You should search for ways to extend the Quill editor, which is the script the Rich Editor is based upon.
If you take the php approach, you need to handle BeforeSaveDiscussion, BeforeSaveComment and BeforeSave of the ConversationMessageModel.
To implement your custom parser, you can create a new class that implements the
ICommentParser
interface. This interface has aParse
method that takes the comment body as input and returns the parsed content. In yourParse
method, you can use regular expressions to find and replace the BBCode emojis and short codes with their corresponding gifs or film titles.Here is an example implementation of the
ICommentParser
interface:public class CustomCommentParser : ICommentParser
{
public string Parse(string commentBody)
{
// Replace BBCode emojis with gifs
commentBody = Regex.Replace(commentBody, @"\[emoji:([^\]]+)\]", "<img src=\"$1.gif\" alt=\"$1\">");
// Replace short codes with film titles
commentBody = Regex.Replace(commentBody, @"\[GE\]", "Goldeneye");
commentBody = Regex.Replace(commentBody, @"\[NTTD\]", "No Time To Die");
return commentBody;
}
}
To use your custom parser, you can register it in the
Global.asax.cs
file in theApplication_Start
method:protected void Application_Start()
{
// Register the custom comment parser
CommentParserManager.RegisterParser(new CustomCommentParser());
}