Move markdown parsing logic outside control thread (#2099)

* Move markdown parsing logic outside control thread

* Update MarkdownPreviewHandlerControl.cs

* Remove trailing whitespace.

That'll teach me for trying to make an edit from the GitHub page.
This commit is contained in:
Ben Randall
2020-04-15 13:10:55 -07:00
committed by GitHub
parent 2077cd4864
commit 67e6688e69

View File

@@ -3,11 +3,8 @@
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
using System; using System;
using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Windows.Forms; using System.Windows.Forms;
using Common; using Common;
@@ -72,29 +69,30 @@ namespace MarkdownPreviewHandler
/// </summary> /// </summary>
/// <param name="dataSource">Path to the file.</param> /// <param name="dataSource">Path to the file.</param>
public override void DoPreview<T>(T dataSource) public override void DoPreview<T>(T dataSource)
{
this.InvokeOnControlThread(() =>
{
try
{ {
this.infoBarDisplayed = false; this.infoBarDisplayed = false;
StringBuilder sb = new StringBuilder(); try
string filePath = dataSource as string; {
string fileText = File.ReadAllText(filePath); if (!(dataSource is string filePath))
this.extension.BaseUrl = Path.GetDirectoryName(filePath); {
throw new ArgumentException($"{nameof(dataSource)} for {nameof(MarkdownPreviewHandler)} must be a string but was a '{typeof(T)}'");
}
Regex rgx = new Regex(@"<[ ]*img.*>"); string fileText = File.ReadAllText(filePath);
if (rgx.IsMatch(fileText)) Regex imageTagRegex = new Regex(@"<[ ]*img.*>");
if (imageTagRegex.IsMatch(fileText))
{ {
this.infoBarDisplayed = true; this.infoBarDisplayed = true;
} }
this.extension.BaseUrl = Path.GetDirectoryName(filePath);
MarkdownPipeline pipeline = this.pipelineBuilder.Build(); MarkdownPipeline pipeline = this.pipelineBuilder.Build();
string parsedMarkdown = Markdown.ToHtml(fileText, pipeline); string parsedMarkdown = Markdown.ToHtml(fileText, pipeline);
sb.AppendFormat("{0}{1}{2}", this.htmlHeader, parsedMarkdown, this.htmlFooter); string markdownHTML = $"{this.htmlHeader}{parsedMarkdown}{this.htmlFooter}";
string markdownHTML = sb.ToString();
this.InvokeOnControlThread(() =>
{
this.browser = new WebBrowserExt this.browser = new WebBrowserExt
{ {
DocumentText = markdownHTML, DocumentText = markdownHTML,
@@ -109,24 +107,30 @@ namespace MarkdownPreviewHandler
if (this.infoBarDisplayed) if (this.infoBarDisplayed)
{ {
this.infoBar = this.GetTextBoxControl(Resources.BlockedImageInfoText); this.infoBar = this.GetTextBoxControl(Resources.BlockedImageInfoText);
this.Resize += this.FormResized;
this.Controls.Add(this.infoBar); this.Controls.Add(this.infoBar);
} }
});
this.Resize += this.FormResized;
base.DoPreview(dataSource);
MarkdownTelemetry.Log.MarkdownFilePreviewed(); MarkdownTelemetry.Log.MarkdownFilePreviewed();
} }
catch (Exception e) catch (Exception e)
{ {
MarkdownTelemetry.Log.MarkdownFilePreviewError(e.Message); MarkdownTelemetry.Log.MarkdownFilePreviewError(e.Message);
this.InvokeOnControlThread(() =>
{
this.Controls.Clear();
this.infoBarDisplayed = true; this.infoBarDisplayed = true;
this.infoBar = this.GetTextBoxControl(Resources.MarkdownNotPreviewedError); this.infoBar = this.GetTextBoxControl(Resources.MarkdownNotPreviewedError);
this.Resize += this.FormResized; this.Resize += this.FormResized;
this.Controls.Clear();
this.Controls.Add(this.infoBar); this.Controls.Add(this.infoBar);
});
}
finally
{
base.DoPreview(dataSource); base.DoPreview(dataSource);
} }
});
} }
/// <summary> /// <summary>