问题
I'd need to get a html friendly version of a truncated Xhtmlstring as the tag endings might get clipped when truncated. Any ideas on how to achieve this? I've thought of just getting rid of all tags first and then clipping but is there a solution for this inside episerver or is this just basic string-manipulation with regex?
回答1:
There is a built-in helper function in the TextIndexer class called StripHtml which can be used to remove any tags to end up with plain text before truncating:
var plainText = TextIndexer.StripHtml(someHtml);
Note that this method can also be used to truncate the string like so:
// Truncate to 150 characters
var truncatedString = TextIndexer.StripHtml(someHtml, 150);
You'll also be able to have a string such as "..." appended to the string if it was truncated.
回答2:
For valid XHTML you can use the XElement
class to simplify things, i.e. you do not care for the occasional regular expression frenzy. The following example should work well for the trivial case when there is only one text-node present:
public class Truncator {
private const String Ellipsis = "…";
private const String EllipsisHtmlEntity = "…";
public static String Truncate(XElement xElement, Int32 length, Boolean useHtmlEntity = false) {
if (ReferenceEquals(xElement, null))
throw new ArgumentException(nameof(xElement));
var textNode =
(XText)
xElement.DescendantNodes()
.FirstOrDefault(node => !ReferenceEquals(node, null) && node.NodeType == XmlNodeType.Text);
if (!ReferenceEquals(textNode, null))
textNode.Value = Truncate(textNode.Value, length);
var truncatedResult = xElement.ToString(SaveOptions.DisableFormatting);
return useHtmlEntity ? truncatedResult.Replace(Ellipsis, EllipsisHtmlEntity) : truncatedResult;
}
public static String Truncate(String str, Int32 length, Boolean useHtmlEntity = false) {
if (String.IsNullOrWhiteSpace(str))
return str;
var truncated = str.Trim().Substring(0, length - 1).Trim();
return String.IsNullOrWhiteSpace(str) || str.Length < length
? str
: $"{truncated}{(useHtmlEntity ? EllipsisHtmlEntity : Ellipsis)}";
}
}
If you have a String
to begin with, just XElement.Parse(it)
to get the XElement
.
来源:https://stackoverflow.com/questions/34151343/truncating-xhtmlstring-in-episerver