-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from andresharpe/feature/cute-chat-markdown-su…
…pport Added markdown support for general chat
- Loading branch information
Showing
30 changed files
with
1,242 additions
and
9 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
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
11 changes: 11 additions & 0 deletions
11
source/Cute/Services/Markdown/Converters/AnsiSupportConverter.cs
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,11 @@ | ||
using Spectre.Console; | ||
|
||
namespace Cute.Services.Markdown.Console.Converters; | ||
|
||
internal static class AnsiSupportConverter | ||
{ | ||
internal static AnsiSupport FromAnsiSupported(bool ansiSupported) => | ||
ansiSupported | ||
? AnsiSupport.Yes | ||
: AnsiSupport.No; | ||
} |
20 changes: 20 additions & 0 deletions
20
source/Cute/Services/Markdown/Converters/ColorSystemSupportConverter.cs
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,20 @@ | ||
using Cute.Services.Markdown.Console.Renderers; | ||
using Spectre.Console; | ||
|
||
namespace Cute.Services.Markdown.Console.Converters; | ||
|
||
internal static class ColorSystemSupportConverter | ||
{ | ||
internal static ColorSystemSupport FromColorSystem(ColorSystem colorSystem) | ||
{ | ||
return (colorSystem) switch | ||
{ | ||
ColorSystem.EightBit => ColorSystemSupport.EightBit, | ||
ColorSystem.Legacy => ColorSystemSupport.Legacy, | ||
ColorSystem.NoColors => ColorSystemSupport.NoColors, | ||
ColorSystem.Standard => ColorSystemSupport.Standard, | ||
ColorSystem.TrueColor => ColorSystemSupport.TrueColor, | ||
_ => ColorSystemSupport.Detect | ||
}; | ||
} | ||
} |
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,9 @@ | ||
using System; | ||
|
||
namespace Cute.Services.Markdown.Console.Extensions; | ||
|
||
public static class ObjectExtensions | ||
{ | ||
public static string ToNotNullString(this object obj) => | ||
obj.ToString() ?? string.Empty; | ||
} |
24 changes: 24 additions & 0 deletions
24
source/Cute/Services/Markdown/Formatters/NumberFormatter.cs
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,24 @@ | ||
using Cute.Services.Markdown.Console.Renderers.CharacterSets; | ||
|
||
namespace Cute.Services.Markdown.Console.Formatters; | ||
|
||
public class NumberFormatter | ||
{ | ||
public string Format(int number, CharacterSet characterSet) | ||
{ | ||
// TODO: There is no fallback if the users font does not support nerd fonts. | ||
// Detecting support could be very hard. | ||
// Nerd font may need to be a opt in/out feature. | ||
return number.ToString() | ||
.Replace("0", characterSet.Zero) | ||
.Replace("1", characterSet.One) | ||
.Replace("2", characterSet.Two) | ||
.Replace("3", characterSet.Three) | ||
.Replace("4", characterSet.Four) | ||
.Replace("5", characterSet.Five) | ||
.Replace("6", characterSet.Six) | ||
.Replace("7", characterSet.Seven) | ||
.Replace("8", characterSet.Eight) | ||
.Replace("9", characterSet.Nine); | ||
} | ||
} |
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,67 @@ | ||
using Cute.Constants; | ||
using Cute.Services.Markdown.Console.Renderers; | ||
|
||
namespace Cute.Services.Markdown; | ||
|
||
/// <summary> | ||
/// Renders markdown in the terminal. | ||
/// </summary> | ||
public static class MarkdownConsole | ||
{ | ||
private const UseNerdFonts UseNerdFontsDefault = UseNerdFonts.No; | ||
private static UseNerdFonts UseNerdFontsField = UseNerdFontsDefault; | ||
private static AnsiRenderer DefaultRenderer = GetAnsiRenderer(); | ||
|
||
/// <summary> | ||
/// Configure Nerd Fonts usage for special characters, like list bullets. | ||
/// <see href="https://www.nerdfonts.com/">Nerd Fonts</see> are awesome, but not supported by all fonts. | ||
/// </summary> | ||
public static UseNerdFonts UseNerdFonts | ||
{ | ||
get => UseNerdFontsField; | ||
set | ||
{ | ||
UseNerdFontsField = value; | ||
DefaultRenderer = GetAnsiRenderer(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Writes formatted markdown in the console. | ||
/// </summary> | ||
/// <param name="markdown">Markdown to format.</param> | ||
public static void Write(string markdown) | ||
{ | ||
DefaultRenderer.Write(markdown); | ||
} | ||
|
||
/// <summary> | ||
/// Writes formatted markdown in the console. | ||
/// </summary> | ||
/// <param name="markdown">Markdown to format.</param> | ||
/// <param name="writer"> | ||
/// Override the default console. | ||
/// <remarks> | ||
/// Useful for test and debugging only. | ||
/// </remarks> | ||
/// </param> | ||
public static void Write(string markdown, TextWriter writer) | ||
{ | ||
GetAnsiRenderer(writer).Write(markdown); | ||
} | ||
|
||
private static AnsiRenderer GetAnsiRenderer() | ||
{ | ||
return new AnsiRendererBuilder() | ||
.SetNerdFontsUsage(UseNerdFontsField) | ||
.Build(); | ||
} | ||
|
||
private static AnsiRenderer GetAnsiRenderer(TextWriter writer) | ||
{ | ||
return new AnsiRendererBuilder() | ||
.SetNerdFontsUsage(UseNerdFontsField) | ||
.RedirectOutput(writer) | ||
.Build(); | ||
} | ||
} |
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,39 @@ | ||
namespace Cute.Services.Markdown.Console.Options; | ||
|
||
/// <summary> | ||
/// Flags that modify the behaviour of the AnsiRenderer. | ||
/// </summary> | ||
internal static class FeatureFlags | ||
{ | ||
private const string ThrowOnSupportedEnvironmentVariable = "MORELLO_MARKDOWN_CONSOLE_THROW_ON_UNSUPPORTED_TYPE"; | ||
private const string ForceBasicSyntaxHighlighterEnvironmentVariable = "MORELLO_MARKDOWN_CONSOLE_FORCE_BASIC_SYNTAX_HIGHLIGHTER"; | ||
private const string ForceAnsiColourEnvironmentVariable = "MORELLO_MARKDOWN_CONSOLE_FORCE_ANSI_COLOUR"; | ||
private const string True = "true"; | ||
private const string Yes = "yes"; | ||
private const string On = "on"; | ||
private const string Enabled = "enabled"; | ||
private const string Active = "active"; | ||
|
||
/// <summary> | ||
/// Disables MarkdownConsole's default behaviour of falling back to plain text when it encounters | ||
/// an unsupported Markdown type. | ||
/// </summary> | ||
public static bool ThrowOnUnsupportedMarkdownType => | ||
EnvironmentVariablePresentAndActive(ThrowOnSupportedEnvironmentVariable); | ||
|
||
public static bool ForceBasicSyntaxHighlighter => | ||
EnvironmentVariablePresentAndActive(ForceBasicSyntaxHighlighterEnvironmentVariable); | ||
|
||
public static bool ForceAnsiColour => | ||
EnvironmentVariablePresentAndActive(ForceAnsiColourEnvironmentVariable); | ||
|
||
private static bool EnvironmentVariablePresentAndActive(string environmentVariableName) | ||
{ | ||
var value = Environment | ||
.GetEnvironmentVariable(environmentVariableName)? | ||
.ToLower() | ||
?? string.Empty; | ||
|
||
return value is True or Yes or On or Enabled or Active; | ||
} | ||
} |
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,24 @@ | ||
using Markdig; | ||
using Markdig.Syntax; | ||
|
||
namespace Cute.Services.Markdown.Console.Parsers; | ||
|
||
/// <summary> | ||
/// Parses raw markdown text and returns an abstract-syntax-tree representation. | ||
/// </summary> | ||
public class MarkdownParser | ||
{ | ||
private readonly static MarkdownPipeline _markdownPipeline = new MarkdownPipelineBuilder() | ||
.UseAdvancedExtensions() | ||
.Build(); | ||
|
||
/// <summary> | ||
/// Converts markdown into a document. | ||
/// /// </summary> | ||
/// <param name="markdown">Raw markdown text</param> | ||
/// <returns>ABT markdown document</returns> | ||
public MarkdownDocument ConvertToMarkdownDocument(string markdown) | ||
{ | ||
return Markdig.Markdown.Parse(markdown, _markdownPipeline); | ||
} | ||
} |
Oops, something went wrong.