Удалить теги html из MainBody - PullRequest
0 голосов
/ 24 марта 2020

У меня возникла проблема, когда я пытаюсь удалить все html теги из этой строки кода EPiServer

 @(Html.PropertyFor(m => m.MainBody)

Поскольку предполагается, что это внутри <a>example code here</a>

Что хороший способ решить эту проблему при запуске EPi Server?

1 Ответ

2 голосов
/ 24 марта 2020

Во-первых, это плохая практика использования XhtmlString таким образом, что, как говорится, мы не всегда можем выбирать.

Я использую это, который является модифицированной версией метода расширения Роба Волка.

using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

public static class HtmlStringExtensions
{
    /// <summary>
    /// Truncates a string containing HTML to a number of text characters, keeping whole words.
    /// The result contains HTML and any tags left open are closed.
    /// by Rob Volk with modifications
    /// http://robvolk.com/truncate-html-string-c-extension-method/
    /// </summary>
    /// <param name="html"></param>
    /// <param name="maxCharacters"></param>
    /// <param name="trailingText"></param>
    /// <returns></returns>
    public static string TruncateHtmlString(this string html, int maxCharacters, string trailingText)
    {
        if (string.IsNullOrEmpty(html))
            return html;

        // find the spot to truncate
        // count the text characters and ignore tags
        var textCount = 0;
        var charCount = 0;
        var ignore = false;
        var newString = string.Empty;
        foreach (char c in html)
        {
            newString += c;

            charCount++;
            if (c == '<')
            {
                ignore = true;
            }
            else if (!ignore)
            {
                textCount++;
            }

            if (c == '>')
            {
                ignore = false;
            }

            // stop once we hit the limit
            if (textCount >= maxCharacters)
            {
                break;
            }
        }

        // Truncate the html and keep whole words only
        var trunc = new StringBuilder(newString);
        //var trunc = new StringBuilder(html.TruncateWords(charCount));

        // keep track of open tags and close any tags left open
        var tags = new Stack<string>();
        var matches = Regex.Matches(trunc.ToString(), // trunc.ToString()
            @"<((?<tag>[^\s/>]+)|/(?<closeTag>[^\s>]+)).*?(?<selfClose>/)?\s*>",
            RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline);

        foreach (Match match in matches)
        {
            if (match.Success)
            {
                var tag = match.Groups["tag"].Value;
                var closeTag = match.Groups["closeTag"].Value;

                // push to stack if open tag and ignore it if it is self-closing, i.e. <br />
                if (!string.IsNullOrEmpty(tag) && string.IsNullOrEmpty(match.Groups["selfClose"].Value))
                    tags.Push(tag);

                // pop from stack if close tag
                else if (!string.IsNullOrEmpty(closeTag))
                {
                    // pop the tag to close it.. find the matching opening tag
                    // ignore any unclosed tags
                    while (tags.Pop() != closeTag && tags.Count > 0)
                    { }
                }
            }
        }

        if (html.Length > charCount)
            // add the trailing text
            trunc.Append(trailingText);

        // pop the rest off the stack to close remainder of tags
        while (tags.Count > 0)
        {
            trunc.Append("</");
            trunc.Append(tags.Pop());
            trunc.Append('>');
        }

        return trunc.ToString();
    }

    /// <summary>
    /// Truncates a string containing HTML to a number of text characters, keeping whole words.
    /// The result contains HTML and any tags left open are closed.
    /// </summary>
    /// <param name="html"></param>
    /// <param name="maxCharacters"></param>
    /// <returns></returns>
    public static string TruncateHtmlString(this string html, int maxCharacters)
    {
        return html.TruncateHtmlString(maxCharacters, null);
    }

    /// <summary>
    /// Strips all HTML tags from a string
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string StripHtml(this string html)
    {
        if (string.IsNullOrEmpty(html))
            return html;

        return Regex.Replace(html, @"<(.|\n)*?>", string.Empty);
    }

}

Реализация с использованием ToHtmlString() из EPiServer.Core

Например

// @using EPiServer.Core
@(Html.PropertyFor(m => m.MainBody.ToHtmlString().TruncateHtmlString(160, "..."))
...