-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a simple markdown to plain text converter (#210)
* Add Alexa Auth Handler. * Fix crazyness in merge. * again. * Add Alexa Skill Id validation check. * Tabs to spaces * Add a couple tests. * Separate skill id check. * Set ExpectReplies and add more tests. * Dont NRE when activities is null. * Make methods virutal for mocking * Add check for Request being null so we don't throw NRE on bad requests from Alexa. * Add simple markdown formatter Co-authored-by: Gary Pretty <gary@garypretty.co.uk>
- Loading branch information
1 parent
3b15b94
commit dc83d1d
Showing
3 changed files
with
94 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
53 changes: 53 additions & 0 deletions
53
...ies/Bot.Builder.Community.Adapters.Alexa.Core/Utility/AlexaMarkdownToPlaintextRenderer.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,53 @@ | ||
using System; | ||
using Microsoft.MarkedNet; | ||
|
||
namespace Bot.Builder.Community.Adapters.Alexa.Core.Utility | ||
{ | ||
/// <summary> | ||
/// Simple Markdown renderer to turn markdown into plain text for Alexa. | ||
/// </summary> | ||
public static class AlexaMarkdownToPlaintextRenderer | ||
{ | ||
private static readonly Marked _marked = new Marked(new Options { Renderer = new RemoveMarkupRenderer() }); | ||
|
||
public static string Render(string source) => _marked.Parse(source); | ||
|
||
private class RemoveMarkupRenderer : MarkdownRenderer | ||
{ | ||
private const string ListItemMarker = "$$ListItemMarker$$"; | ||
|
||
public override string Blockquote(string quote) => string.Concat(Environment.NewLine, quote, Environment.NewLine); | ||
public override string Br() => Environment.NewLine; | ||
public override string Code(string code, string lang, bool escaped) => code; | ||
public override string Codespan(string text) => text; | ||
public override string Del(string text) => text; | ||
public override string Em(string text) => text; | ||
public override string Heading(string text, int level, string raw) => string.Concat(Environment.NewLine, text, Environment.NewLine); | ||
public override string Hr() => Environment.NewLine; | ||
public override string Html(string html) => string.Empty; | ||
public override string Image(string href, string title, string text) => title ?? text; | ||
public override string Link(string href, string title, string text) => $"{title ?? text} {href}"; | ||
public override string List(string body, bool ordered, int start) | ||
{ | ||
if (ordered) | ||
{ | ||
for (int marker = start, markerIndex = body.IndexOf(ListItemMarker); markerIndex >= 0; markerIndex = body.IndexOf(ListItemMarker), ++marker) | ||
{ | ||
body = body.Substring(0, markerIndex) + Environment.NewLine + marker + " " + body.Substring(markerIndex + ListItemMarker.Length); | ||
} | ||
} | ||
else | ||
{ | ||
body = body.Replace(ListItemMarker, Environment.NewLine); | ||
} | ||
return body; | ||
} | ||
public override string ListItem(string text) => $"{ListItemMarker}{text}"; | ||
public override string Paragraph(string text) => string.Concat(Environment.NewLine, text, Environment.NewLine); | ||
public override string Strong(string text) => text; | ||
public override string Table(string header, string body) => string.Empty; | ||
public override string TableCell(string content, TableCellFlags flags) => string.Empty; | ||
public override string TableRow(string content) => string.Empty; | ||
} | ||
} | ||
} |