Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • bh 175 posts 598 karma points
    Mar 13, 2019 @ 20:24
    bh
    0

    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.

  • Thomsen 76 posts 267 karma points
    Mar 13, 2019 @ 23:10
    Thomsen
    0

    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 ...

  • bh 175 posts 598 karma points
    Mar 15, 2019 @ 16:31
    bh
    0

    Does anyone have a alternative for the deprecated RemoveFirstParagraphTag? I use that method extensively throughout my projects.

  • Marc Goodson 945 posts 6284 karma points MVP 4x c-trib
    Mar 15, 2019 @ 21:29
    Marc Goodson
    0

    Hi bh

    The implementation for it was like so:

     public static string RemoveFirstParagraphTag(string text)
            {
                if (String.IsNullOrEmpty(text))
                    return "";
    
                if (text.Length > 5)
                {
                    if (text.ToUpper().Substring(0, 3) == "<P>")
                        text = text.Substring(3, text.Length - 3);
                    if (text.ToUpper().Substring(text.Length - 4, 4) == "</P>")
                        text = text.Substring(0, text.Length - 4);
                }
                return text;
            }
    

    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

  • Thomas Higginbotham 4 posts 78 karma points
    Jul 15, 2019 @ 19:02
    Thomas Higginbotham
    1

    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);
            }
        }
    }
    

    Usage from controller:

    using MyNamespace.Extensions;
    
    ...
    
    string rawContent = content.Value<string>("myContent");
    string fixedContent = rawContent.RemoveParagraphWrapperTags();
    
Please Sign in or register to post replies

Write your reply to:

Draft