private static string UnwrapStyleMarkers()

in src/PSRule/Parser/MarkdownReader.cs [250:314]


        private static string UnwrapStyleMarkers(MarkdownStream stream, out MarkdownTokens flag)
        {
            flag = MarkdownTokens.None;

            // Check for style
            var styleChar = stream.Current;
            var stylePrevious = stream.Previous;
            var styleCount = styleChar == Asterix || styleChar == Underscore ? stream.Skip(styleChar, max: 0) : 0;
            var codeCount = styleChar == Backtick ? stream.Skip(Backtick, max: 0) : 0;

            CharacterMatchDelegate stop = IsTextStop;
            if (codeCount > 0)
                stop = IsCodeStop;

            var text = stream.CaptureUntil(stop, ignoreEscaping: false);

            // Check for italic and bold endings
            if (styleCount > 0)
            {
                if (stream.Current == styleChar)
                {
                    var styleEnding = stream.Skip(styleChar, max: styleCount);

                    // Add back underscores within text
                    if (styleChar == Underscore && stylePrevious != Whitespace)
                        return Pad(text, styleChar, left: styleCount, right: styleCount);

                    // Add back asterixes/underscores that are part of text
                    if (styleEnding < styleCount)
                        text = Pad(text, styleChar, left: styleCount - styleEnding);

                    if (styleEnding == 1 || styleEnding == 3)
                        flag |= MarkdownTokens.Italic;

                    if (styleEnding >= 2)
                        flag |= MarkdownTokens.Bold;
                }
                else
                {
                    // Add back asterixes/underscores that are part of text
                    text = Pad(text, styleChar, left: styleCount);
                }
            }

            if (codeCount > 0)
            {
                if (stream.Current == styleChar)
                {
                    var codeEnding = stream.Skip(styleChar, max: 1);

                    // Add back backticks that are part of text
                    if (codeEnding < codeCount)
                        text = Pad(text, styleChar, left: codeCount - codeEnding);

                    if (codeEnding == 1)
                        flag |= MarkdownTokens.Code;
                }
                else
                {
                    // Add back backticks that are part of text
                    text = Pad(text, styleChar, left: codeCount);
                }
            }
            return text;
        }