public TokenMd GetToken(string text, int index, out int finalIndex) { link = default; linkText = default; finalIndex = index; builder = new StringBuilder(); mark = new LinkMark(); helper = new ParseHelper(); isFirst = true; symbolsCount = 0; currentSymbol = "["; var isLink = true; while (finalIndex < text.Length) { if (isFirst && text[finalIndex] != '[') { isLink = false; } var resToken = HandleSquareBrackets(text, index, finalIndex, out finalIndex); if (resToken != null) { isLink = false; } resToken = HandleOpenRoundBrackets(text, index, finalIndex, out finalIndex); if (resToken != null) { isLink = false; } if (text[finalIndex] == ')' && currentSymbol == "(") { link = builder.ToString(); finalIndex += 2; symbolsCount++; mark.LinkText = linkText; mark.Link = link; if (string.IsNullOrEmpty(linkText) && string.IsNullOrEmpty(link)) { isLink = false; } if (symbolsCount == mark.AllSymbols.Length) { if (isLink) { return(new TokenMd(linkText, mark)); } var tokenText = text.Substring(index, finalIndex - index); return(new TokenMd(tokenText, new EmptyMark())); } } var resultText = text.Substring(index, finalIndex - index); return(new TokenMd(resultText, new EmptyMark())); } return(new TokenMd(text.Substring(index, finalIndex - index), new EmptyMark())); }