Exemple #1
0
        public string ConvertMarkdown(string markdown)
        {
            var sanitizer = new HtmlSanitizer();

            sanitizer.RemovingAttribute += OnRemovingAttribute;

            // disallow form elements
            sanitizer.AllowedTags.Remove("form");
            sanitizer.AllowedTags.Remove("button");
            sanitizer.AllowedTags.Remove("input");

            // parse the markdown to HTML
            markdown = Westwind.AspNetCore.Markdown.Markdown.Parse(markdown);

            // encode any custom tags (pasting e-mails and other markup (XML))
            // From author of HtmlSanitizer
            // https://gist.github.com/mganss/00bec2c2245c0ef86d9c82d6211def7b
            markdown = HtmlRegex.Replace(markdown, m =>
            {
                var tagName = m.Groups[1].Value;
                if (!HtmlTags.Contains(tagName))
                {
                    return("<" + m.Value.Substring(1));
                }
                return(m.Value);
            });

            markdown = sanitizer.Sanitize(markdown);

            return(markdown);
        }
 public static string StripTagsRegexCompiled(string source)
 {
     return(HtmlRegex.Replace(source, string.Empty));
 }