src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierExtension.cs
/// <summary>
/// Process the inlines of the heading to create a unique identifier
/// </summary>
/// <param name="processor">The processor.</param>
/// <param name="inline">The inline.</param>
Process the inlines of the heading to create a unique identifier
Process the inlines of the heading to create a unique identifier
private void HeadingBlock_ProcessInlinesEnd(InlineProcessor processor, Inline? inline)
        {
            var identifiers = processor.Document.GetData(AutoIdentifierKey) as HashSet<string>;
            if (identifiers is null)
            {
                identifiers = new HashSet<string>();
                processor.Document.SetData(AutoIdentifierKey, identifiers);
            }
            var headingBlock = (HeadingBlock) processor.Block!;
            if (headingBlock.Inline is null)
            {
                return;
            }
            var attributes = processor.Block!.GetAttributes();
            if (attributes.Id != null)
            {
                return;
            }
            var stripRenderer = rendererCache.Get();
            stripRenderer.Render(headingBlock.Inline);
            var headingText = stripRenderer.Writer.ToString()!;
            rendererCache.Release(stripRenderer);
            headingText = (options & AutoIdentifierOptions.GitHub) != 0
                ? LinkHelper.UrilizeAsGfm(headingText)
                : LinkHelper.Urilize(headingText, (options & AutoIdentifierOptions.AllowOnlyAscii) != 0);
            var baseHeadingId = string.IsNullOrEmpty(headingText) ? "section" : headingText;
            int index = 0;
            var headingId = baseHeadingId;
            var headingBuffer = StringBuilderCache.Local();
            while (!identifiers.Add(headingId))
            {
                index++;
                headingBuffer.Append(baseHeadingId);
                headingBuffer.Append('-');
                headingBuffer.Append(index);
                headingId = headingBuffer.ToString();
                headingBuffer.Length = 0;
            }
            attributes.Id = headingId;
        }
private void HeadingBlock_ProcessInlinesEnd ( InlineProcessor processor , Inline ? inline ) { var identifiers = processor . Document . GetData ( AutoIdentifierKey ) as HashSet < string > ; if ( identifiers is null ) { identifiers = new HashSet < string > ( ) ; processor . Document . SetData ( AutoIdentifierKey , identifiers ) ; } var headingBlock = ( HeadingBlock ) processor . Block ! ; if ( headingBlock . Inline is null ) { return ; } var attributes = processor . Block ! . GetAttributes ( ) ; if ( attributes . Id != null ) { return ; } var stripRenderer = rendererCache . Get ( ) ; stripRenderer . Render ( headingBlock . Inline ) ; var headingText = stripRenderer . Writer . ToString ( ) ! ; rendererCache . Release ( stripRenderer ) ; headingText = ( options & AutoIdentifierOptions . GitHub ) != 0 ? LinkHelper . UrilizeAsGfm ( headingText ) : LinkHelper . Urilize ( headingText , ( options & AutoIdentifierOptions . AllowOnlyAscii ) != 0 ) ; var baseHeadingId = string . IsNullOrEmpty ( headingText ) ? " " : headingText ; int index = 0 ; var headingId = baseHeadingId ; var headingBuffer = StringBuilderCache . Local ( ) ; while ( ! identifiers . Add ( headingId ) ) { index ++ ; headingBuffer . Append ( baseHeadingId ) ; headingBuffer . Append ( ' ' ) ; headingBuffer . Append ( index ) ; headingId = headingBuffer . ToString ( ) ; headingBuffer . Length = 0 ; } attributes . Id = headingId ; }
Process the inlines of the heading to create a unique identifier
Process the inlines of the heading to create a unique identifier
// If id is already set, don't try to modify it // Use internally a HtmlRenderer to strip links from a heading // Urilize the link // If the heading is empty, use the word "section" instead // Add a trailing -1, -2, -3...etc. in case of collision
processor
InlineProcessor
None
inline
Inline?
None