66using System . IO ;
77
88using Common . UI ;
9- using Markdig ;
10- using Microsoft . PowerToys . FilePreviewCommon ;
119
1210namespace Peek . FilePreviewer . Previewers
1311{
1412 public class MarkdownHelper
1513 {
16- /// <summary>
17- /// Markdown HTML header for light theme.
18- /// </summary>
19- private static readonly string HtmlLightHeader = "<!doctype html><style>body{width:100%;margin:0;font-family:-apple-system,BlinkMacSystemFont,\" Segoe UI\" ,Roboto,\" Helvetica Neue\" ,Arial,\" Noto Sans\" ,sans-serif,\" Apple Color Emoji\" ,\" Segoe UI Emoji\" ,\" Segoe UI Symbol\" ,\" Noto Color Emoji\" ;font-size:1rem;font-weight:400;line-height:1.5;color:#212529;text-align:left;background-color:#fff}.container{padding:5%}body img{max-width:100%;height:auto}body h1,body h2,body h3,body h4,body h5,body h6{margin-top:24px;margin-bottom:16px;font-weight:600;line-height:1.25}body h1,body h2{padding-bottom:.3em;border-bottom:1px solid #eaecef}body{font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji}body h3{font-size:1.25em}body h4{font-size:1em}body h5{font-size:.875em}body h6{font-size:.85em;color:#6a737d}pre{font-family:SFMono-Regular,Consolas,Liberation Mono,Menlo,monospace;background-color:#f6f8fa;border-radius:3px;padding:16px;font-size:85%}a{color:#0366d6}strong{font-weight:600}em{font-style:italic}code{padding:.2em .4em;margin:0;font-size:85%;background-color:#f6f8fa;border-radius:3px}hr{border-color:#EEE -moz-use-text-color #FFF;border-style:solid none;border-width:.5px 0;margin:18px 0}table{display:block;width:100%;overflow:auto;border-spacing:0;border-collapse:collapse}tbody{display:table-row-group;vertical-align:middle;border-color:inherit;vertical-align:inherit;border-color:inherit}table tr{background-color:#fff;border-top:1px solid #c6cbd1}tr{display:table-row;vertical-align:inherit;border-color:inherit}table td,table th{padding:6px 13px;border:1px solid #dfe2e5}th{font-weight:600;display:table-cell;vertical-align:inherit;font-weight:bold;text-align:-internal-center}thead{display:table-header-group;vertical-align:middle;border-color:inherit}td{display:table-cell;vertical-align:inherit}code,pre,tt{font-family:SFMono-Regular,Menlo,Monaco,Consolas,\" Liberation Mono\" ,\" Courier New\" ,monospace;color:#24292e;overflow-x:auto}pre code{display:block;font-size:inherit;color:inherit;word-break:normal}blockquote{background-color:#fff;border-radius:3px;padding:15px;font-size:14px;display:block;margin-block-start:1em;margin-block-end:1em;margin-inline-start:40px;margin-inline-end:40px;padding:0 1em;color:#6a737d;border-left:.25em solid #dfe2e5}</style><body><div class=\" container\" >" ;
20-
21- /// <summary>
22- /// Markdown HTML header for dark theme.
23- /// </summary>
24- private static readonly string HtmlDarkHeader = "<!doctype html><style>body{width:100%;margin:0;font-family:-apple-system,BlinkMacSystemFont,\" Segoe UI\" ,Roboto,\" Helvetica Neue\" ,Arial,\" Noto Sans\" ,sans-serif,\" Apple Color Emoji\" ,\" Segoe UI Emoji\" ,\" Segoe UI Symbol\" ,\" Noto Color Emoji\" ;font-size:1rem;font-weight:400;line-height:1.5;color:#d4d4d4;text-align:left;background-color:#1e1e1e}.container{padding:5%}body img{max-width:100%;height:auto}body h1,body h2,body h3,body h4,body h5,body h6{margin-top:24px;margin-bottom:16px;font-weight:600;line-height:1.25}body h1,body h2{padding-bottom:.3em;border-bottom:1px solid #474747}body{font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji}body h3{font-size:1.25em}body h4{font-size:1em}body h5{font-size:.875em}body h6{font-size:.85em;color:#d4d4d4}pre{font-family:SFMono-Regular,Consolas,Liberation Mono,Menlo,monospace;background-color:#161616;border-radius:3px;padding:16px;font-size:85%}a{color:#0366d6}strong{font-weight:600}em{font-style:italic}code{padding:.2em .4em;margin:0;font-size:85%;background-color:#161616;border-radius:3px}hr{border-color:#EEE -moz-use-text-color #FFF;border-style:solid none;border-width:.5px 0;margin:18px 0}table{display:block;width:100%;overflow:auto;border-spacing:0;border-collapse:collapse}tbody{display:table-row-group;vertical-align:middle;border-color:inherit;vertical-align:inherit;border-color:inherit}table tr{background-color:#1e1e1e;border-top:1px solid #c6cbd1}tr{display:table-row;vertical-align:inherit;border-color:inherit}table td,table th{padding:6px 13px;border:1px solid #474747}th{font-weight:600;display:table-cell;vertical-align:inherit;font-weight:bold;text-align:-internal-center}thead{display:table-header-group;vertical-align:middle;border-color:inherit}td{display:table-cell;vertical-align:inherit}code,pre,tt{font-family:SFMono-Regular,Menlo,Monaco,Consolas,\" Liberation Mono\" ,\" Courier New\" ,monospace;color:#d4d4d4;overflow-x:auto}pre code{display:block;font-size:inherit;color:inherit;word-break:normal}blockquote{background-color:#282828;border-radius:3px;padding:15px;font-size:14px;display:block;margin-block-start:1em;margin-block-end:1em;margin-inline-start:40px;margin-inline-end:40px;padding:0 1em;color:#d4d4d4;border-left:.25em solid #d4d4d4}</style><body><div class=\" container\" >" ;
25-
26- /// <summary>
27- /// Markdown HTML footer.
28- /// </summary>
29- private static readonly string HtmlFooter = "</div></body></html>" ;
30-
3114 /// <summary>
3215 /// Prepares temp html for the previewing
3316 /// </summary>
3417 public static string PreviewTempFile ( string fileText , string filePath , string tempFolder )
3518 {
3619 string theme = ThemeManager . GetWindowsBaseColor ( ) . ToLowerInvariant ( ) ;
37- string markdownHTML = MarkdownHtml ( fileText , theme , filePath , ImageBlockedCallback ) ;
20+ string markdownHTML = Microsoft . PowerToys . FilePreviewCommon . MarkdownHelper . MarkdownHtml ( fileText , theme , filePath , ImageBlockedCallback ) ;
3821
3922 string filename = tempFolder + "\\ " + Guid . NewGuid ( ) . ToString ( ) + ".html" ;
4023 File . WriteAllText ( filename , markdownHTML ) ;
@@ -44,32 +27,5 @@ public static string PreviewTempFile(string fileText, string filePath, string te
4427 private static void ImageBlockedCallback ( )
4528 {
4629 }
47-
48- public static string MarkdownHtml ( string fileContent , string theme , string filePath , ImagesBlockedCallBack imagesBlockedCallBack )
49- {
50- var htmlHeader = theme == "dark" ? HtmlDarkHeader : HtmlLightHeader ;
51-
52- // Extension to modify markdown AST.
53- HTMLParsingExtension extension = new HTMLParsingExtension ( imagesBlockedCallBack ) ;
54- extension . FilePath = Path . GetDirectoryName ( filePath ) ?? string . Empty ;
55-
56- // if you have a string with double space, some people view it as a new line.
57- // while this is against spec, even GH supports this. Technically looks like GH just trims whitespace
58- // https://github.com/microsoft/PowerToys/issues/10354
59- var softlineBreak = new Markdig . Extensions . Hardlines . SoftlineBreakAsHardlineExtension ( ) ;
60-
61- MarkdownPipelineBuilder pipelineBuilder ;
62-
63- // Removed .DisableHtml()
64- pipelineBuilder = new MarkdownPipelineBuilder ( ) . UseAdvancedExtensions ( ) . UseEmojiAndSmiley ( ) . UseYamlFrontMatter ( ) . UseMathematics ( ) ;
65- pipelineBuilder . Extensions . Add ( extension ) ;
66- pipelineBuilder . Extensions . Add ( softlineBreak ) ;
67-
68- MarkdownPipeline pipeline = pipelineBuilder . Build ( ) ;
69- string parsedMarkdown = Markdown . ToHtml ( fileContent , pipeline ) ;
70-
71- string markdownHTML = $ "{ htmlHeader } { parsedMarkdown } { HtmlFooter } ";
72- return markdownHTML ;
73- }
7430 }
7531}
0 commit comments