An extension for Markdig that adds syntax highlighting to code through the power of ColorCode.
using Foo.Bar.Baz;
namespace Foo.Api;
public interface FooService {
/// <summary>
/// Gets a new Foo!
/// </summary>
/// <returns>A new Foo</returns>
public void GetFoo() {
return new Foo();
}
}
using Foo.Bar.Baz;
namespace Foo.Api;
public interface FooService {
/// <summary>
/// Gets a new Foo!
/// </summary>
/// <returns>A new Foo</returns>
public void GetFoo() {
return new Foo();
}
}
Install-Package Markdown.ColorCode -Version 2.3.0
dotnet add package Markdown.ColorCode --version 2.3.0
To use this extension with Markdig, simply install the Markdown.ColorCode
package use the ColorCode extension:
var pipeline = new MarkdownPipelineBuilder()
.UseAdvancedExtensions()
.UseColorCode()
.Build();
var colorizedHtml = Markdig.Markdown.ToHtml(someMarkdown, pipeline);
Several optional configuration options are also at your disposal:
var pipeline = new MarkdownPipelineBuilder()
.UseAdvancedExtensions()
.UseColorCode(
HtmlFormatterType.Style, // use style-based colorization (default)
myCustomStyleDictionary, // use a custom colorization style dictionary
myAdditionalLanguages, // augment the built-in language support
myCustomLanguageId // set a default language ID to fall back to
)
.Build();
var colorizedHtml = Markdig.Markdown.ToHtml(someMarkdown, pipeline);
For an improved experience with C# code blocks, consider using the Markdown.ColorCode.CSharpToColoredHtml
package. This package provides a more robust syntax highlighting experience for C# code blocks by leveraging CSharpToColouredHtml.Core.
var pipeline = new MarkdownPipelineBuilder()
.UseAdvancedExtensions()
.UseColorCodeWithCSharpToColoredHtml(
HtmlFormatterType.Style, // use style-based colorization (default)
myCustomStyleDictionary, // use a custom colorization style dictionary
myHtmlEmitterSettings, // configures CSharpToColouredHtml's HTML emitter
myAdditionalLanguages, // augment the built-in language support
myCustomLanguageId // set a default language ID to fall back to
)
.Build();
var colorizedHtml = Markdig.Markdown.ToHtml(someMarkdown, pipeline);
[!NOTE] The
CsharpToColouredHTML
package introduces dependencies which will not work well with Blazor WebAssembly projects. Either generate the HTML on the server side and send it to the client or use the baseMarkdown.ColorCode
package if you are working with Blazor WebAssembly. See https://github.com/dotnet/aspnetcore/issues/27373 and https://github.com/dotnet/aspnetcore/issues/26724 for more details on the problem.
See the open issues for a list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated. For detailed contributing guidelines, please see CONTRIBUTING.md.
Distributed under the MIT License
License. See LICENSE
for more information.
Project Link: https://github.com/wbaldoumas/markdown-colorcode
This README
was adapted from
https://github.com/othneildrew/Best-README-Template.