Skip to content

Commit 1909a66

Browse files
committed
Emit error when parsing fails
1 parent 462ae29 commit 1909a66

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

src/Elastic.Markdown/Myst/Roles/Kbd/Kbd.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class KeyboardShortcut(IReadOnlyList<IKeyNode> keys)
1414
{
1515
private IReadOnlyList<IKeyNode> Keys { get; } = keys;
1616

17+
public static KeyboardShortcut Empty { get; } = new([]);
18+
1719
public static KeyboardShortcut Parse(string input)
1820
{
1921
if (string.IsNullOrWhiteSpace(input))

src/Elastic.Markdown/Myst/Roles/Kbd/KbdParser.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
// See the LICENSE file in the project root for more information
44

55
using System;
6+
using Elastic.Markdown.Diagnostics;
67
using Markdig.Parsers;
78

89
namespace Elastic.Markdown.Myst.Roles.Kbd;
910

1011
public class KbdParser : RoleParser<KbdRole>
1112
{
1213

13-
protected override KbdRole CreateRole(string role, string content, InlineProcessor parserContext) =>
14-
new(role, content);
14+
protected override KbdRole CreateRole(string role, string content, InlineProcessor parserContext)
15+
=> new(role, content, parserContext);
1516

1617
protected override bool Matches(ReadOnlySpan<char> role) => role is "{kbd}";
1718
}

src/Elastic.Markdown/Myst/Roles/Kbd/KbdRole.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,25 @@
33
// See the LICENSE file in the project root for more information
44

55
using System.Diagnostics;
6+
using Elastic.Markdown.Diagnostics;
7+
using Markdig.Parsers;
68

79
namespace Elastic.Markdown.Myst.Roles.Kbd;
810

911
[DebuggerDisplay("{GetType().Name} Line: {Line}, Role: {Role}, Content: {Content}")]
10-
public class KbdRole(string role, string content) : RoleLeaf(role, content)
12+
public class KbdRole : RoleLeaf
1113
{
12-
public KeyboardShortcut KeyboardShortcut { get; } = KeyboardShortcut.Parse(content);
14+
public KbdRole(string role, string content, InlineProcessor parserContext) : base(role, content)
15+
{
16+
try
17+
{
18+
KeyboardShortcut = KeyboardShortcut.Parse(content);
19+
}
20+
catch (Exception ex)
21+
{
22+
parserContext.EmitError(this, Role.Length + content.Length, $"Failed to parse keyboard shortcut: \"{content}\"", ex);
23+
KeyboardShortcut = KeyboardShortcut.Empty;
24+
}
25+
}
26+
public KeyboardShortcut KeyboardShortcut { get; }
1327
}

0 commit comments

Comments
 (0)