/// <summary> /// Gets the childs of a markdown object. Null is returned if no childs were to be found. /// </summary> /// <param name="parent">The markdown object from which to get the childs.</param> /// <returns>The childs of the given markdown object, or null.</returns> public static IEnumerable <Markdig.Syntax.MarkdownObject> GetChilds(Markdig.Syntax.MarkdownObject parent) { if (parent is Markdig.Syntax.LeafBlock leafBlock) { return(leafBlock.Inline); } else if (parent is Markdig.Syntax.Inlines.ContainerInline containerInline) { return(containerInline); } else if (parent is Markdig.Syntax.ContainerBlock containerBlock) { return(containerBlock); } else { return(null); } }
/// <summary> /// Gets the childs of a markdown object. Null is returned if no childs were to be found. /// </summary> /// <param name="parent">The markdown object from which to get the childs.</param> /// <returns>The childs of the given markdown object, or null.</returns> public static IReadOnlyList <Markdig.Syntax.MarkdownObject> GetChildList(Markdig.Syntax.MarkdownObject parent) { if (parent is Markdig.Syntax.LeafBlock leafBlock) { return(leafBlock.Inline?.ToArray <Markdig.Syntax.MarkdownObject>()); } else if (parent is Markdig.Syntax.Inlines.ContainerInline containerInline) { return(containerInline.ToArray <Markdig.Syntax.MarkdownObject>()); } else if (parent is Markdig.Syntax.ContainerBlock containerBlock) { return(containerBlock); } else { return(null); } }
/// <summary> /// Converts a position in the flow document into a position in the source text. A flag is delivered too, which /// indicates whether the returned position is considered to be accurate or only approximate. /// </summary> /// <param name="textPosition">The text position in the viewer.</param> /// <returns>The source text position and a flag indicating if the returned source text position is accurate.</returns> public static (int sourceTextOffset, bool isReturnedPositionAccurate) ViewersTextPositionToSourceEditorsTextPosition(TextPointer textPosition) { bool isReturnedPositionAccurate = true; TextElement parent; if (textPosition.Parent is TextElement pe) { parent = pe; } else { parent = textPosition.Paragraph; isReturnedPositionAccurate = false; // we are maybe in an image or a checkbox, thus the returned position may not be accurate } // search parent or the ancestors of parent for a Markdig tag Markdig.Syntax.MarkdownObject markdigTag = null; while (null != parent) { if (parent.Tag is Markdig.Syntax.MarkdownObject mdo) { markdigTag = mdo; break; } parent = parent.Parent as TextElement; isReturnedPositionAccurate = false; // we have to use the parent of this text element, thus the returned positon may not be accurate } if (null != markdigTag) { int charOffset = parent.ContentStart.GetOffsetToPosition(textPosition); int sourceTextOffset = ContentSpan(markdigTag).Start + charOffset; return(sourceTextOffset, isReturnedPositionAccurate); } else { return(0, false); } }
private void Renderer_ObjectWriteBefore(Markdig.Renderers.IMarkdownRenderer arg1, Markdig.Syntax.MarkdownObject obj) { var linkInline = obj as Markdig.Syntax.Inlines.LinkInline; if (linkInline != null) { OnBeforeWriteLinkInline(linkInline); } }
/// <summary> /// Enumerates all objects in a markdown parse tree recursively, starting with the given element. /// </summary> /// <param name="startElement">The start element.</param> /// <returns>All text element (the given text element and all its childs).</returns> public static IEnumerable <Markdig.Syntax.MarkdownObject> EnumerateAllMarkdownObjectsRecursively(Markdig.Syntax.MarkdownObject startElement) { yield return(startElement); var childList = GetChildList(startElement); if (null != childList) { foreach (var child in GetChildList(startElement)) { foreach (var childAndSub in EnumerateAllMarkdownObjectsRecursively(child)) { yield return(childAndSub); } } } }