// 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.IO; using Markdig; namespace Microsoft.PowerToys.FilePreviewCommon { public static class MarkdownHelper { /// /// Markdown HTML header for light theme. /// private static readonly string HtmlLightHeader = "
"; /// /// Markdown HTML header for dark theme. /// private static readonly string HtmlDarkHeader = "
"; /// /// Markdown HTML footer. /// private static readonly string HtmlFooter = "
"; public static string MarkdownHtml(string fileContent, string theme, string filePath, ImagesBlockedCallBack imagesBlockedCallBack) { var htmlHeader = theme == "dark" ? HtmlDarkHeader : HtmlLightHeader; // Extension to modify markdown AST. HTMLParsingExtension extension = new HTMLParsingExtension(imagesBlockedCallBack); extension.FilePath = Path.GetDirectoryName(filePath) ?? string.Empty; // if you have a string with double space, some people view it as a new line. // while this is against spec, even GH supports this. Technically looks like GH just trims whitespace // https://github.com/microsoft/PowerToys/issues/10354 var softlineBreak = new Markdig.Extensions.Hardlines.SoftlineBreakAsHardlineExtension(); MarkdownPipelineBuilder pipelineBuilder; pipelineBuilder = new MarkdownPipelineBuilder().UseAdvancedExtensions().UseEmojiAndSmiley().UseYamlFrontMatter().UseMathematics(); pipelineBuilder.Extensions.Add(extension); pipelineBuilder.Extensions.Add(softlineBreak); MarkdownPipeline pipeline = pipelineBuilder.Build(); string parsedMarkdown = Markdown.ToHtml(fileContent, pipeline); string markdownHTML = $"{htmlHeader}{parsedMarkdown}{HtmlFooter}"; return markdownHTML; } } }