diff --git a/src/Notepads/Controls/Markdown/MarkdownExtensionView.xaml.cs b/src/Notepads/Controls/Markdown/MarkdownExtensionView.xaml.cs index 9bf790ec1..29f4b0548 100644 --- a/src/Notepads/Controls/Markdown/MarkdownExtensionView.xaml.cs +++ b/src/Notepads/Controls/Markdown/MarkdownExtensionView.xaml.cs @@ -7,10 +7,12 @@ using Notepads.Controls.TextEditor; using Notepads.Extensions; using Notepads.Services; + using Windows.Storage; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; using Microsoft.Toolkit.Uwp.UI; + using Windows.UI.Xaml.Media.Imaging; public sealed partial class MarkdownExtensionView : UserControl, IContentPreviewExtension { @@ -18,6 +20,10 @@ public sealed partial class MarkdownExtensionView : UserControl, IContentPreview private bool _isExtensionEnabled; + private string _parentPath; + + private readonly char[] ignoredChars = { '?', '#', ' ' }; + public bool IsExtensionEnabled { get => _isExtensionEnabled; @@ -78,18 +84,40 @@ private async void MarkdownTextBlock_ImageResolving(object sender, ImageResolvin try { - var imageUri = new Uri(e.Url); - if (Path.GetExtension(imageUri.AbsolutePath)?.ToLowerInvariant() == ".svg") + try { - // SvgImageSource is not working properly when width and height are not set in uri - // I am disabling Svg parsing here. - // e.Image = await GetImageAsync(e.Url); - e.Handled = true; + var imageUri = new Uri(e.Url); + if (Path.GetExtension(imageUri.AbsolutePath)?.ToLowerInvariant() == ".svg") + { + // SvgImageSource is not working properly when width and height are not set in uri + // I am disabling Svg parsing here. + // e.Image = await GetImageAsync(e.Url); + e.Handled = true; + } + else + { + e.Image = await GetImageAsync(e.Url); + e.Handled = true; + } } - else + catch (Exception) { - e.Image = await GetImageAsync(e.Url); - e.Handled = true; + var ignoreIndex = e.Url.IndexOfAny(ignoredChars); + var imagePath = _parentPath + (ignoreIndex > -1 + ? e.Url.Remove(ignoreIndex).Replace('/', Path.DirectorySeparatorChar) + : e.Url.Replace('/', Path.DirectorySeparatorChar)); + if (Path.GetExtension(imagePath)?.ToLowerInvariant() == ".svg") + { + // SvgImageSource is not working properly when width and height are not set in uri + // I am disabling Svg parsing here. + // e.Image = await GetImageAsync(e.Url); + e.Handled = true; + } + else + { + e.Image = await GetLocalImageAsync(imagePath); + e.Handled = true; + } } } catch (Exception ex) @@ -133,7 +161,28 @@ private async Task GetImageAsync(string url) //} } - public void Bind(TextEditorCore editorCore) + private async Task GetLocalImageAsync(string imagePath) + { + var imageFile = await StorageFile.GetFileFromPathAsync(imagePath); + + using (var ms = await imageFile.OpenReadAsync()) + { + if (Path.GetExtension(imagePath)?.ToLowerInvariant() == ".svg") + { + var image = new SvgImageSource(); + await image.SetSourceAsync(ms); + return image; + } + else + { + var image = new BitmapImage(); + await image.SetSourceAsync(ms); + return image; + } + } + } + + public void Bind(TextEditorCore editorCore, string parentPath) { if (_editorCore != null) { @@ -146,6 +195,8 @@ public void Bind(TextEditorCore editorCore) _editorCore.TextChanged += OnTextChanged; _editorCore.TextWrappingChanged += OnTextWrappingChanged; _editorCore.FontSizeChanged += OnFontSizeChanged; + + _parentPath = parentPath; } private void OnTextChanged(object sender, RoutedEventArgs e) @@ -213,8 +264,16 @@ private async void MarkdownTextBlock_OnLinkClicked(object sender, LinkClickedEve try { - var uri = new Uri(e.Link); - await Windows.System.Launcher.LaunchUriAsync(uri); + try + { + var uri = new Uri(e.Link); + await Windows.System.Launcher.LaunchUriAsync(uri); + } + catch (Exception) + { + var file = await StorageFile.GetFileFromPathAsync(_parentPath + e.Link.Replace('/', Path.DirectorySeparatorChar)); + await Windows.System.Launcher.LaunchFileAsync(file); + } } catch (Exception ex) { diff --git a/src/Notepads/Controls/TextEditor/TextEditor.xaml.cs b/src/Notepads/Controls/TextEditor/TextEditor.xaml.cs index 86ad23546..e0368d276 100644 --- a/src/Notepads/Controls/TextEditor/TextEditor.xaml.cs +++ b/src/Notepads/Controls/TextEditor/TextEditor.xaml.cs @@ -613,7 +613,7 @@ public void ShowHideContentPreview() { _contentPreviewExtension = ExtensionProvider?.GetContentPreviewExtension(FileType); if (_contentPreviewExtension == null) return; - _contentPreviewExtension.Bind(TextEditorCore); + _contentPreviewExtension.Bind(TextEditorCore, System.IO.Path.GetDirectoryName(EditingFilePath) + System.IO.Path.DirectorySeparatorChar); } if (SplitPanel == null) LoadSplitView(); diff --git a/src/Notepads/Extensions/IContentPreviewExtension.cs b/src/Notepads/Extensions/IContentPreviewExtension.cs index 86b68ec4a..c56d60d65 100644 --- a/src/Notepads/Extensions/IContentPreviewExtension.cs +++ b/src/Notepads/Extensions/IContentPreviewExtension.cs @@ -4,7 +4,7 @@ public interface IContentPreviewExtension { - void Bind(TextEditorCore editor); + void Bind(TextEditorCore editor, string parentPath); bool IsExtensionEnabled { get; set; } diff --git a/src/Notepads/Package.appxmanifest b/src/Notepads/Package.appxmanifest index 818b79ad5..ed34b844f 100644 --- a/src/Notepads/Package.appxmanifest +++ b/src/Notepads/Package.appxmanifest @@ -1181,5 +1181,6 @@ +