Exemple #1
0
        private void OnHeadingBlockParsed(BlockProcessor processor, Block block)
        {
            if (!(block is HeadingBlock headingBlock) || block is BlogMetadataBlock)
            {
                return;
            }

            if (headingBlock.Level < 2)
            {
                return; // Ignore h1 since there's no point including it.
            }
            var document = processor.Document;
            var toc      = document.Where(b => b is TableOfContentsBlock).FirstOrDefault() as TableOfContentsBlock;

            if (toc == null)
            {
                return;
            }

            ContainerBlock parent = toc;

            for (int i = 0; i < headingBlock.Level - 2; i++) // 2 is the minimum level we support, hence -2
            {
                if (!(parent.LastChild is ContainerBlock childContainer))
                {
                    childContainer = new ListItemBlock(block.Parser);
                    parent.Add(childContainer);
                }
                parent = (ContainerBlock)parent.LastChild;
            }

            var headingCopy = new HeadingBlock(block.Parser)
            {
                Column                    = headingBlock.Column,
                HeaderChar                = headingBlock.HeaderChar,
                Inline                    = headingBlock.Inline,
                IsBreakable               = headingBlock.IsBreakable,
                IsOpen                    = headingBlock.IsOpen,
                Level                     = headingBlock.Level,
                Line                      = headingBlock.Line,
                ProcessInlines            = headingBlock.ProcessInlines,
                RemoveAfterProcessInlines = headingBlock.RemoveAfterProcessInlines,
                Span                      = headingBlock.Span
            };

            headingCopy.Lines = new StringLineGroup(headingBlock.Lines.Lines.Length);
            headingCopy.SetAttributes(headingBlock.GetAttributes());
            foreach (var line in headingBlock.Lines.Lines)
            {
                if (line.Slice.Text == null)
                {
                    continue;
                }

                var textCopy     = new StringSlice(line.Slice.Text, line.Slice.Start, line.Slice.End);
                var reffableLine = new StringLine(ref textCopy);
                headingCopy.Lines.Add(ref reffableLine);
            }
            parent.Add(headingCopy);
        }
            protected override void Write(HtmlRenderer renderer, HeadingBlock heading)
            {
                // Clone the heading block, and increment the level
                var hackHeading = new HeadingBlock(heading.Parser)
                {
                    Level = heading.Level + 1,

                    Column                    = heading.Column,
                    HeaderChar                = heading.HeaderChar,
                    Inline                    = heading.Inline,
                    IsBreakable               = heading.IsBreakable,
                    IsOpen                    = heading.IsOpen,
                    Line                      = heading.Line,
                    Lines                     = heading.Lines,
                    ProcessInlines            = heading.ProcessInlines,
                    RemoveAfterProcessInlines = heading.RemoveAfterProcessInlines,
                    Span                      = heading.Span,
                };

                hackHeading.SetAttributes(heading.GetAttributes());
                base.Write(renderer, hackHeading);
            }