One problem with using this method from Umbraco is that if you have multiple paragraphs or content that follows a paragraph, it will only strip the first opening tag and the last closing tag, which could result in something like the following:
My first paragraph</p> <p>My last paragraph
Or this:
A paragraph of text</p> <ul><li>A list item</li></ul>
It's usually better to leave the text alone in those cases since we're only interested in removing paragraph tags that Umbraco injected around the content. Here's a better implementation:
namespace MyNamespace.Extensions
{
public static class StringExtensions
{
/// <summary>
/// Strips out <P> and </P> tags if they were used as a wrapper
/// for other HTML content.
/// </summary>
/// <param name="text">The HTML text.</param>
public static string RemoveParagraphWrapperTags(this string text)
{
if (string.IsNullOrEmpty(text))
{
return text;
}
string trimmedText = text.Trim();
string upperText = trimmedText.ToUpper();
int paragraphIndex = upperText.IndexOf("<P>");
if (paragraphIndex != 0 ||
paragraphIndex != upperText.LastIndexOf("<P>") ||
upperText.Substring(upperText.Length - 4, 4) != "</P>")
{
// Paragraph not used as a wrapper element
return text;
}
// Remove paragraph wrapper tags
return trimmedText.Substring(3, trimmedText.Length - 7);
}
}
}
RemoveFirstParagraphTag has been completely removed from Umbraco 8, which the developers confirmed. It hasn't been reimplemented in any of the Library or Helper classes.
This is the shortest method I found - might be helpful:
v8 RemoveFirstParagraphTag
I've been using
@Html.Raw(umbraco.library.RemoveFirstParagraphTag(Model.GetPropertyValue("homeNewsPerspectiveTitle").ToString()))
what's the v8 version of that? umbraco.library.RemoveFirstParagraphTag doesn't seem to exist, or if it does I haven't found it yet.
The whole umbraco.library has been obsolete for years and has now been removed from v8 as a part of the "code cleanup".
Though, the RemoveFirstParagraphTag method was nice and should be requested re-invoked ...
Does anyone have a alternative for the deprecated RemoveFirstParagraphTag? I use that method extensively throughout my projects.
Hi bh
The implementation for it was like so:
so you could add an extension method or service to do the same logic in your new projects.
https://github.com/umbraco/Umbraco-CMS/blob/dev-v7/src/Umbraco.Web/umbraco.presentation/library.cs
regards
Marc
One problem with using this method from Umbraco is that if you have multiple paragraphs or content that follows a paragraph, it will only strip the first opening tag and the last closing tag, which could result in something like the following:
Or this:
It's usually better to leave the text alone in those cases since we're only interested in removing paragraph tags that Umbraco injected around the content. Here's a better implementation:
Usage from controller:
RemoveFirstParagraphTag has been completely removed from Umbraco 8, which the developers confirmed. It hasn't been reimplemented in any of the Library or Helper classes.
This is the shortest method I found - might be helpful:
Needs a @using at the top:
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.