using System.Text.RegularExpressions; using Nop.Core.Domain.Common; using Nop.Services.Html.CodeFormatter; namespace Nop.Services.Html; /// /// Represents a BBCode helper /// public partial class BBCodeHelper : IBBCodeHelper { #region Fields protected readonly CommonSettings _commonSettings; protected static readonly Regex _regexBold = new(@"\[b\](.+?)\[/b\]", RegexOptions.Compiled | RegexOptions.IgnoreCase); protected static readonly Regex _regexItalic = new(@"\[i\](.+?)\[/i\]", RegexOptions.Compiled | RegexOptions.IgnoreCase); protected static readonly Regex _regexUnderLine = new(@"\[u\](.+?)\[/u\]", RegexOptions.Compiled | RegexOptions.IgnoreCase); protected static readonly Regex _regexUrl1 = new(@"\[url\=(https?:.+?)\]([^\]]+)\[/url\]", RegexOptions.Compiled | RegexOptions.IgnoreCase); protected static readonly Regex _regexUrl2 = new(@"\[url\](https?:.+?)\[/url\]", RegexOptions.Compiled | RegexOptions.IgnoreCase); protected static readonly Regex _regexQuote = new(@"\[quote=(.+?)\](.+?)\[/quote\]", RegexOptions.Compiled | RegexOptions.IgnoreCase); protected static readonly Regex _regexImg = new(@"\[img\](.+?)\[/img\]", RegexOptions.Compiled | RegexOptions.IgnoreCase); #endregion #region Ctor public BBCodeHelper(CommonSettings commonSettings) { _commonSettings = commonSettings; } #endregion #region Methods /// /// Formats the text /// /// Text /// A value indicating whether to replace Bold /// A value indicating whether to replace Italic /// A value indicating whether to replace Underline /// A value indicating whether to replace URL /// A value indicating whether to replace Code /// A value indicating whether to replace Quote /// A value indicating whether to replace Img /// Formatted text public virtual string FormatText(string text, bool replaceBold, bool replaceItalic, bool replaceUnderline, bool replaceUrl, bool replaceCode, bool replaceQuote, bool replaceImg) { if (string.IsNullOrEmpty(text)) return string.Empty; if (replaceBold) // format the bold tags: [b][/b] becomes: text = _regexBold.Replace(text, "$1"); if (replaceItalic) // format the italic tags: [i][/i] becomes: text = _regexItalic.Replace(text, "$1"); if (replaceUnderline) // format the underline tags: [u][/u] becomes: text = _regexUnderLine.Replace(text, "$1"); if (replaceUrl) { var newWindow = _commonSettings.BbcodeEditorOpenLinksInNewWindow; // format the URL tags: [url=https://www.nopCommerce.com]my site[/url] // becomes: my site text = _regexUrl1.Replace(text, $"$2"); // format the URL tags: [url]https://www.nopCommerce.com[/url] // becomes: https://www.nopCommerce.com text = _regexUrl2.Replace(text, $"$1"); } if (replaceQuote) while (_regexQuote.IsMatch(text)) text = _regexQuote.Replace(text, "$1 wrote:
$2
"); if (replaceCode) text = CodeFormatHelper.FormatTextSimple(text); if (replaceImg) // format the img tags: [img]https://www.nopCommerce.com/Content/Images/Image.jpg[/img] // becomes: text = _regexImg.Replace(text, "\"\""); return text; } /// /// Removes all quotes from string /// /// Source string /// string public virtual string RemoveQuotes(string str) { str = Regex.Replace(str, @"\[quote=(.+?)\]", string.Empty, RegexOptions.Compiled | RegexOptions.IgnoreCase); str = Regex.Replace(str, @"\[/quote\]", string.Empty, RegexOptions.Compiled | RegexOptions.IgnoreCase); return str; } #endregion }