Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move markdown parsing logic outside control thread #2099

Merged
merged 3 commits into from
Apr 15, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Common;
Expand Down Expand Up @@ -73,28 +70,29 @@ public MarkdownPreviewHandlerControl()
/// <param name="dataSource">Path to the file.</param>
public override void DoPreview<T>(T dataSource)
{
this.InvokeOnControlThread(() =>
if (!(dataSource is string filePath))
veleek marked this conversation as resolved.
Show resolved Hide resolved
{
try
{
this.infoBarDisplayed = false;
throw new ArgumentException($"{nameof(dataSource)} for {nameof(MarkdownPreviewHandler)} must be a string but was a '{typeof(T)}'");
}

StringBuilder sb = new StringBuilder();
string filePath = dataSource as string;
string fileText = File.ReadAllText(filePath);
this.extension.BaseUrl = Path.GetDirectoryName(filePath);
this.infoBarDisplayed = false;

Regex rgx = new Regex(@"<[ ]*img.*>");
if (rgx.IsMatch(fileText))
{
this.infoBarDisplayed = true;
}
try
{
string fileText = File.ReadAllText(filePath);
Regex imageTagRegex = new Regex(@"<[ ]*img.*>");
if (imageTagRegex.IsMatch(fileText))
{
this.infoBarDisplayed = true;
}

MarkdownPipeline pipeline = this.pipelineBuilder.Build();
string parsedMarkdown = Markdown.ToHtml(fileText, pipeline);
sb.AppendFormat("{0}{1}{2}", this.htmlHeader, parsedMarkdown, this.htmlFooter);
string markdownHTML = sb.ToString();
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,
Expand All @@ -109,24 +107,30 @@ public override void DoPreview<T>(T dataSource)
if (this.infoBarDisplayed)
{
this.infoBar = this.GetTextBoxControl(Resources.BlockedImageInfoText);
this.Resize += this.FormResized;
this.Controls.Add(this.infoBar);
}
});

this.Resize += this.FormResized;
base.DoPreview(dataSource);
MarkdownTelemetry.Log.MarkdownFilePreviewed();
}
catch (Exception e)
MarkdownTelemetry.Log.MarkdownFilePreviewed();
}
catch (Exception e)
{
MarkdownTelemetry.Log.MarkdownFilePreviewError(e.Message);

this.InvokeOnControlThread(() =>
{
MarkdownTelemetry.Log.MarkdownFilePreviewError(e.Message);
this.Controls.Clear();
this.infoBarDisplayed = true;
this.infoBar = this.GetTextBoxControl(Resources.MarkdownNotPreviewedError);
this.Resize += this.FormResized;
this.Controls.Clear();
this.Controls.Add(this.infoBar);
base.DoPreview(dataSource);
}
});
});
}
finally
{
base.DoPreview(dataSource);
}
}

/// <summary>
Expand Down