private static Inline CreateLinkToFootnote(InlineProcessor state, LinkReferenceDefinition linkRef, Inline?child) { var footnote = ((FootnoteLinkReferenceDefinition)linkRef).Footnote; if (footnote.Order < 0) { var footnotes = (FootnoteGroup)state.Document.GetData(DocumentKey) !; footnotes.CurrentOrder++; footnote.Order = footnotes.CurrentOrder; } var link = new FootnoteLink(footnote); footnote.Links.Add(link); return(link); }
/// <summary> /// Add footnotes to the end of the document /// </summary> /// <param name="state">The processor.</param> /// <param name="inline">The inline.</param> private void Document_ProcessInlinesEnd(InlineProcessor state, Inline inline) { // Unregister state.Document.ProcessInlinesEnd -= Document_ProcessInlinesEnd; var footnotes = ((FootnoteGroup)state.Document.GetData(DocumentKey)); // Remove the footnotes from the document and readd them at the end state.Document.Remove(footnotes); state.Document.Add(footnotes); state.Document.RemoveData(DocumentKey); footnotes.Sort( (leftObj, rightObj) => { var left = (Footnote)leftObj; var right = (Footnote)rightObj; return(left.Order >= 0 && right.Order >= 0 ? left.Order.CompareTo(right.Order) : 0); }); int linkIndex = 0; for (int i = 0; i < footnotes.Count; i++) { var footnote = (Footnote)footnotes[i]; if (footnote.Order < 0) { // Remove this footnote if it doesn't have any links footnotes.RemoveAt(i); i--; continue; } // Insert all footnote backlinks var paragraphBlock = footnote.LastChild as ParagraphBlock; if (paragraphBlock == null) { paragraphBlock = new ParagraphBlock(); footnote.Add(paragraphBlock); } if (paragraphBlock.Inline == null) { paragraphBlock.Inline = new ContainerInline(); } foreach (var link in footnote.Links) { linkIndex++; link.Index = linkIndex; var backLink = new FootnoteLink() { Index = linkIndex, IsBackLink = true, Footnote = footnote }; paragraphBlock.Inline.AppendChild(backLink); } } }