{paragraph.Trim()}"); + } + else if (IsURL(paragraph)) + { + htmlBuilder.AppendLine($"{paragraph.Trim()}"); + } + else if (HasURLText(paragraph)) + { + ConvertMarkdownToHtml(paragraph, htmlBuilder); + } + else if (ContainsBoldPattern(paragraph)) + { + htmlBuilder.AppendLine($"
{ConvertToHtmlBold(paragraph)}
"); + } + else if (paragraph.Contains("```")) + { + htmlBuilder.AppendLine("");
+ htmlBuilder.AppendLine("");
+ htmlBuilder.AppendLine("");
+ htmlBuilder.AppendLine(" |
{paragraph.Trim()}
"); + } + } + + htmlBuilder.AppendLine(""); + htmlBuilder.AppendLine(""); + return htmlBuilder.ToString(); + } + public string TrimHashFromTitle(string title, out int headingLevel) + { + headingLevel = 0; + + // Loop from 4 to 1 to check for "####", "###", "##", and "#" + for (int i = 4; i > 0; i--) + { + string hashes = new string('#', i); + int index = title.IndexOf(hashes); + if (index != -1) + { + headingLevel = i; + // Trim the found hashes from the title and any leading/trailing spaces + return title.Substring(0, index).Trim() + title.Substring(index + i).Trim(); + } + } + + // If no hashes are found, return the original title + return title; + } + private bool IsHeading(string text) + { + // Detect headings, e.g., all caps or specific prefixes + return text == text.ToUpper() || Regex.IsMatch(text, @"^#"); + } + + private bool IsList(string text) + { + // Detect bullet points or ordered lists + return text.StartsWith("- ");// || Regex.IsMatch(text, @"^\d+\."); + } + + private bool IsQuote(string text) + { + // Simplistic quote detection + return text.StartsWith("\"") && text.EndsWith("\""); + } + + private bool HasBackticks(string text) + { + var backtickPattern = @"`[^`]*`"; + return Regex.IsMatch(text, backtickPattern); + } + + private void FormatBackTicks(StringBuilder htmlBuilder, string text) + { + var backtickPattern = @"`([^`]*)`"; + var formattedText = Regex.Replace(text, backtickPattern, "$1"); + htmlBuilder.AppendLine($"{formattedText}
"); + } + + private bool IsURL(string text) + { + // URL detection based on simple regex + var directURLPattern = @"^(http|https):\/\/[^\s$.?#].[^\s]*$"; + return Regex.IsMatch(text, directURLPattern); + } + private bool HasURLText(string markdown) + { + // Define the regex pattern to extract URLs from Markdown links. + string pattern = @"\[(.*?)\]\((http[s]?:\/\/[^\s\)]+)\)"; + + // Create a match to extract the text and URL from the Markdown link. + Match match = Regex.Match(markdown, pattern, RegexOptions.IgnoreCase); + if (match.Success) + { + return IsURL(match.Groups[2].Value); + } + return false; + } + private static string ConvertMarkdownToHtml(string markdown, StringBuilder htmlBuilder) + { + // Define the regex pattern to extract URLs from Markdown links. + string pattern = @"\[(.*?)\]\((http[s]?:\/\/[^\s\)]+)\)"; + + // Create a match to extract the text and URL from the Markdown link. + Match match = Regex.Match(markdown, pattern, RegexOptions.IgnoreCase); + if (match.Success) + { + string linkText = match.Groups[1].Value; // Group 1 contains the link text. + string url = match.Groups[2].Value; // Group 2 contains the URL. + + htmlBuilder.AppendLine($""); + } + + // Return original text if no Markdown link is found. + return markdown; + } + + private string ConvertToHtmlBold(string input) + { + string pattern = @"\*\*(.*?)\*\*"; + string result = Regex.Replace(input, pattern, "$1"); + if (HasBackticks(result)) + { + var backtickPattern = @"`([^`]*)`"; + var formattedText = Regex.Replace(result, backtickPattern, "$1"); + return formattedText; + } + return result; + } + + private bool ContainsBoldPattern(string input) + { + string pattern = @"\*\*.+?\*\*"; + return Regex.IsMatch(input, pattern); + } + + private void ConvertToHtmlList(string paragraph, StringBuilder htmlBuilder) + { + var items = paragraph.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); + htmlBuilder.AppendLine("