-
Notifications
You must be signed in to change notification settings - Fork 32
Icons #1413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Icons #1413
Changes from 16 commits
7808089
69b43a4
874632f
96a32a7
6f62f40
e45efec
7ccba83
6769762
9dd4ee6
d3d7b10
66780ca
660e08b
d0fd929
79c8154
66337ea
36b67d2
91fc82b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| @layer components { | ||
| .markdown-content { | ||
| .icon { | ||
| @apply inline-block align-middle; | ||
| transform: translateY(-0.05em); | ||
| svg { | ||
| width: 1em; | ||
| height: 1em; | ||
| fill: currentColor; | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // Licensed to Elasticsearch B.V under one or more agreements. | ||
| // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
| // See the LICENSE file in the project root for more information | ||
|
|
||
| using System.Diagnostics; | ||
| using System.Text.RegularExpressions; | ||
| using Elastic.Markdown.Diagnostics; | ||
| using Markdig.Parsers; | ||
|
|
||
| namespace Elastic.Markdown.Myst.Roles.Icons; | ||
|
|
||
| [DebuggerDisplay("{GetType().Name} Line: {Line}, Role: {Role}, Content: {Content}")] | ||
| public class IconsRole : RoleLeaf | ||
| { | ||
| private static readonly IReadOnlyDictionary<string, string> IconMap; | ||
| static IconsRole() | ||
| { | ||
| var assembly = typeof(IconsRole).Assembly; | ||
| var iconFolder = $"{assembly.GetName().Name}.Myst.Roles.Icons.svgs."; | ||
| IconMap = assembly.GetManifestResourceNames() | ||
| .Where(r => r.StartsWith(iconFolder) && r.EndsWith(".svg")) | ||
| .ToDictionary( | ||
| r => r[iconFolder.Length..].Replace(".svg", string.Empty), | ||
| r => | ||
| { | ||
| using var stream = assembly.GetManifestResourceStream(r); | ||
| if (stream is null) | ||
| return string.Empty; | ||
| using var reader = new StreamReader(stream); | ||
| return reader.ReadToEnd(); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| public IconsRole(string role, string content, InlineProcessor processor) : base(role, content) | ||
| { | ||
|
|
||
| if (IconMap.TryGetValue(content, out var svg)) | ||
| { | ||
| Svg = svg; | ||
| Name = content; | ||
| } | ||
| else | ||
| processor.EmitError(this, Role.Length + content.Length, $"Unknown icon: {content}"); | ||
| } | ||
|
|
||
| public string? Name { get; } | ||
| public string? Svg { get; } | ||
| } | ||
|
|
||
| public partial class IconParser : RoleParser<IconsRole> | ||
| { | ||
| [GeneratedRegex(@"\{icon\}`([^`]+)`", RegexOptions.Compiled)] | ||
| public static partial Regex IconRegex(); | ||
|
|
||
| protected override IconsRole CreateRole(string role, string content, InlineProcessor parserContext) => | ||
| new(role, content, parserContext); | ||
|
|
||
| protected override bool Matches(ReadOnlySpan<char> role) => role is "{icon}"; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // Licensed to Elasticsearch B.V under one or more agreements. | ||
| // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
| // See the LICENSE file in the project root for more information | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
| using Markdig; | ||
| using Markdig.Parsers.Inlines; | ||
| using Markdig.Renderers; | ||
| using Markdig.Renderers.Html; | ||
| using Markdig.Renderers.Html.Inlines; | ||
|
|
||
| namespace Elastic.Markdown.Myst.Roles.Icons; | ||
|
|
||
| public class IconRoleHtmlRenderer : HtmlObjectRenderer<IconsRole> | ||
| { | ||
|
|
||
| [SuppressMessage("Reliability", "CA2012:Use ValueTasks correctly")] | ||
|
||
| protected override void Write(HtmlRenderer renderer, IconsRole role) | ||
| { | ||
| _ = renderer.Write($"<span aria-label=\"Icon for {role.Name}\" class=\"icon icon-{role.Name}\">"); | ||
| _ = renderer.Write(role.Svg); | ||
| _ = renderer.Write("</span>"); | ||
| } | ||
| } | ||
|
|
||
| public static class InlineAppliesToExtensions | ||
| { | ||
| public static MarkdownPipelineBuilder UseInlineIcons(this MarkdownPipelineBuilder pipeline) | ||
| { | ||
| pipeline.Extensions.AddIfNotAlready<InlineIconExtension>(); | ||
| return pipeline; | ||
| } | ||
| } | ||
|
|
||
| public class InlineIconExtension : IMarkdownExtension | ||
| { | ||
| public void Setup(MarkdownPipelineBuilder pipeline) => _ = pipeline.InlineParsers.InsertBefore<CodeInlineParser>(new IconParser()); | ||
|
|
||
| public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) => | ||
| renderer.ObjectRenderers.InsertBefore<CodeInlineRenderer>(new IconRoleHtmlRenderer()); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Not an issue in the PR, but in GH) What a weird false positive... If you open this in a hex editor, U+2009 (
0xE2 0x80 0x89) is nowhere to be seen.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, you mean the error in the failing test. I was not able to find a difference and I don't think this error will cause any troubles in production.
We might feel the pain when we add tests in the future 😅
But I think the simplified test is testing the necessary parts. Which is fine.
Thank you for checking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're referring to the docs-build hints, it's not a false positive, the character really is invisible. It should be a space or not a space. Easy to catch with a regex such as
\u2009if I remember well. I fixed most of those in another PR, so a rebase should do the trick.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. I had a test that was failing only in windows.
The error said that the output doesn't match the expected. But it was exactly the same.
Hence, @cotti suspected that there must be a weird space or something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah sorry, I actually misunderstood this haha :D