src/modules/previewpane/MarkdownPreviewHandler/MarkdownPreviewHandlerControl.cs
/// <summary>
/// Start the preview on the Control.
/// </summary>
/// <param name="dataSource">Path to the file.</param>
Start the preview on the Control.
Start the preview on the Control .
public override void DoPreview<T>(T dataSource)
        {
            this.infoBarDisplayed = false;
            try
            {
                if (!(dataSource is string filePath))
                {
                    throw new ArgumentException($"{nameof(dataSource)} for {nameof(MarkdownPreviewHandler)} must be a string but was a '{typeof(T)}'");
                }
                string fileText = File.ReadAllText(filePath);
                Regex imageTagRegex = new Regex(@"<[ ]*img.*>");
                if (imageTagRegex.IsMatch(fileText))
                {
                    this.infoBarDisplayed = true;
                }
                this.extension.BaseUrl = Path.GetDirectoryName(filePath);
                MarkdownPipeline pipeline = this.pipelineBuilder.Build();
                string parsedMarkdown = Markdown.ToHtml(fileText, pipeline);
                string markdownHTML = $"{this.htmlHeader}{parsedMarkdown}{this.htmlFooter}";
                this.InvokeOnControlThread(() =>
                {
                    this.browser = new WebBrowserExt
                    {
                        DocumentText = markdownHTML,
                        Dock = DockStyle.Fill,
                        IsWebBrowserContextMenuEnabled = false,
                        ScriptErrorsSuppressed = true,
                        ScrollBarsEnabled = true,
                        AllowNavigation = false,
                    };
                    this.Controls.Add(this.browser);
                    if (this.infoBarDisplayed)
                    {
                        this.infoBar = this.GetTextBoxControl(Resources.BlockedImageInfoText);
                        this.Resize += this.FormResized;
                        this.Controls.Add(this.infoBar);
                    }
                });
                PowerToysTelemetry.Log.WriteEvent(new MarkdownFilePreviewed());
            }
            catch (Exception e)
            {
                PowerToysTelemetry.Log.WriteEvent(new MarkdownFilePreviewError { Message = e.Message });
                this.InvokeOnControlThread(() =>
                {
                    this.Controls.Clear();
                    this.infoBarDisplayed = true;
                    this.infoBar = this.GetTextBoxControl(Resources.MarkdownNotPreviewedError);
                    this.Resize += this.FormResized;
                    this.Controls.Add(this.infoBar);
                });
            }
            finally
            {
                base.DoPreview(dataSource);
            }
        }
public override void DoPreview < T > ( T dataSource ) { this . infoBarDisplayed = false ; try { if ( ! ( dataSource is string filePath ) ) { throw new ArgumentException ( $" { nameof ( dataSource ) }  for  { nameof ( MarkdownPreviewHandler ) }  must be a string but was a ' { typeof ( T ) } ' " ) ; } string fileText = File . ReadAllText ( filePath ) ; Regex imageTagRegex = new Regex ( @"<[ ]*img.*>" ) ; if ( imageTagRegex . IsMatch ( fileText ) ) { this . infoBarDisplayed = true ; } this . extension . BaseUrl = Path . GetDirectoryName ( filePath ) ; MarkdownPipeline pipeline = this . pipelineBuilder . Build ( ) ; string parsedMarkdown = Markdown . ToHtml ( fileText , pipeline ) ; string markdownHTML = $" { this . htmlHeader } { parsedMarkdown } { this . htmlFooter } " ; this . InvokeOnControlThread ( ( ) => { this . browser = new WebBrowserExt { DocumentText = markdownHTML , Dock = DockStyle . Fill , IsWebBrowserContextMenuEnabled = false , ScriptErrorsSuppressed = true , ScrollBarsEnabled = true , AllowNavigation = false , } ; this . Controls . Add ( this . browser ) ; if ( this . infoBarDisplayed ) { this . infoBar = this . GetTextBoxControl ( Resources . BlockedImageInfoText ) ; this . Resize += this . FormResized ; this . Controls . Add ( this . infoBar ) ; } } ) ; PowerToysTelemetry . Log . WriteEvent ( new MarkdownFilePreviewed ( ) ) ; } catch ( Exception e ) { PowerToysTelemetry . Log . WriteEvent ( new MarkdownFilePreviewError { Message = e . Message } ) ; this . InvokeOnControlThread ( ( ) => { this . Controls . Clear ( ) ; this . infoBarDisplayed = true ; this . infoBar = this . GetTextBoxControl ( Resources . MarkdownNotPreviewedError ) ; this . Resize += this . FormResized ; this . Controls . Add ( this . infoBar ) ; } ) ; } finally { base . DoPreview ( dataSource ) ; } }
Start the preview on the Control.
Start the preview on the Control .
dataSource
T
Path to the file.
Path to the file .