Ejemplo n.º 1
0
        /// <summary>
        /// Parsing helper method.
        /// </summary>
        /// <param name="listItem">The listItem to edit.</param>
        /// <param name="markdown">The markdown text.</param>
        /// <param name="start">The start position.</param>
        /// <param name="end">The end position.</param>
        /// <param name="newLine">True if the text is a new line. Prefixed whitespace is ignored then.</param>
        private static void AppendTextToListItem(ListItemBlock listItem, string markdown, int start, int end, bool newLine = false)
        {
            ListItemBuilder?listItemBuilder = null;

            if (listItem.Blocks.Count > 0)
            {
                listItemBuilder = listItem.Blocks[listItem.Blocks.Count - 1] as ListItemBuilder;
            }

            if (listItemBuilder == null)
            {
                // Add a new block.
                listItemBuilder = new ListItemBuilder();
                listItem.Blocks.Add(listItemBuilder);
            }

            var builder = listItemBuilder.Builder;

            if (builder.Length >= 2 &&
                ParseHelpers.IsMarkdownWhiteSpace(builder[builder.Length - 2]) &&
                ParseHelpers.IsMarkdownWhiteSpace(builder[builder.Length - 1]))
            {
                builder.Length -= 2;
                builder.AppendLine();
            }
            else if (builder.Length > 0)
            {
                builder.Append(' ');
            }

            if (newLine)
            {
                start = Common.FindNextNoneWhiteSpace(markdown, start, end, true);
            }

            builder.Append(markdown, start, end - start);
        }
Ejemplo n.º 2
0
            private (ListItemBlock item, int consumedLines, ListStyle style, int startNumber)? ParseItemBlock(LineBlock markdown, MarkdownDocument document)
            {
                if (markdown.LineCount == 0)
                {
                    return(null);
                }

                var firstLine = markdown[0];

                var preInformation = GetListStyle(firstLine);

                if (preInformation is null)
                {
                    return(null);
                }

                var(style, preLength, startNumber) = preInformation.Value;

                var  indention   = preLength;
                bool isFirstLine = true;
                bool isLazy      = true;
                var  listBlock   = markdown.RemoveFromLine((line, lineIndex) =>
                {
                    if (isFirstLine)
                    {
                        isFirstLine = false;
                        return(indention, line.Length - indention, false, false);
                    }

                    if (line.IsWhiteSpace())
                    {
                        isLazy = false;
                        return(0, 0, false, false);
                    }

                    var firstNonWhitespace = line.IndexOfNonWhiteSpace();

                    var listInfo = GetListStyle(line.Slice(firstNonWhitespace));
                    if (listInfo.HasValue)
                    {
                        // a sub list must be indented more then 2 char more then the current indention
                        if (firstNonWhitespace >= indention)
                        {
                            // another list item will also disable lazy
                            isLazy = false;
                            return(indention, line.Length - indention, false, false);
                        }

                        // every other list will stop the current block
                        return(0, 0, true, true);
                    }

                    if (firstNonWhitespace < indention)
                    {
                        if (isLazy)
                        {
                            return(0, line.Length, false, false);
                        }

                        return(0, 0, true, true);
                    }

                    // if we are indented we will no longer support lazy
                    isLazy = false;
                    return(indention, line.Length - indention, false, false);
                });

                // remove empty lines at the end
                listBlock = listBlock.TrimEnd();

                var item = new ListItemBlock()
                {
                    Blocks = document.ParseBlocks(listBlock),
                };

                return(item, listBlock.LineCount, style, startNumber);
            }