A Swiss Army knife for developers.
C# blazorcross-platformcsharpdesktopdesktop-appdesktop-applicationdeveloper-toolsextensiblefluentfluent-designlinuxmacosmicasyntax-highlightingtypescriptwindowswindows-10windows-11winui
Ported Markdown Preview to DevToys 2.0 (#991)
Etienne BAUDOUXa7f43ff77257
diff --git a/src/Directory.Packages.props b/src/Directory.Packages.propsindex 8cd8e91ee..9b7276843 100644--- a/src/Directory.Packages.props+++ b/src/Directory.Packages.props@@ -16,6 +16,8 @@<PackageVersion Include="GirCore.Adw-1" Version="$(GirCore)" /><PackageVersion Include="GirCore.Gtk-4.0" Version="$(GirCore)" /><PackageVersion Include="GirCore.WebKit-6.0" Version="$(GirCore)" />+ <PackageVersion Include="Markdig" Version="0.33.0" />+ <PackageVersion Include="Markdown.ColorCode" Version="2.1.0" /><PackageVersion Include="Microsoft.AspNetCore.Components.Web" Version="$(DotNetVersion)" /><PackageVersion Include="Microsoft.AspNetCore.Components.WebView" Version="$(DotNetVersion)" /><PackageVersion Include="Microsoft.AspNetCore.Components.WebView.Wpf" Version="7.0.86" />diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIWebView.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIWebView.csnew file mode 100644index 000000000..61c5099df--- /dev/null+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIWebView.cs@@ -0,0 +1,76 @@+using OneOf;++namespace DevToys.Api;++/// <summary>+/// A component that represents a web view.+/// </summary>+public interface IUIWebView : IUITitledElement+{+ /// <summary>+ /// The HTML content to display, or URI to the page to display.+ /// </summary>+ OneOf<string, Uri>? Source { get; }++ /// <summary>+ /// Raised when <see cref="Source"/> is changed.+ /// </summary>+ event EventHandler? SourceChanged;+}++[DebuggerDisplay($"Id = {{{nameof(Id)}}}, Source = {{{nameof(Source)}}}")]+internal sealed class UIWebView : UITitledElement, IUIWebView+{+ private OneOf<string, Uri>? _source = default;++ internal UIWebView(string? id)+ : base(id)+ {+ }++ public OneOf<string, Uri>? Source+ {+ get => _source;+ internal set => SetPropertyValue(ref _source, value, SourceChanged);+ }++ public event EventHandler? SourceChanged;+}++public static partial class GUI+{+ /// <summary>+ /// A component that displays a web page.+ /// </summary>+ public static IUIWebView WebView()+ {+ return WebView(id: null);+ }++ /// <summary>+ /// A component that displays a web page.+ /// </summary>+ /// <param name="id">An optional unique identifier for this UI element.</param>+ public static IUIWebView WebView(string? id)+ {+ return new UIWebView(id);+ }++ /// <summary>+ /// Sets the <see cref="IUIWebView.Source"/> from an HTML document.+ /// </summary>+ public static IUIWebView RenderHTML(this IUIWebView element, string html)+ {+ ((UIWebView)element).Source = html;+ return element;+ }++ /// <summary>+ /// Sets the <see cref="IUIWebView.Source"/> from a <see cref="Uri"/>.+ /// </summary>+ public static IUIWebView NavigateToUri(this IUIWebView element, Uri uri)+ {+ ((UIWebView)element).Source = uri;+ return element;+ }+}diff --git a/src/app/dev/DevToys.Blazor/Components/UIElements/UIElementPresenter.razor b/src/app/dev/DevToys.Blazor/Components/UIElements/UIElementPresenter.razorindex 0a4d777a5..ef79011d9 100644--- a/src/app/dev/DevToys.Blazor/Components/UIElements/UIElementPresenter.razor+++ b/src/app/dev/DevToys.Blazor/Components/UIElements/UIElementPresenter.razor@@ -94,6 +94,10 @@<UIImageViewerPresenter UIImageViewer="@imageViewer" />break;+ case IUIWebView webView:+ <UIWebViewPresenter UIWebView="@webView" />+ break;+default:throw new NotSupportedException($"Gui Tool component of type '{UIElement.GetType().FullName}' isn't supported.");}diff --git a/src/app/dev/DevToys.Blazor/Components/UIElements/UIWebViewPresenter.razor b/src/app/dev/DevToys.Blazor/Components/UIElements/UIWebViewPresenter.razornew file mode 100644index 000000000..10336aa6e--- /dev/null+++ b/src/app/dev/DevToys.Blazor/Components/UIElements/UIWebViewPresenter.razor@@ -0,0 +1,40 @@+@namespace DevToys.Blazor.Components.UIElements+@using DevToys.Api;+@inherits JSStyledComponentBase++<CascadingValue Name="ParentIsEnabled" Value="@IsActuallyEnabled">++ <Grid id="@UIWebView.Id"+ Class="ui-web-view"+ IsVisible="@UIWebView.IsVisible"+ IsEnabled="@UIWebView.IsEnabled"+ HorizontalAlignment="@UIWebView.HorizontalAlignment"+ VerticalAlignment="@UIWebView.VerticalAlignment"+ RowSpacing="6"+ Rows="@(new List<UIGridLength>+ {+ UIGridLength.Auto,+ new UIGridLength(1, UIGridUnitType.Fraction)+ })"+ ColumnSpacing="6"+ Columns="@(new List<UIGridLength>+ {+ new UIGridLength(1, UIGridUnitType.Fraction)+ })">+ <GridCell Row="0" Column="0" Class="ui-web-view-title">+ <TextBlock id="@(UIWebView.Id + "-title")"+ Text="@UIWebView.Title"+ NoWrap="true"+ CanTrim="true" />+ </GridCell>+ <GridCell Row="1" Column="0">+ <Container>+ <iframe @ref="@_iframeElement"+ id="@(UIWebView.Id + "-iframe")"+ sandbox="allow-scripts allow-same-origin"+ class="ui-web-view-frame" />+ </Container>+ </GridCell>+ </Grid>++</CascadingValue>\ No newline at end of filediff --git a/src/app/dev/DevToys.Blazor/Components/UIElements/UIWebViewPresenter.razor.cs b/src/app/dev/DevToys.Blazor/Components/UIElements/UIWebViewPresenter.razor.csnew file mode 100644index 000000000..8d24d56e0--- /dev/null+++ b/src/app/dev/DevToys.Blazor/Components/UIElements/UIWebViewPresenter.razor.cs@@ -0,0 +1,56 @@+namespace DevToys.Blazor.Components.UIElements;++public partial class UIWebViewPresenter : JSStyledComponentBase+{+ protected override string? JavaScriptFile => "./_content/DevToys.Blazor/Components/UIElements/UIWebViewPresenter.razor.js";++ [Parameter]+ public IUIWebView UIWebView { get; set; } = default!;++ private ElementReference _iframeElement;++ protected override void OnInitialized()+ {+ base.OnInitialized();+ UIWebView.SourceChanged += UIWebView_SourceChanged;+ }++ public override async ValueTask DisposeAsync()+ {+ UIWebView.SourceChanged -= UIWebView_SourceChanged;+ await base.DisposeAsync();+ }++ protected override async Task OnAfterRenderAsync(bool firstRender)+ {+ await base.OnAfterRenderAsync(firstRender);++ if (firstRender)+ {+ await UpdateWebViewContentAsync();+ }+ }++ private void UIWebView_SourceChanged(object? sender, EventArgs e)+ {+ UpdateWebViewContentAsync().Forget();+ }++ private async Task UpdateWebViewContentAsync()+ {+ using (await Semaphore.WaitAsync(CancellationToken.None))+ {+ if (UIWebView.Source.HasValue)+ {+ if (UIWebView.Source.Value.TryPickT0(out string html, out Uri uri))+ {+ await (await JSModule).InvokeVoidWithErrorHandlingAsync("webViewNavigateToHtml", _iframeElement, html);+ }+ else+ {+ await (await JSModule).InvokeVoidWithErrorHandlingAsync("webViewNavigateToUri", _iframeElement, uri.ToString());+ }+ }+ }+ }+}diff --git a/src/app/dev/DevToys.Blazor/Components/UIElements/UIWebViewPresenter.razor.js b/src/app/dev/DevToys.Blazor/Components/UIElements/UIWebViewPresenter.razor.jsnew file mode 100644index 000000000..dc0198282--- /dev/null+++ b/src/app/dev/DevToys.Blazor/Components/UIElements/UIWebViewPresenter.razor.js@@ -0,0 +1,8 @@+export function webViewNavigateToUri(iframe, uri) {+ iframe.src = uri;+}+export function webViewNavigateToHtml(iframe, html) {+ const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;+ iframeDocument.body.innerHTML = html;+}+//# sourceMappingURL=UIWebViewPresenter.razor.js.map\ No newline at end of filediff --git a/src/app/dev/DevToys.Blazor/Components/UIElements/UIWebViewPresenter.razor.js.map b/src/app/dev/DevToys.Blazor/Components/UIElements/UIWebViewPresenter.razor.js.mapnew file mode 100644index 000000000..385d0da01--- /dev/null+++ b/src/app/dev/DevToys.Blazor/Components/UIElements/UIWebViewPresenter.razor.js.map@@ -0,0 +1 @@+{"version":3,"file":"UIWebViewPresenter.razor.js","sourceRoot":"","sources":["UIWebViewPresenter.razor.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,oBAAoB,CAAC,MAAyB,EAAE,GAAW;IACvE,MAAM,CAAC,GAAG,GAAG,GAAG,CAAC;AACrB,CAAC;AAED,MAAM,UAAU,qBAAqB,CAAC,MAAyB,EAAE,IAAY;IACzE,MAAM,cAAc,GAAG,MAAM,CAAC,eAAe,IAAI,MAAM,CAAC,aAAa,CAAC,QAAQ,CAAC;IAC/E,cAAc,CAAC,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;AACzC,CAAC"}\ No newline at end of filediff --git a/src/app/dev/DevToys.Blazor/Components/UIElements/UIWebViewPresenter.razor.scss b/src/app/dev/DevToys.Blazor/Components/UIElements/UIWebViewPresenter.razor.scssnew file mode 100644index 000000000..447b3cce9--- /dev/null+++ b/src/app/dev/DevToys.Blazor/Components/UIElements/UIWebViewPresenter.razor.scss@@ -0,0 +1,33 @@+@use "../../Assets/sass/devtoys" as *;++.ui-web-view {+ &-title {+ height: 20px !important;+ margin-top: 14px;+ }++ &-frame {+ @include flex($direction: row, $align: center, $justify: between, $gap: 12px);+ @include typography-body;+ color: var(--card-foreground);+ border: var(--card-border-thickness);+ border-color: var(--card-border);+ border-radius: var(--control-corner-radius);+ background-clip: padding-box;+ background-color: var(--card-background-color);+ min-height: 72px;+ width: 100%;+ height: 100%;+ pointer-events: all;+ }+}++// Compact mode+div[data-compactmode] {+ .ui-web-view {+ &-title {+ height: 20px !important;+ margin-top: 7px;+ }+ }+}diff --git a/src/app/dev/DevToys.Blazor/Components/UIElements/UIWebViewPresenter.razor.ts b/src/app/dev/DevToys.Blazor/Components/UIElements/UIWebViewPresenter.razor.tsnew file mode 100644index 000000000..96c5caafa--- /dev/null+++ b/src/app/dev/DevToys.Blazor/Components/UIElements/UIWebViewPresenter.razor.ts@@ -0,0 +1,8 @@+export function webViewNavigateToUri(iframe: HTMLIFrameElement, uri: string): void {+ iframe.src = uri;+}++export function webViewNavigateToHtml(iframe: HTMLIFrameElement, html: string): void {+ const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;+ iframeDocument.body.innerHTML = html;+}\ No newline at end of filediff --git a/src/app/dev/DevToys.Tools/Assets/GitHubMarkdown/index.html b/src/app/dev/DevToys.Tools/Assets/GitHubMarkdown/index.htmlnew file mode 100644index 000000000..a39ac55ae--- /dev/null+++ b/src/app/dev/DevToys.Tools/Assets/GitHubMarkdown/index.html@@ -0,0 +1,1021 @@+<!doctype html>+<html>+<head>+ <meta charset="utf-8">+ <meta name="viewport" content="width=device-width, initial-scale=1, minimal-ui">+ <title>DevToys Markdown Preview</title>+ <style>+ div[data-theme='dark'] {+ color-scheme: dark;+ --color-prettylights-syntax-comment: #8b949e;+ --color-prettylights-syntax-constant: #79c0ff;+ --color-prettylights-syntax-entity: #d2a8ff;+ --color-prettylights-syntax-storage-modifier-import: #c9d1d9;+ --color-prettylights-syntax-entity-tag: #7ee787;+ --color-prettylights-syntax-keyword: #ff7b72;+ --color-prettylights-syntax-string: #a5d6ff;+ --color-prettylights-syntax-variable: #ffa657;+ --color-prettylights-syntax-brackethighlighter-unmatched: #f85149;+ --color-prettylights-syntax-invalid-illegal-text: #f0f6fc;+ --color-prettylights-syntax-invalid-illegal-bg: #8e1519;+ --color-prettylights-syntax-carriage-return-text: #f0f6fc;+ --color-prettylights-syntax-carriage-return-bg: #b62324;+ --color-prettylights-syntax-string-regexp: #7ee787;+ --color-prettylights-syntax-markup-list: #f2cc60;+ --color-prettylights-syntax-markup-heading: #1f6feb;+ --color-prettylights-syntax-markup-italic: #c9d1d9;+ --color-prettylights-syntax-markup-bold: #c9d1d9;+ --color-prettylights-syntax-markup-deleted-text: #ffdcd7;+ --color-prettylights-syntax-markup-deleted-bg: #67060c;+ --color-prettylights-syntax-markup-inserted-text: #aff5b4;+ --color-prettylights-syntax-markup-inserted-bg: #033a16;+ --color-prettylights-syntax-markup-changed-text: #ffdfb6;+ --color-prettylights-syntax-markup-changed-bg: #5a1e02;+ --color-prettylights-syntax-markup-ignored-text: #c9d1d9;+ --color-prettylights-syntax-markup-ignored-bg: #1158c7;+ --color-prettylights-syntax-meta-diff-range: #d2a8ff;+ --color-prettylights-syntax-brackethighlighter-angle: #8b949e;+ --color-prettylights-syntax-sublimelinter-gutter-mark: #484f58;+ --color-prettylights-syntax-constant-other-reference-link: #a5d6ff;+ --color-fg-default: #c9d1d9;+ --color-fg-muted: #8b949e;+ --color-fg-subtle: #484f58;+ --color-canvas-default: #0d1117;+ --color-canvas-subtle: #161b22;+ --color-border-default: #30363d;+ --color-border-muted: #21262d;+ --color-neutral-muted: rgba(110,118,129,0.4);+ --color-accent-fg: #58a6ff;+ --color-accent-emphasis: #1f6feb;+ --color-danger-fg: #f85149;+ }++ div[data-theme='light'] {+ color-scheme: light;+ --color-prettylights-syntax-comment: #6e7781;+ --color-prettylights-syntax-constant: #0550ae;+ --color-prettylights-syntax-entity: #8250df;+ --color-prettylights-syntax-storage-modifier-import: #24292f;+ --color-prettylights-syntax-entity-tag: #116329;+ --color-prettylights-syntax-keyword: #cf222e;+ --color-prettylights-syntax-string: #0a3069;+ --color-prettylights-syntax-variable: #953800;+ --color-prettylights-syntax-brackethighlighter-unmatched: #82071e;+ --color-prettylights-syntax-invalid-illegal-text: #f6f8fa;+ --color-prettylights-syntax-invalid-illegal-bg: #82071e;+ --color-prettylights-syntax-carriage-return-text: #f6f8fa;+ --color-prettylights-syntax-carriage-return-bg: #cf222e;+ --color-prettylights-syntax-string-regexp: #116329;+ --color-prettylights-syntax-markup-list: #3b2300;+ --color-prettylights-syntax-markup-heading: #0550ae;+ --color-prettylights-syntax-markup-italic: #24292f;+ --color-prettylights-syntax-markup-bold: #24292f;+ --color-prettylights-syntax-markup-deleted-text: #82071e;+ --color-prettylights-syntax-markup-deleted-bg: #FFEBE9;+ --color-prettylights-syntax-markup-inserted-text: #116329;+ --color-prettylights-syntax-markup-inserted-bg: #dafbe1;+ --color-prettylights-syntax-markup-changed-text: #953800;+ --color-prettylights-syntax-markup-changed-bg: #ffd8b5;+ --color-prettylights-syntax-markup-ignored-text: #eaeef2;+ --color-prettylights-syntax-markup-ignored-bg: #0550ae;+ --color-prettylights-syntax-meta-diff-range: #8250df;+ --color-prettylights-syntax-brackethighlighter-angle: #57606a;+ --color-prettylights-syntax-sublimelinter-gutter-mark: #8c959f;+ --color-prettylights-syntax-constant-other-reference-link: #0a3069;+ --color-fg-default: #24292f;+ --color-fg-muted: #57606a;+ --color-fg-subtle: #6e7781;+ --color-canvas-default: #ffffff;+ --color-canvas-subtle: #f6f8fa;+ --color-border-default: #d0d7de;+ --color-border-muted: hsla(210,18%,87%,1);+ --color-neutral-muted: rgba(175,184,193,0.2);+ --color-accent-fg: #0969da;+ --color-accent-emphasis: #0969da;+ --color-danger-fg: #cf222e;+ }++ body {+ box-sizing: border-box;+ min-width: 200px;+ margin: 0 auto;+ }++ .root {+ min-height: 100%;+ color: var(--color-fg-default);+ background-color: var(--color-canvas-default);+ }++ .markdown-body {+ -ms-text-size-adjust: 100%;+ -webkit-text-size-adjust: 100%;+ margin: 0;+ font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji";+ font-size: 16px;+ line-height: 1.5;+ word-wrap: break-word;+ padding: 16px;+ }++ .markdown-body .octicon {+ display: inline-block;+ fill: currentColor;+ vertical-align: text-bottom;+ }++ .markdown-body h1:hover .anchor .octicon-link:before,+ .markdown-body h2:hover .anchor .octicon-link:before,+ .markdown-body h3:hover .anchor .octicon-link:before,+ .markdown-body h4:hover .anchor .octicon-link:before,+ .markdown-body h5:hover .anchor .octicon-link:before,+ .markdown-body h6:hover .anchor .octicon-link:before {+ width: 16px;+ height: 16px;+ content: ' ';+ display: inline-block;+ background-color: currentColor;+ -webkit-mask-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' version='1.1' aria-hidden='true'><path fill-rule='evenodd' d='M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z'></path></svg>");+ mask-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' version='1.1' aria-hidden='true'><path fill-rule='evenodd' d='M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z'></path></svg>");+ }++ .markdown-body details,+ .markdown-body figcaption,+ .markdown-body figure {+ display: block;+ }++ .markdown-body summary {+ display: list-item;+ }++ .markdown-body a {+ background-color: transparent;+ color: var(--color-accent-fg);+ text-decoration: none;+ }++ .markdown-body a:active,+ .markdown-body a:hover {+ outline-width: 0;+ }++ .markdown-body abbr[title] {+ border-bottom: none;+ -webkit-text-decoration: underline dotted;+ text-decoration: underline dotted;+ }++ .markdown-body b,+ .markdown-body strong {+ font-weight: 600;+ }++ .markdown-body dfn {+ font-style: italic;+ }++ .markdown-body h1 {+ margin: .67em 0;+ font-weight: 600;+ padding-bottom: .3em;+ font-size: 2em;+ border-bottom: 1px solid var(--color-border-muted);+ }++ .markdown-body mark {+ background-color: #ff0;+ color: var(--color-text-primary);+ }++ .markdown-body small {+ font-size: 90%;+ }++ .markdown-body sub,+ .markdown-body sup {+ font-size: 75%;+ line-height: 0;+ position: relative;+ vertical-align: baseline;+ }++ .markdown-body sub {+ bottom: -0.25em;+ }++ .markdown-body sup {+ top: -0.5em;+ }++ .markdown-body img {+ border-style: none;+ max-width: 100%;+ box-sizing: content-box;+ background-color: var(--color-canvas-default);+ }++ .markdown-body code,+ .markdown-body kbd,+ .markdown-body pre,+ .markdown-body samp {+ font-family: monospace,monospace;+ font-size: 1em;+ }++ .markdown-body figure {+ margin: 1em 40px;+ }++ .markdown-body hr {+ box-sizing: content-box;+ overflow: hidden;+ background: transparent;+ border-bottom: 1px solid var(--color-border-muted);+ height: .25em;+ padding: 0;+ margin: 24px 0;+ background-color: var(--color-border-default);+ border: 0;+ }++ .markdown-body html [type=button],+ .markdown-body [type=reset],+ .markdown-body [type=submit] {+ -webkit-appearance: button;+ }++ .markdown-body [type=button]::-moz-focus-inner,+ .markdown-body [type=reset]::-moz-focus-inner,+ .markdown-body [type=submit]::-moz-focus-inner {+ border-style: none;+ padding: 0;+ }++ .markdown-body [type=button]:-moz-focusring,+ .markdown-body [type=reset]:-moz-focusring,+ .markdown-body [type=submit]:-moz-focusring {+ outline: 1px dotted ButtonText;+ }++ .markdown-body [type=checkbox],+ .markdown-body [type=radio] {+ box-sizing: border-box;+ padding: 0;+ }++ .markdown-body [type=number]::-webkit-inner-spin-button,+ .markdown-body [type=number]::-webkit-outer-spin-button {+ height: auto;+ }++ .markdown-body [type=search] {+ -webkit-appearance: textfield;+ outline-offset: -2px;+ }++ .markdown-body [type=search]::-webkit-search-cancel-button,+ .markdown-body [type=search]::-webkit-search-decoration {+ -webkit-appearance: none;+ }++ .markdown-body ::-webkit-input-placeholder {+ color: inherit;+ opacity: .54;+ }++ .markdown-body ::-webkit-file-upload-button {+ -webkit-appearance: button;+ font: inherit;+ }++ .markdown-body a:hover {+ text-decoration: underline;+ }++ .markdown-body hr::before {+ display: table;+ content: "";+ }++ .markdown-body hr::after {+ display: table;+ clear: both;+ content: "";+ }++ .markdown-body table {+ border-spacing: 0;+ border-collapse: collapse;+ display: block;+ width: max-content;+ max-width: 100%;+ overflow: auto;+ }++ .markdown-body td,+ .markdown-body th {+ padding: 0;+ }++ .markdown-body details summary {+ cursor: pointer;+ }++ .markdown-body details:not([open]) > *:not(summary) {+ display: none !important;+ }++ .markdown-body kbd {+ display: inline-block;+ padding: 3px 5px;+ font: 11px ui-monospace,SFMono-Regular,SF Mono,Menlo,Consolas,Liberation Mono,monospace;+ line-height: 10px;+ color: var(--color-fg-default);+ vertical-align: middle;+ background-color: var(--color-canvas-subtle);+ border: solid 1px var(--color-neutral-muted);+ border-bottom-color: var(--color-neutral-muted);+ border-radius: 6px;+ box-shadow: inset 0 -1px 0 var(--color-neutral-muted);+ }++ .markdown-body h1,+ .markdown-body h2,+ .markdown-body h3,+ .markdown-body h4,+ .markdown-body h5,+ .markdown-body h6 {+ margin-top: 24px;+ margin-bottom: 16px;+ font-weight: 600;+ line-height: 1.25;+ }++ .markdown-body h2 {+ font-weight: 600;+ padding-bottom: .3em;+ font-size: 1.5em;+ border-bottom: 1px solid var(--color-border-muted);+ }++ .markdown-body h3 {+ font-weight: 600;+ font-size: 1.25em;+ }++ .markdown-body h4 {+ font-weight: 600;+ font-size: 1em;+ }++ .markdown-body h5 {+ font-weight: 600;+ font-size: .875em;+ }++ .markdown-body h6 {+ font-weight: 600;+ font-size: .85em;+ color: var(--color-fg-muted);+ }++ .markdown-body p {+ margin-top: 0;+ margin-bottom: 10px;+ }++ .markdown-body blockquote {+ margin: 0;+ padding: 0 1em;+ color: var(--color-fg-muted);+ border-left: .25em solid var(--color-border-default);+ }++ .markdown-body ul,+ .markdown-body ol {+ margin-top: 0;+ margin-bottom: 0;+ padding-left: 2em;+ }++ .markdown-body ol ol,+ .markdown-body ul ol {+ list-style-type: lower-roman;+ }++ .markdown-body ul ul ol,+ .markdown-body ul ol ol,+ .markdown-body ol ul ol,+ .markdown-body ol ol ol {+ list-style-type: lower-alpha;+ }++ .markdown-body dd {+ margin-left: 0;+ }++ .markdown-body tt,+ .markdown-body code {+ font-family: ui-monospace,SFMono-Regular,SF Mono,Menlo,Consolas,Liberation Mono,monospace;+ font-size: 12px;+ }++ .markdown-body pre {+ margin-top: 0;+ margin-bottom: 0;+ font-family: ui-monospace,SFMono-Regular,SF Mono,Menlo,Consolas,Liberation Mono,monospace;+ font-size: 12px;+ word-wrap: normal;+ }++ .markdown-body :-ms-input-placeholder {+ color: var(--color-fg-subtle);+ opacity: 1;+ }++ .markdown-body ::-ms-input-placeholder {+ color: var(--color-fg-subtle);+ opacity: 1;+ }++ .markdown-body ::placeholder {+ color: var(--color-fg-subtle);+ opacity: 1;+ }++ .markdown-body .pl-c {+ color: var(--color-prettylights-syntax-comment);+ }++ .markdown-body .pl-c1,+ .markdown-body .pl-s .pl-v {+ color: var(--color-prettylights-syntax-constant);+ }++ .markdown-body .pl-e,+ .markdown-body .pl-en {+ color: var(--color-prettylights-syntax-entity);+ }++ .markdown-body .pl-smi,+ .markdown-body .pl-s .pl-s1 {+ color: var(--color-prettylights-syntax-storage-modifier-import);+ }++ .markdown-body .pl-ent {+ color: var(--color-prettylights-syntax-entity-tag);+ }++ .markdown-body .pl-k {+ color: var(--color-prettylights-syntax-keyword);+ }++ .markdown-body .pl-s,+ .markdown-body .pl-pds,+ .markdown-body .pl-s .pl-pse .pl-s1,+ .markdown-body .pl-sr,+ .markdown-body .pl-sr .pl-cce,+ .markdown-body .pl-sr .pl-sre,+ .markdown-body .pl-sr .pl-sra {+ color: var(--color-prettylights-syntax-string);+ }++ .markdown-body .pl-v,+ .markdown-body .pl-smw {+ color: var(--color-prettylights-syntax-variable);+ }++ .markdown-body .pl-bu {+ color: var(--color-prettylights-syntax-brackethighlighter-unmatched);+ }++ .markdown-body .pl-ii {+ color: var(--color-prettylights-syntax-invalid-illegal-text);+ background-color: var(--color-prettylights-syntax-invalid-illegal-bg);+ }++ .markdown-body .pl-c2 {+ color: var(--color-prettylights-syntax-carriage-return-text);+ background-color: var(--color-prettylights-syntax-carriage-return-bg);+ }++ .markdown-body .pl-sr .pl-cce {+ font-weight: bold;+ color: var(--color-prettylights-syntax-string-regexp);+ }++ .markdown-body .pl-ml {+ color: var(--color-prettylights-syntax-markup-list);+ }++ .markdown-body .pl-mh,+ .markdown-body .pl-mh .pl-en,+ .markdown-body .pl-ms {+ font-weight: bold;+ color: var(--color-prettylights-syntax-markup-heading);+ }++ .markdown-body .pl-mi {+ font-style: italic;+ color: var(--color-prettylights-syntax-markup-italic);+ }++ .markdown-body .pl-mb {+ font-weight: bold;+ color: var(--color-prettylights-syntax-markup-bold);+ }++ .markdown-body .pl-md {+ color: var(--color-prettylights-syntax-markup-deleted-text);+ background-color: var(--color-prettylights-syntax-markup-deleted-bg);+ }++ .markdown-body .pl-mi1 {+ color: var(--color-prettylights-syntax-markup-inserted-text);+ background-color: var(--color-prettylights-syntax-markup-inserted-bg);+ }++ .markdown-body .pl-mc {+ color: var(--color-prettylights-syntax-markup-changed-text);+ background-color: var(--color-prettylights-syntax-markup-changed-bg);+ }++ .markdown-body .pl-mi2 {+ color: var(--color-prettylights-syntax-markup-ignored-text);+ background-color: var(--color-prettylights-syntax-markup-ignored-bg);+ }++ .markdown-body .pl-mdr {+ font-weight: bold;+ color: var(--color-prettylights-syntax-meta-diff-range);+ }++ .markdown-body .pl-ba {+ color: var(--color-prettylights-syntax-brackethighlighter-angle);+ }++ .markdown-body .pl-sg {+ color: var(--color-prettylights-syntax-sublimelinter-gutter-mark);+ }++ .markdown-body .pl-corl {+ text-decoration: underline;+ color: var(--color-prettylights-syntax-constant-other-reference-link);+ }++ .markdown-body [data-catalyst] {+ display: block;+ }++ .markdown-body g-emoji {+ font-family: "Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol";+ font-size: 1em;+ font-style: normal !important;+ font-weight: 400;+ line-height: 1;+ vertical-align: -0.075em;+ }++ .markdown-body g-emoji img {+ width: 1em;+ height: 1em;+ }++ .markdown-body::before {+ display: table;+ content: "";+ }++ .markdown-body::after {+ display: table;+ clear: both;+ content: "";+ }++ .markdown-body > *:first-child {+ margin-top: 0 !important;+ }++ .markdown-body > *:last-child {+ margin-bottom: 0 !important;+ }++ .markdown-body a:not([href]) {+ color: inherit;+ text-decoration: none;+ }++ .markdown-body .absent {+ color: var(--color-danger-fg);+ }++ .markdown-body .anchor {+ float: left;+ padding-right: 4px;+ margin-left: -20px;+ line-height: 1;+ }++ .markdown-body .anchor:focus {+ outline: none;+ }++ .markdown-body p,+ .markdown-body blockquote,+ .markdown-body ul,+ .markdown-body ol,+ .markdown-body dl,+ .markdown-body table,+ .markdown-body pre,+ .markdown-body details {+ margin-top: 0;+ margin-bottom: 16px;+ }++ .markdown-body blockquote > :first-child {+ margin-top: 0;+ }++ .markdown-body blockquote > :last-child {+ margin-bottom: 0;+ }++ .markdown-body sup > a::before {+ content: "[";+ }++ .markdown-body sup > a::after {+ content: "]";+ }++ .markdown-body h1 .octicon-link,+ .markdown-body h2 .octicon-link,+ .markdown-body h3 .octicon-link,+ .markdown-body h4 .octicon-link,+ .markdown-body h5 .octicon-link,+ .markdown-body h6 .octicon-link {+ color: var(--color-fg-default);+ vertical-align: middle;+ visibility: hidden;+ }++ .markdown-body h1:hover .anchor,+ .markdown-body h2:hover .anchor,+ .markdown-body h3:hover .anchor,+ .markdown-body h4:hover .anchor,+ .markdown-body h5:hover .anchor,+ .markdown-body h6:hover .anchor {+ text-decoration: none;+ }++ .markdown-body h1:hover .anchor .octicon-link,+ .markdown-body h2:hover .anchor .octicon-link,+ .markdown-body h3:hover .anchor .octicon-link,+ .markdown-body h4:hover .anchor .octicon-link,+ .markdown-body h5:hover .anchor .octicon-link,+ .markdown-body h6:hover .anchor .octicon-link {+ visibility: visible;+ }++ .markdown-body h1 tt,+ .markdown-body h1 code,+ .markdown-body h2 tt,+ .markdown-body h2 code,+ .markdown-body h3 tt,+ .markdown-body h3 code,+ .markdown-body h4 tt,+ .markdown-body h4 code,+ .markdown-body h5 tt,+ .markdown-body h5 code,+ .markdown-body h6 tt,+ .markdown-body h6 code {+ padding: 0 .2em;+ font-size: inherit;+ }++ .markdown-body ul.no-list,+ .markdown-body ol.no-list {+ padding: 0;+ list-style-type: none;+ }++ .markdown-body ol[type="1"] {+ list-style-type: decimal;+ }++ .markdown-body ol[type=a] {+ list-style-type: lower-alpha;+ }++ .markdown-body ol[type=i] {+ list-style-type: lower-roman;+ }++ .markdown-body div > ol:not([type]) {+ list-style-type: decimal;+ }++ .markdown-body div {+ border-radius: 10px;+ }++ .markdown-body ul ul,+ .markdown-body ul ol,+ .markdown-body ol ol,+ .markdown-body ol ul {+ margin-top: 0;+ margin-bottom: 0;+ }++ .markdown-body li > p {+ margin-top: 16px;+ }++ .markdown-body li + li {+ margin-top: .25em;+ }++ .markdown-body dl {+ padding: 0;+ }++ .markdown-body dl dt {+ padding: 0;+ margin-top: 16px;+ font-size: 1em;+ font-style: italic;+ font-weight: 600;+ }++ .markdown-body dl dd {+ padding: 0 16px;+ margin-bottom: 16px;+ }++ .markdown-body table th {+ font-weight: 600;+ }++ .markdown-body table th,+ .markdown-body table td {+ padding: 6px 13px;+ border: 1px solid var(--color-border-default);+ }++ .markdown-body table tr {+ background-color: var(--color-canvas-default);+ border-top: 1px solid var(--color-border-muted);+ }++ .markdown-body table tr:nth-child(2n) {+ background-color: var(--color-canvas-subtle);+ }++ .markdown-body table img {+ background-color: transparent;+ }++ .markdown-body img[align=right] {+ padding-left: 20px;+ }++ .markdown-body img[align=left] {+ padding-right: 20px;+ }++ .markdown-body .emoji {+ max-width: none;+ vertical-align: text-top;+ background-color: transparent;+ }++ .markdown-body span.frame {+ display: block;+ overflow: hidden;+ }++ .markdown-body span.frame > span {+ display: block;+ float: left;+ width: auto;+ padding: 7px;+ margin: 13px 0 0;+ overflow: hidden;+ border: 1px solid var(--color-border-default);+ }++ .markdown-body span.frame span img {+ display: block;+ float: left;+ }++ .markdown-body span.frame span span {+ display: block;+ padding: 5px 0 0;+ clear: both;+ color: var(--color-fg-default);+ }++ .markdown-body span.align-center {+ display: block;+ overflow: hidden;+ clear: both;+ }++ .markdown-body span.align-center > span {+ display: block;+ margin: 13px auto 0;+ overflow: hidden;+ text-align: center;+ }++ .markdown-body span.align-center span img {+ margin: 0 auto;+ text-align: center;+ }++ .markdown-body span.align-right {+ display: block;+ overflow: hidden;+ clear: both;+ }++ .markdown-body span.align-right > span {+ display: block;+ margin: 13px 0 0;+ overflow: hidden;+ text-align: right;+ }++ .markdown-body span.align-right span img {+ margin: 0;+ text-align: right;+ }++ .markdown-body span.float-left {+ display: block;+ float: left;+ margin-right: 13px;+ overflow: hidden;+ }++ .markdown-body span.float-left span {+ margin: 13px 0 0;+ }++ .markdown-body span.float-right {+ display: block;+ float: right;+ margin-left: 13px;+ overflow: hidden;+ }++ .markdown-body span.float-right > span {+ display: block;+ margin: 13px auto 0;+ overflow: hidden;+ text-align: right;+ }++ .markdown-body code,+ .markdown-body tt {+ padding: .2em .4em;+ margin: 0;+ font-size: 85%;+ background-color: var(--color-neutral-muted);+ border-radius: 6px;+ }++ .markdown-body code br,+ .markdown-body tt br {+ display: none;+ }++ .markdown-body del code {+ text-decoration: inherit;+ }++ .markdown-body pre code {+ font-size: 100%;+ }++ .markdown-body pre > code {+ padding: 0;+ margin: 0;+ word-break: normal;+ white-space: pre;+ background: transparent;+ border: 0;+ }++ .markdown-body .highlight {+ margin-bottom: 16px;+ }++ .markdown-body .highlight pre {+ margin-bottom: 0;+ word-break: normal;+ }++ .markdown-body .highlight pre,+ .markdown-body pre {+ padding: 16px;+ overflow: auto;+ font-size: 85%;+ line-height: 1.45;+ background-color: var(--color-canvas-subtle);+ border-radius: 6px;+ }++ .markdown-body pre code,+ .markdown-body pre tt {+ display: inline;+ max-width: auto;+ padding: 0;+ margin: 0;+ overflow: visible;+ line-height: inherit;+ word-wrap: normal;+ background-color: transparent;+ border: 0;+ }++ .markdown-body .csv-data td,+ .markdown-body .csv-data th {+ padding: 5px;+ overflow: hidden;+ font-size: 12px;+ line-height: 1;+ text-align: left;+ white-space: nowrap;+ }++ .markdown-body .csv-data .blob-num {+ padding: 10px 8px 9px;+ text-align: right;+ background: var(--color-canvas-default);+ border: 0;+ }++ .markdown-body .csv-data tr {+ border-top: 0;+ }++ .markdown-body .csv-data th {+ font-weight: 600;+ background: var(--color-canvas-subtle);+ border-top: 0;+ }++ .markdown-body .footnotes {+ font-size: 12px;+ color: var(--color-fg-muted);+ border-top: 1px solid var(--color-border-default);+ }++ .markdown-body .footnotes ol {+ padding-left: 16px;+ }++ .markdown-body .footnotes li {+ position: relative;+ }++ .markdown-body .footnotes li:target::before {+ position: absolute;+ top: -8px;+ right: -8px;+ bottom: -8px;+ left: -24px;+ pointer-events: none;+ content: "";+ border: 2px solid var(--color-accent-emphasis);+ border-radius: 6px;+ }++ .markdown-body .footnotes li:target {+ color: var(--color-fg-default);+ }++ .markdown-body .footnotes .data-footnote-backref g-emoji {+ font-family: monospace;+ }++ .markdown-body [hidden] {+ display: none !important;+ }++ .markdown-body ::-webkit-calendar-picker-indicator {+ filter: invert(50%);+ }+ </style>+</head>+<body>+ <div class="root" data-theme="{{renderTheme}}">+ <article class="markdown-body">+ {{htmlBody}}+ </article>+ </div>+</body>+</html>diff --git a/src/app/dev/DevToys.Tools/DevToys.Tools.csproj b/src/app/dev/DevToys.Tools/DevToys.Tools.csprojindex c74d45a7e..cb1255575 100644--- a/src/app/dev/DevToys.Tools/DevToys.Tools.csproj+++ b/src/app/dev/DevToys.Tools/DevToys.Tools.csproj@@ -114,6 +114,11 @@<AutoGen>True</AutoGen><DependentUpon>TextCompare.resx</DependentUpon></Compile>+ <Compile Update="Tools\Text\MarkdownPreview\MarkdownPreview.Designer.cs">+ <DesignTime>True</DesignTime>+ <AutoGen>True</AutoGen>+ <DependentUpon>MarkdownPreview.resx</DependentUpon>+ </Compile><Compile Update="Tools\Text\RegExTester\RegExTester.Designer.cs"><DesignTime>True</DesignTime><AutoGen>True</AutoGen>@@ -194,6 +199,10 @@<Generator>ResXFileCodeGenerator</Generator><LastGenOutput>TextCompare.Designer.cs</LastGenOutput></EmbeddedResource>+ <EmbeddedResource Update="Tools\Text\MarkdownPreview\MarkdownPreview.resx">+ <Generator>ResXFileCodeGenerator</Generator>+ <LastGenOutput>MarkdownPreview.Designer.cs</LastGenOutput>+ </EmbeddedResource><EmbeddedResource Update="Tools\Text\RegExTester\RegExTester.resx"><Generator>ResXFileCodeGenerator</Generator><LastGenOutput>RegExTester.Designer.cs</LastGenOutput>@@ -206,9 +215,12 @@<ItemGroup><EmbeddedResource Include="..\..\..\..\assets\font\DevToys-Tools-Icons.ttf" Link="Assets\fonts\DevToys-Tools-Icons.ttf" />+ <EmbeddedResource Include="Assets\GitHubMarkdown\index.html" /></ItemGroup><ItemGroup>+ <PackageReference Include="Markdig" />+ <PackageReference Include="Markdown.ColorCode" /><PackageReference Include="Newtonsoft.Json" /><PackageReference Include="System.Text.Json" /><PackageReference Include="YamlDotNet" />diff --git a/src/app/dev/DevToys.Tools/Models/MarkdownPreviewTheme.cs b/src/app/dev/DevToys.Tools/Models/MarkdownPreviewTheme.csnew file mode 100644index 000000000..673756c36--- /dev/null+++ b/src/app/dev/DevToys.Tools/Models/MarkdownPreviewTheme.cs@@ -0,0 +1,6 @@+namespace DevToys.Tools.Models;+internal enum MarkdownPreviewTheme+{+ Dark,+ Light+}diff --git a/src/app/dev/DevToys.Tools/SmartDetection/MarkdownDataTypeDetector.cs b/src/app/dev/DevToys.Tools/SmartDetection/MarkdownDataTypeDetector.csnew file mode 100644index 000000000..a91ef7955--- /dev/null+++ b/src/app/dev/DevToys.Tools/SmartDetection/MarkdownDataTypeDetector.cs@@ -0,0 +1,43 @@+using System.Text.RegularExpressions;++namespace DevToys.Tools.SmartDetection;++[Export(typeof(IDataTypeDetector))]+[DataTypeName("Markdown", baseName: PredefinedCommonDataTypeNames.Text)]+internal sealed partial class MarkdownDataTypeDetector : IDataTypeDetector+{+ [GeneratedRegex("^#{1,6}\\s", RegexOptions.Compiled)]+ private static partial Regex HeaderRegex();++ [GeneratedRegex("\\*\\*.*\\*\\*", RegexOptions.Compiled)]+ private static partial Regex BoldRegex();++ [GeneratedRegex("\\*.*\\*", RegexOptions.Compiled)]+ private static partial Regex ItalicRegex();++ [GeneratedRegex("\\[.*\\]\\(.*\\)", RegexOptions.Compiled)]+ private static partial Regex LinkRegex();++ [GeneratedRegex("!\\[.*\\]\\(.*\\)", RegexOptions.Compiled)]+ private static partial Regex ImageRegex();++ public ValueTask<DataDetectionResult> TryDetectDataAsync(object data, DataDetectionResult? resultFromBaseDetector, CancellationToken cancellationToken)+ {+ if (resultFromBaseDetector is not null+ && resultFromBaseDetector.Data is string dataString+ && dataString.Length <= 10_000) // Perf: Don't try to detect Markdown in large strings as RegEx are expensive.+ {+ // Check if the input string contains any of the Markdown elements+ if (HeaderRegex().IsMatch(dataString)+ || BoldRegex().IsMatch(dataString)+ || ItalicRegex().IsMatch(dataString)+ || LinkRegex().IsMatch(dataString)+ || ImageRegex().IsMatch(dataString))+ {+ return ValueTask.FromResult(new DataDetectionResult(Success: true, Data: dataString));+ }+ }++ return ValueTask.FromResult(DataDetectionResult.Unsuccessful);+ }+}diff --git a/src/app/dev/DevToys.Tools/Tools/Text/MarkdownPreview/MarkdownPreview.Designer.cs b/src/app/dev/DevToys.Tools/Tools/Text/MarkdownPreview/MarkdownPreview.Designer.csnew file mode 100644index 000000000..d36a9de14--- /dev/null+++ b/src/app/dev/DevToys.Tools/Tools/Text/MarkdownPreview/MarkdownPreview.Designer.cs@@ -0,0 +1,171 @@+//------------------------------------------------------------------------------+// <auto-generated>+// This code was generated by a tool.+// Runtime Version:4.0.30319.42000+//+// Changes to this file may cause incorrect behavior and will be lost if+// the code is regenerated.+// </auto-generated>+//------------------------------------------------------------------------------++namespace DevToys.Tools.Tools.Text.MarkdownPreview {+ using System;+++ /// <summary>+ /// A strongly-typed resource class, for looking up localized strings, etc.+ /// </summary>+ // This class was auto-generated by the StronglyTypedResourceBuilder+ // class via a tool like ResGen or Visual Studio.+ // To add or remove a member, edit your .ResX file then rerun ResGen+ // with the /str option, or rebuild your VS project.+ [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]+ [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]+ [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]+ internal class MarkdownPreview {++ private static global::System.Resources.ResourceManager resourceMan;++ private static global::System.Globalization.CultureInfo resourceCulture;++ [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]+ internal MarkdownPreview() {+ }++ /// <summary>+ /// Returns the cached ResourceManager instance used by this class.+ /// </summary>+ [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]+ internal static global::System.Resources.ResourceManager ResourceManager {+ get {+ if (object.ReferenceEquals(resourceMan, null)) {+ global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("DevToys.Tools.Tools.Text.MarkdownPreview.MarkdownPreview", typeof(MarkdownPreview).Assembly);+ resourceMan = temp;+ }+ return resourceMan;+ }+ }++ /// <summary>+ /// Overrides the current thread's CurrentUICulture property for all+ /// resource lookups using this strongly typed resource class.+ /// </summary>+ [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]+ internal static global::System.Globalization.CultureInfo Culture {+ get {+ return resourceCulture;+ }+ set {+ resourceCulture = value;+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Markdown preview tool.+ /// </summary>+ internal static string AccessibleName {+ get {+ return ResourceManager.GetString("AccessibleName", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Configuration.+ /// </summary>+ internal static string Configuration {+ get {+ return ResourceManager.GetString("Configuration", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Dark.+ /// </summary>+ internal static string DarkTheme {+ get {+ return ResourceManager.GetString("DarkTheme", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Preview a Markdown document with a GitHub-like render.+ /// </summary>+ internal static string Description {+ get {+ return ResourceManager.GetString("Description", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Markdown.+ /// </summary>+ internal static string Input {+ get {+ return ResourceManager.GetString("Input", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Light.+ /// </summary>+ internal static string LightTheme {+ get {+ return ResourceManager.GetString("LightTheme", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Markdown Preview.+ /// </summary>+ internal static string LongDisplayTitle {+ get {+ return ResourceManager.GetString("LongDisplayTitle", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Preview.+ /// </summary>+ internal static string Output {+ get {+ return ResourceManager.GetString("Output", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to MD.+ /// </summary>+ internal static string SearchKeywords {+ get {+ return ResourceManager.GetString("SearchKeywords", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Markdown Preview.+ /// </summary>+ internal static string ShortDisplayTitle {+ get {+ return ResourceManager.GetString("ShortDisplayTitle", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Select which theme to use to preview the Markdown.+ /// </summary>+ internal static string ThemeDescription {+ get {+ return ResourceManager.GetString("ThemeDescription", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Theme.+ /// </summary>+ internal static string ThemeTitle {+ get {+ return ResourceManager.GetString("ThemeTitle", resourceCulture);+ }+ }+ }+}diff --git a/src/app/dev/DevToys.Tools/Tools/Text/MarkdownPreview/MarkdownPreview.resx b/src/app/dev/DevToys.Tools/Tools/Text/MarkdownPreview/MarkdownPreview.resxnew file mode 100644index 000000000..8869d465c--- /dev/null+++ b/src/app/dev/DevToys.Tools/Tools/Text/MarkdownPreview/MarkdownPreview.resx@@ -0,0 +1,156 @@+<?xml version="1.0" encoding="utf-8"?>+<root>+ <!--+ Microsoft ResX Schema++ Version 2.0++ The primary goals of this format is to allow a simple XML format+ that is mostly human readable. The generation and parsing of the+ various data types are done through the TypeConverter classes+ associated with the data types.++ Example:++ ... ado.net/XML headers & schema ...+ <resheader name="resmimetype">text/microsoft-resx</resheader>+ <resheader name="version">2.0</resheader>+ <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>+ <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>+ <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>+ <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>+ <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">+ <value>[base64 mime encoded serialized .NET Framework object]</value>+ </data>+ <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">+ <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>+ <comment>This is a comment</comment>+ </data>++ There are any number of "resheader" rows that contain simple+ name/value pairs.++ Each data row contains a name, and value. The row also contains a+ type or mimetype. Type corresponds to a .NET class that support+ text/value conversion through the TypeConverter architecture.+ Classes that don't support this are serialized and stored with the+ mimetype set.++ The mimetype is used for serialized objects, and tells the+ ResXResourceReader how to depersist the object. This is currently not+ extensible. For a given mimetype the value must be set accordingly:++ Note - application/x-microsoft.net.object.binary.base64 is the format+ that the ResXResourceWriter will generate, however the reader can+ read any of the formats listed below.++ mimetype: application/x-microsoft.net.object.binary.base64+ value : The object must be serialized with+ : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter+ : and then encoded with base64 encoding.++ mimetype: application/x-microsoft.net.object.soap.base64+ value : The object must be serialized with+ : System.Runtime.Serialization.Formatters.Soap.SoapFormatter+ : and then encoded with base64 encoding.++ mimetype: application/x-microsoft.net.object.bytearray.base64+ value : The object must be serialized into a byte array+ : using a System.ComponentModel.TypeConverter+ : and then encoded with base64 encoding.+ -->+ <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">+ <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />+ <xsd:element name="root" msdata:IsDataSet="true">+ <xsd:complexType>+ <xsd:choice maxOccurs="unbounded">+ <xsd:element name="metadata">+ <xsd:complexType>+ <xsd:sequence>+ <xsd:element name="value" type="xsd:string" minOccurs="0" />+ </xsd:sequence>+ <xsd:attribute name="name" use="required" type="xsd:string" />+ <xsd:attribute name="type" type="xsd:string" />+ <xsd:attribute name="mimetype" type="xsd:string" />+ <xsd:attribute ref="xml:space" />+ </xsd:complexType>+ </xsd:element>+ <xsd:element name="assembly">+ <xsd:complexType>+ <xsd:attribute name="alias" type="xsd:string" />+ <xsd:attribute name="name" type="xsd:string" />+ </xsd:complexType>+ </xsd:element>+ <xsd:element name="data">+ <xsd:complexType>+ <xsd:sequence>+ <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />+ <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />+ </xsd:sequence>+ <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />+ <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />+ <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />+ <xsd:attribute ref="xml:space" />+ </xsd:complexType>+ </xsd:element>+ <xsd:element name="resheader">+ <xsd:complexType>+ <xsd:sequence>+ <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />+ </xsd:sequence>+ <xsd:attribute name="name" type="xsd:string" use="required" />+ </xsd:complexType>+ </xsd:element>+ </xsd:choice>+ </xsd:complexType>+ </xsd:element>+ </xsd:schema>+ <resheader name="resmimetype">+ <value>text/microsoft-resx</value>+ </resheader>+ <resheader name="version">+ <value>2.0</value>+ </resheader>+ <resheader name="reader">+ <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>+ </resheader>+ <resheader name="writer">+ <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>+ </resheader>+ <data name="AccessibleName" xml:space="preserve">+ <value>Markdown preview tool</value>+ </data>+ <data name="Configuration" xml:space="preserve">+ <value>Configuration</value>+ </data>+ <data name="DarkTheme" xml:space="preserve">+ <value>Dark</value>+ </data>+ <data name="Description" xml:space="preserve">+ <value>Preview a Markdown document with a GitHub-like render</value>+ </data>+ <data name="Input" xml:space="preserve">+ <value>Markdown</value>+ </data>+ <data name="LightTheme" xml:space="preserve">+ <value>Light</value>+ </data>+ <data name="LongDisplayTitle" xml:space="preserve">+ <value>Markdown Preview</value>+ </data>+ <data name="Output" xml:space="preserve">+ <value>Preview</value>+ </data>+ <data name="SearchKeywords" xml:space="preserve">+ <value>MD</value>+ </data>+ <data name="ShortDisplayTitle" xml:space="preserve">+ <value>Markdown Preview</value>+ </data>+ <data name="ThemeDescription" xml:space="preserve">+ <value>Select which theme to use to preview the Markdown</value>+ </data>+ <data name="ThemeTitle" xml:space="preserve">+ <value>Theme</value>+ </data>+</root>\ No newline at end of filediff --git a/src/app/dev/DevToys.Tools/Tools/Text/MarkdownPreview/MarkdownPreviewGuiTool.cs b/src/app/dev/DevToys.Tools/Tools/Text/MarkdownPreview/MarkdownPreviewGuiTool.csnew file mode 100644index 000000000..0c35660d2--- /dev/null+++ b/src/app/dev/DevToys.Tools/Tools/Text/MarkdownPreview/MarkdownPreviewGuiTool.cs@@ -0,0 +1,193 @@+using System.Reflection;+using ColorCode.Styling;+using DevToys.Tools.Models;+using Markdig;+using Markdown.ColorCode;+using Microsoft.Extensions.Logging;++namespace DevToys.Tools.Tools.Text.MarkdownPreview;++[Export(typeof(IGuiTool))]+[Name("MarkdownPreview")]+[ToolDisplayInformation(+ IconFontName = "DevToys-Tools-Icons",+ IconGlyph = '\u0112',+ GroupName = PredefinedCommonToolGroupNames.Text,+ ResourceManagerAssemblyIdentifier = nameof(DevToysToolsResourceManagerAssemblyIdentifier),+ ResourceManagerBaseName = "DevToys.Tools.Tools.Text.MarkdownPreview.MarkdownPreview",+ ShortDisplayTitleResourceName = nameof(MarkdownPreview.ShortDisplayTitle),+ LongDisplayTitleResourceName = nameof(MarkdownPreview.LongDisplayTitle),+ DescriptionResourceName = nameof(MarkdownPreview.Description),+ AccessibleNameResourceName = nameof(MarkdownPreview.AccessibleName),+ SearchKeywordsResourceName = nameof(MarkdownPreview.SearchKeywords))]+[AcceptedDataTypeName("Markdown")]+internal sealed class MarkdownPreviewGuiTool : IGuiTool, IDisposable+{+ /// <summary>+ /// Whether the markdown should be previewed in dark or light theme.+ /// </summary>+ private static readonly SettingDefinition<MarkdownPreviewTheme> theme+ = new(+ name: $"{nameof(MarkdownPreviewGuiTool)}.{nameof(theme)}",+ defaultValue: MarkdownPreviewTheme.Dark);++ private enum GridRows+ {+ TopAuto,+ Stretch,+ }++ private enum GridColumns+ {+ Stretch+ }++ private readonly DisposableSemaphore _semaphore = new();+ private readonly ILogger _logger;+ private readonly ISettingsProvider _settingsProvider;+ private readonly IUIWebView _webView = WebView();+ private readonly IUIMultiLineTextInput _inputText = MultilineTextInput();+ private readonly Lazy<string> _githubMarkdownIndexPage = new(() => ReadEmbeddedResource("index.html"));++ private CancellationTokenSource? _cancellationTokenSource;++ [ImportingConstructor]+ public MarkdownPreviewGuiTool(ISettingsProvider settingsProvider)+ {+ _logger = this.Log();+ _settingsProvider = settingsProvider;+ }++ // For unit tests.+ internal Task? WorkTask { get; private set; }++ public UIToolView View+ => new(+ isScrollable: false,+ Grid()+ .ColumnMediumSpacing()++ .Rows(+ (GridRows.TopAuto, Auto),+ (GridRows.Stretch, new UIGridLength(1, UIGridUnitType.Fraction)))++ .Columns(+ (GridColumns.Stretch, new UIGridLength(1, UIGridUnitType.Fraction)))++ .Cells(+ Cell(+ GridRows.TopAuto,+ GridColumns.Stretch,++ Stack()+ .Vertical()++ .WithChildren(+ Label().Text(MarkdownPreview.Configuration),++ Setting()+ .Icon("FluentSystemIcons", '\uF591')+ .Title(MarkdownPreview.ThemeTitle)+ .Description(MarkdownPreview.ThemeDescription)+ .Handle(+ _settingsProvider,+ theme,+ OnThemeChanged,+ Item(MarkdownPreview.DarkTheme, MarkdownPreviewTheme.Dark),+ Item(MarkdownPreview.LightTheme, MarkdownPreviewTheme.Light)))),++ Cell(+ GridRows.Stretch,+ GridColumns.Stretch,++ SplitGrid()+ .Vertical()+ .LeftPaneLength(new UIGridLength(1, UIGridUnitType.Fraction))+ .RightPaneLength(new UIGridLength(1, UIGridUnitType.Fraction))++ .WithLeftPaneChild(+ _inputText+ .Title(MarkdownPreview.Input)+ .Language("markdown")+ .CanCopyWhenEditable()+ .OnTextChanged(OnInputTextChanged))++ .WithRightPaneChild(+ _webView+ .Title(MarkdownPreview.Output)))));++ public void OnDataReceived(string dataTypeName, object? parsedData)+ {+ if (dataTypeName == "Markdown" && parsedData is string markdown)+ {+ _inputText.Text(markdown); // This will trigger an update of the preview.+ }+ }++ public void Dispose()+ {+ _cancellationTokenSource?.Cancel();+ _cancellationTokenSource?.Dispose();+ _semaphore.Dispose();+ }++ private void OnThemeChanged(MarkdownPreviewTheme _)+ {+ StartPreview();+ }++ private void OnInputTextChanged(string _)+ {+ StartPreview();+ }++ private void StartPreview()+ {+ _cancellationTokenSource?.Cancel();+ _cancellationTokenSource?.Dispose();+ _cancellationTokenSource = new CancellationTokenSource();++ WorkTask = PreviewAsync(_inputText.Text, _settingsProvider.GetSetting(theme), _cancellationTokenSource.Token);+ }++ private async Task PreviewAsync(string markdown, MarkdownPreviewTheme theme, CancellationToken cancellationToken)+ {+ using (await _semaphore.WaitAsync(cancellationToken))+ {+ await TaskSchedulerAwaiter.SwitchOffMainThreadAsync(cancellationToken);++ StyleDictionary codeStyleDictionary = theme == MarkdownPreviewTheme.Dark+ ? StyleDictionary.DefaultDark+ : StyleDictionary.DefaultLight;++ MarkdownPipeline pipeline+ = new MarkdownPipelineBuilder()+ .UseEmojiAndSmiley()+ .UseSmartyPants()+ .UseAdvancedExtensions()+ .UseColorCode(styleDictionary: codeStyleDictionary)+ .EnableTrackTrivia()+ .UseYamlFrontMatter()+ .Build();++ string? htmlBody = Markdig.Markdown.ToHtml(markdown, pipeline);++ string htmlDocument+ = _githubMarkdownIndexPage.Value+ .Replace("{{renderTheme}}", theme.ToString().ToLower())+ .Replace("{{htmlBody}}", htmlBody);++ _webView.RenderHTML(htmlDocument);+ }+ }++ private static string ReadEmbeddedResource(string name)+ {+ var assembly = Assembly.GetExecutingAssembly();+ string resourceName = $"DevToys.Tools.Assets.GitHubMarkdown.{name}";++ using Stream stream = assembly.GetManifestResourceStream(resourceName)!;+ using StreamReader reader = new(stream);+ return reader.ReadToEnd();+ }+}