/// <summary> /// Initializes a new instance of the <see cref="MarkdownDifferenceUpdater"/> class. /// </summary> /// <param name="oldSourceText">The previous version of the source text.</param> /// <param name="oldDocument">The previous version of the parsed markdown document.</param> /// <param name="pipeline">The markdown pipeline used to process the source text.</param> /// <param name="styles">The markdown styles to be used for the rendering.</param> /// <param name="imageProvider">The image provider.</param> /// <param name="newSourceText">The new version of the source text.</param> /// <param name="newSourceTextUsn">The serial number of the new source text.</param> /// <param name="flowDocument">The flow document to render to.</param> /// <param name="dispatcher">The dispatcher (used here to process some actions in Gui context).</param> /// <param name="newDocumentSetter"> /// Action that is called at the end of the processing in Gui context. It is intended to set the new new document in the class that is calling this function. /// The arguments are: i) the new source text, ii) the serial number of the new source text, and iii) the parsed markdown document. /// </param> /// <param name="setFlowDocumentUpdateInProgressFlag"> /// Action that is called in Gui context. It is intended to signal the class that is calling this function exactly when the flow document is updated. /// This action is called twice: at the beginning of the update with the argument set to true, and at the end of the update with the argument set to false. /// Argument is a flag that tells if the update of the flow document is in progress now. /// </param> /// <param name="cancellationToken">Cancellation token that can be used to cancel the update, e.g. if the user has entered new text.</param> public MarkdownDifferenceUpdater(string oldSourceText, MarkdownDocument oldDocument, MarkdownPipeline pipeline, IStyles styles, IWpfImageProvider imageProvider, string newSourceText, long newSourceTextUsn, FlowDocument flowDocument, Dispatcher dispatcher, Action <string, long, MarkdownDocument> newDocumentSetter, Action <bool> setFlowDocumentUpdateInProgressFlag, CancellationToken cancellationToken) { OldSourceText = oldSourceText; OldDocument = oldDocument; Pipeline = pipeline; Styles = styles; ImageProvider = imageProvider; NewSourceText = newSourceText; NewSourceTextUsn = newSourceTextUsn; FlowDocument = flowDocument; Dispatcher = dispatcher; NewTextAndDocumentSetter = newDocumentSetter; SetFlowDocumentUpdateInProgressFlag = setFlowDocumentUpdateInProgressFlag; _cancellationToken = cancellationToken; }
/// <summary> /// Initializes a new instance of the <see cref="LinkReferenceTrackerPostProcessor"/> class, and executes the post-processor /// </summary> /// <param name="document">The document.</param> /// <param name="updateSequenceNumber">A number that is increased every time the source document changed.</param> /// <param name="imageProvider">The image provider. Can be null (in this case nothing is tracked).</param> public static void TrackLinks(MarkdownDocument document, long updateSequenceNumber, IWpfImageProvider imageProvider) { var urlCollector = imageProvider?.CreateUrlCollector(); if (null != urlCollector) { foreach (var element in PositionHelper.EnumerateAllMarkdownObjectsRecursively(document)) { if (element is LinkInline linkInline) { if (linkInline.UrlSpan.HasValue) { urlCollector.AddUrl(linkInline.IsImage, linkInline.Url, linkInline.UrlSpan.Value.Start, linkInline.UrlSpan.Value.End); } } } urlCollector.Freeze(); // announce that the collection proccess has finished imageProvider.UpdateUrlCollector(urlCollector, updateSequenceNumber); } }
/// <summary> /// Converts a list of (top level) <see cref="MarkdownObject"/>s to a list of (top level) <see cref="TextElement"/>s. /// </summary> /// <param name="markdownObjects">List of top level markdown objects.</param> /// <param name="pipeline">The pipeline used for the conversion.</param> /// <returns>The list of (top level) <see cref="TextElement"/>s as the result of the conversion.</returns> /// <exception cref="System.ArgumentNullException">if markdown variable is null</exception> public static IList <TextElement> ListOfMarkdownObjectsToListOfTextElements(IList <MarkdownObject> markdownObjects, MarkdownPipeline pipeline, IStyles styles, IWpfImageProvider imageProvider) { var p = new Paragraph(); styles.ApplyParagraphStyle(p); var result = new TextElementList() { FontSize = p.FontSize }; var renderer = new Markdig.Renderers.WpfRenderer(result, styles) { ImageProvider = imageProvider }; pipeline.Setup(renderer); renderer.Render(markdownObjects); return(result); }