Copied to clipboard

Flag this post as spam?

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


  • bh 408 posts 1395 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 112 posts 335 karma points
    Mar 13, 2019 @ 23:10
    Thomsen
    2

    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 408 posts 1395 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 2141 posts 14324 karma points MVP 8x 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 81 karma points
    Jul 15, 2019 @ 19:02
    Thomas Higginbotham
    2

    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();
    
  • Alexander Gräf 2 posts 25 karma points
    Feb 01, 2020 @ 21:08
    Alexander Gräf
    3

    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:

    @Html.Raw(Regex.Match(Model.BodyText.ToHtmlString(), "^(<p>(?<Text>.*)</p>)|(?<Text>.*)", RegexOptions.Singleline).Groups["Text"])
    

    Needs a @using at the top:

    @using System.Text.RegularExpressions
    
Please Sign in or register to post replies

Write your reply to:

Draft