-
Notifications
You must be signed in to change notification settings - Fork 634
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
289 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<configuration> | ||
<startup> | ||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" /> | ||
</startup> | ||
</configuration> |
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,62 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{0893F745-CB1A-427A-8E87-CA927273039A}</ProjectGuid> | ||
<OutputType>Exe</OutputType> | ||
<RootNamespace>MD2HTML</RootNamespace> | ||
<AssemblyName>MD2HTML</AssemblyName> | ||
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> | ||
<Deterministic>true</Deterministic> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>..\..\..\bin\AnyCPU\Debug\MD2HTML\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>..\..\..\bin\AnyCPU\Release\MD2HTML\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Net.Http" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="MarkdownHandler.cs" /> | ||
<Compile Include="Program.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="App.config" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="HtmlSanitizer"> | ||
<Version>5.0.355</Version> | ||
</PackageReference> | ||
<PackageReference Include="Markdig"> | ||
<Version>0.22.0</Version> | ||
</PackageReference> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
</Project> |
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,109 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using Ganss.XSS; | ||
using Markdig; | ||
using Markdig.Parsers; | ||
using Markdig.Renderers; | ||
using Markdig.Syntax; | ||
using Markdig.Syntax.Inlines; | ||
|
||
namespace MD2HTML | ||
{ | ||
/// <summary> | ||
/// Handles markdown files by converting them to Html, so they can display in a browser | ||
/// </summary> | ||
internal class MarkdownHandler | ||
{ | ||
private readonly MarkdownPipeline pipeline; | ||
|
||
|
||
private static MarkdownHandler instance; | ||
internal static MarkdownHandler Instance | ||
{ | ||
get | ||
{ | ||
if (instance is null) { instance = new MarkdownHandler(); } | ||
return instance; | ||
} | ||
} | ||
|
||
private MarkdownHandler() | ||
{ | ||
var pipelineBuilder = new MarkdownPipelineBuilder(); | ||
pipeline = pipelineBuilder | ||
.UseAdvancedExtensions() | ||
.Build(); | ||
} | ||
|
||
/// <summary> | ||
/// Converts a markdown string into Html. | ||
/// </summary> | ||
/// <param name="writer"></param> | ||
/// <param name="mdString"></param> | ||
/// <returns>Returns true if any script tags was removed from the string</returns> | ||
internal void ParseToHtml(ref StringWriter writer, string mdString, string mdPath) | ||
{ | ||
if (writer is null) | ||
throw new ArgumentNullException(nameof(writer)); | ||
|
||
if (string.IsNullOrWhiteSpace(mdString)) | ||
return; | ||
|
||
// Remove scripts from user content for security reasons. | ||
RemoveScriptTagsFromString(ref mdString); | ||
|
||
var renderer = new HtmlRenderer(writer); | ||
pipeline.Setup(renderer); | ||
|
||
var document = MarkdownParser.Parse(mdString, pipeline); | ||
ConvertRelativeLocalImagePathsToAbsolute(mdPath, document); | ||
|
||
renderer.Render(document); | ||
} | ||
|
||
/// <summary> | ||
/// For markdown local images needs to be in the same folder as the md file | ||
/// referencing it with a relative path "./image.png", when we convert to html | ||
/// we need the full path. This method finds relative image paths and converts them to absolute paths. | ||
/// </summary> | ||
private static void ConvertRelativeLocalImagePathsToAbsolute(string mdFilePath, MarkdownDocument document) | ||
{ | ||
var imageLinks = document.Descendants<ParagraphBlock>() | ||
.SelectMany(x => x.Inline.Descendants<LinkInline>()) | ||
.Where(x => x.IsImage) | ||
.Select(x => x).ToList(); | ||
|
||
foreach (var image in imageLinks) | ||
{ | ||
if (!image.Url.StartsWith("./")) | ||
continue; | ||
|
||
var imageName = image.Url.Split(new string[] { "./" }, StringSplitOptions.None); | ||
var dir = Path.GetDirectoryName(mdFilePath); | ||
|
||
var htmlImagePathPrefix = @"file:///"; | ||
var absoluteImagePath = Path.Combine(dir, imageName.Last()); | ||
|
||
image.Url = $"{htmlImagePathPrefix}{absoluteImagePath}"; | ||
} | ||
} | ||
|
||
private static HtmlSanitizer htmlSanitizer = new HtmlSanitizer(); | ||
|
||
/// <summary> | ||
/// Clean up possible dangerous HTML content from the content string. | ||
/// </summary> | ||
/// <param name="content"></param> | ||
/// <returns>Returns true if any content was removed from the content string</returns> | ||
internal static bool RemoveScriptTagsFromString(ref string content) | ||
{ | ||
var sanitizedContent = htmlSanitizer.Sanitize(content); | ||
if (content.Equals(sanitizedContent)) | ||
return false; | ||
|
||
content = sanitizedContent; | ||
return true; | ||
} | ||
} | ||
} |
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,65 @@ | ||
using System; | ||
using System.IO; | ||
|
||
namespace MD2HTML | ||
{ | ||
static class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
Console.WriteLine(""); | ||
|
||
while (true) | ||
{ | ||
var line = Console.ReadLine(); | ||
if (line == "<<<<<Sanitize>>>>>") | ||
{ | ||
Sanitize(); | ||
} | ||
else if (line == "<<<<<Convert>>>>>") | ||
{ | ||
Convert(); | ||
} | ||
Console.WriteLine("<<<<<Eod>>>>>"); | ||
} | ||
} | ||
|
||
static void Sanitize() | ||
{ | ||
StringWriter data = new StringWriter(); | ||
GetData(ref data); | ||
|
||
string output = data.ToString(); | ||
MarkdownHandler.RemoveScriptTagsFromString(ref output); | ||
|
||
Console.WriteLine(output); | ||
} | ||
|
||
static void Convert() | ||
{ | ||
var mdPath = Console.ReadLine(); | ||
|
||
StringWriter data = new StringWriter(); | ||
GetData(ref data); | ||
|
||
var instance = MarkdownHandler.Instance; | ||
StringWriter output = new StringWriter(); | ||
instance.ParseToHtml(ref output, data.ToString(), mdPath); | ||
|
||
Console.WriteLine(output.ToString()); | ||
} | ||
|
||
static void GetData(ref StringWriter data) | ||
{ | ||
while (true) | ||
{ | ||
var line = Console.ReadLine(); | ||
if (line == "<<<<<Eod>>>>>") | ||
{ | ||
break; | ||
} | ||
data.WriteLine(line); | ||
} | ||
} | ||
} | ||
} |
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,36 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyTitle("MD2HTML")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("MD2HTML")] | ||
[assembly: AssemblyCopyright("Copyright © 2020")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("0893f745-cb1a-427a-8e87-ca927273039a")] | ||
|
||
// Version information for an assembly consists of the following four values: | ||
// | ||
// Major Version | ||
// Minor Version | ||
// Build Number | ||
// Revision | ||
// | ||
// You can specify all the values or you can default the Build and Revision Numbers | ||
// by using the '*' as shown below: | ||
// [assembly: AssemblyVersion("1.0.*")] | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |