-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Display infobar for blocked relative image path (#1695)
* Updated Parsing extension to show infobar when relative URL isblocked and updated corresponding tests * Updated Controller to display infobar when html img tag is embedded in markdown
- Loading branch information
1 parent
15cefc6
commit c2e219b
Showing
6 changed files
with
114 additions
and
132 deletions.
There are no files selected for viewing
186 changes: 86 additions & 100 deletions
186
src/modules/previewpane/MarkDownPreviewHandler/HTMLParsingExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,103 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.IO; | ||
using Markdig; | ||
using Markdig.Extensions.Figures; | ||
using Markdig.Extensions.Tables; | ||
using Markdig.Renderers; | ||
using Markdig.Renderers.Html; | ||
using Markdig.Syntax; | ||
using Markdig.Syntax.Inlines; | ||
|
||
namespace MarkdownPreviewHandler | ||
{ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.IO; | ||
using Markdig; | ||
using Markdig.Extensions.Figures; | ||
using Markdig.Extensions.Tables; | ||
using Markdig.Renderers; | ||
using Markdig.Renderers.Html; | ||
using Markdig.Syntax; | ||
using Markdig.Syntax.Inlines; | ||
|
||
namespace MarkdownPreviewHandler | ||
{ | ||
/// <summary> | ||
/// Callback if extension blocks external images. | ||
/// </summary> | ||
public delegate void ImagesBlockedCallBack(); | ||
|
||
/// <summary> | ||
/// Markdig Extension to process html nodes in markdown AST. | ||
/// </summary> | ||
public class HTMLParsingExtension : IMarkdownExtension | ||
{ | ||
/// <summary> | ||
/// Markdig Extension to process html nodes in markdown AST. | ||
/// </summary> | ||
public class HTMLParsingExtension : IMarkdownExtension | ||
{ | ||
/// <summary> | ||
/// Callback if extension blocks external images. | ||
/// </summary> | ||
private readonly ImagesBlockedCallBack imagesBlockedCallBack; | ||
|
||
/// </summary> | ||
private readonly ImagesBlockedCallBack imagesBlockedCallBack; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="HTMLParsingExtension"/> class. | ||
/// </summary> | ||
/// <param name="imagesBlockedCallBack">Callback function if image is blocked by extension.</param> | ||
/// <param name="baseUrl">Absolute path of markdown file.</param> | ||
public HTMLParsingExtension(ImagesBlockedCallBack imagesBlockedCallBack, string baseUrl = "") | ||
{ | ||
this.imagesBlockedCallBack = imagesBlockedCallBack; | ||
this.BaseUrl = baseUrl; | ||
} | ||
/// <param name="baseUrl">Absolute path of markdown file.</param> | ||
public HTMLParsingExtension(ImagesBlockedCallBack imagesBlockedCallBack, string baseUrl = "") | ||
{ | ||
this.imagesBlockedCallBack = imagesBlockedCallBack; | ||
this.BaseUrl = baseUrl; | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets path to directory containing markdown file. | ||
/// </summary> | ||
public string BaseUrl { get; set; } | ||
/// <summary> | ||
/// Gets or sets path to directory containing markdown file. | ||
/// </summary> | ||
public string BaseUrl { get; set; } | ||
|
||
/// <inheritdoc/> | ||
public void Setup(MarkdownPipelineBuilder pipeline) | ||
{ | ||
// Make sure we don't have a delegate twice | ||
pipeline.DocumentProcessed -= this.PipelineOnDocumentProcessed; | ||
pipeline.DocumentProcessed += this.PipelineOnDocumentProcessed; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) | ||
{ | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Setup(MarkdownPipelineBuilder pipeline) | ||
{ | ||
// Make sure we don't have a delegate twice | ||
pipeline.DocumentProcessed -= this.PipelineOnDocumentProcessed; | ||
pipeline.DocumentProcessed += this.PipelineOnDocumentProcessed; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Process nodes in markdown AST. | ||
/// </summary> | ||
/// <param name="document">Markdown Document.</param> | ||
public void PipelineOnDocumentProcessed(MarkdownDocument document) | ||
{ | ||
foreach (var node in document.Descendants()) | ||
/// <summary> | ||
/// Process nodes in markdown AST. | ||
/// </summary> | ||
/// <param name="document">Markdown Document.</param> | ||
public void PipelineOnDocumentProcessed(MarkdownDocument document) | ||
{ | ||
foreach (var node in document.Descendants()) | ||
{ | ||
if (node is Block) | ||
{ | ||
if (node is Table) | ||
{ | ||
node.GetAttributes().AddClass("table table-striped table-bordered"); | ||
} | ||
else if (node is QuoteBlock) | ||
{ | ||
node.GetAttributes().AddClass("blockquote"); | ||
} | ||
else if (node is Figure) | ||
{ | ||
node.GetAttributes().AddClass("figure"); | ||
} | ||
else if (node is FigureCaption) | ||
{ | ||
node.GetAttributes().AddClass("figure-caption"); | ||
} | ||
} | ||
else if (node is Inline) | ||
{ | ||
if (node is LinkInline link) | ||
{ | ||
if (node is Block) | ||
{ | ||
if (node is Table) | ||
{ | ||
node.GetAttributes().AddClass("table table-striped table-bordered"); | ||
} | ||
else if (node is QuoteBlock) | ||
{ | ||
node.GetAttributes().AddClass("blockquote"); | ||
} | ||
else if (node is Figure) | ||
{ | ||
node.GetAttributes().AddClass("figure"); | ||
} | ||
else if (node is FigureCaption) | ||
{ | ||
node.GetAttributes().AddClass("figure-caption"); | ||
} | ||
} | ||
else if (node is Inline) | ||
{ | ||
if (node is LinkInline link) | ||
{ | ||
if (link.IsImage) | ||
{ | ||
link.Url = "#"; | ||
link.GetAttributes().AddClass("img-fluid"); | ||
} | ||
|
||
if (!Uri.TryCreate(link.Url, UriKind.Absolute, out _)) | ||
{ | ||
link.Url = link.Url.TrimStart('/', '\\'); | ||
this.BaseUrl = this.BaseUrl.TrimEnd('/', '\\'); | ||
Uri uriLink = new Uri(Path.Combine(this.BaseUrl, link.Url)); | ||
link.Url = uriLink.ToString(); | ||
} | ||
else | ||
{ | ||
if (link.IsImage) | ||
{ | ||
link.Url = "#"; | ||
this.imagesBlockedCallBack(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
this.imagesBlockedCallBack(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/modules/previewpane/PreviewPaneUnitTests/HelperFiles/MarkdownWithHTMLImageTag.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
## Something | ||
<img src="./a.jpg" \> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters