Skip to content
Merged

Icons #1413

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions docs/_docset.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project: 'doc-builder'

Check notice on line 1 in docs/_docset.yml

View workflow job for this annotation

GitHub Actions / build

Irregular whitespace character detected: U+2009 (Thin Space). This may impair Markdown rendering.

Check notice on line 1 in docs/_docset.yml

View workflow job for this annotation

GitHub Actions / build

Irregular whitespace character detected: U+2009 (Thin Space). This may impair Markdown rendering.

Check notice on line 1 in docs/_docset.yml

View workflow job for this annotation

GitHub Actions / build

Irregular whitespace character detected: U+2009 (Thin Space). This may impair Markdown rendering.

Check notice on line 1 in docs/_docset.yml

View workflow job for this annotation

GitHub Actions / build

Irregular whitespace character detected: U+2009 (Thin Space). This may impair Markdown rendering.

Check notice on line 1 in docs/_docset.yml

View workflow job for this annotation

GitHub Actions / build

Irregular whitespace character detected: U+2009 (Thin Space). This may impair Markdown rendering.

Check notice on line 1 in docs/_docset.yml

View workflow job for this annotation

GitHub Actions / build

Irregular whitespace character detected: U+2009 (Thin Space). This may impair Markdown rendering.

Check notice on line 1 in docs/_docset.yml

View workflow job for this annotation

GitHub Actions / build

Irregular whitespace character detected: U+2009 (Thin Space). This may impair Markdown rendering.

Check notice on line 1 in docs/_docset.yml

View workflow job for this annotation

GitHub Actions / build

Irregular whitespace character detected: U+2009 (Thin Space). This may impair Markdown rendering.

Check notice on line 1 in docs/_docset.yml

View workflow job for this annotation

GitHub Actions / build

Irregular whitespace character detected: U+2009 (Thin Space). This may impair Markdown rendering.

Check notice on line 1 in docs/_docset.yml

View workflow job for this annotation

GitHub Actions / build

Irregular whitespace character detected: U+2009 (Thin Space). This may impair Markdown rendering.
max_toc_depth: 2
# indicates this documentation set is not linkable by assembler.
# relaxes a few restrictions around toc building and file placement
Expand Down Expand Up @@ -85,6 +85,7 @@
- file: example_blocks.md
- file: file_inclusion.md
- file: frontmatter.md
- file: icons.md
- file: images.md
- file: lists.md
- file: line_breaks.md
Expand Down
580 changes: 580 additions & 0 deletions docs/syntax/icons.md

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/Elastic.Documentation.Site/Assets/markdown/icons.css
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;
}
}
}
}
1 change: 1 addition & 0 deletions src/Elastic.Documentation.Site/Assets/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@import './markdown/list.css';
@import './markdown/tabs.css';
@import './markdown/code.css';
@import './markdown/icons.css';
@import './copybutton.css';
@import './markdown/admonition.css';
@import './markdown/dropdown.css';
Expand Down
4 changes: 4 additions & 0 deletions src/Elastic.Markdown/Elastic.Markdown.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@
<ProjectReference Include="..\Elastic.Documentation.Configuration\Elastic.Documentation.Configuration.csproj" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Myst\Roles\Icons\svgs\*.svg" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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 Markdig.Parsers;
using Markdig.Syntax;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information

using System.Text.RegularExpressions;
using Elastic.Markdown.Myst.Roles.Icons;
using Markdig;
using Markdig.Helpers;
using Markdig.Parsers;
Expand Down Expand Up @@ -30,13 +31,17 @@ public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) { }

public class HeadingBlockWithSlugParser : HeadingBlockParser
{
private static readonly Regex IconSyntax = IconParser.IconRegex();

public override bool Close(BlockProcessor processor, Block block)
{
if (block is not HeadingBlock headerBlock)
if (block is not HeadingBlock headingBlock)
return base.Close(processor, block);

var text = headerBlock.Lines.Lines[0].Slice.AsSpan();
headerBlock.SetData("header", text.ToString());
var text = headingBlock.Lines.Lines[0].Slice.AsSpan();
// Remove icon syntax from the heading text
var cleanText = IconSyntax.Replace(text.ToString(), "").Trim();
headingBlock.SetData("header", cleanText);

if (!HeadingAnchorParser.MatchAnchorLine().IsMatch(text))
return base.Close(processor, block);
Expand All @@ -49,13 +54,13 @@ public override bool Close(BlockProcessor processor, Block block)
var anchor = text.Slice(match.Index, match.Length);

var newSlice = new StringSlice(header.ToString());
headerBlock.Lines.Lines[0] = new StringLine(ref newSlice);
headingBlock.Lines.Lines[0] = new StringLine(ref newSlice);

if (header.IndexOf('$') >= 0)
anchor = HeadingAnchorParser.MatchAnchor().Replace(anchor.ToString(), "");

headerBlock.SetData("anchor", anchor.ToString());
headerBlock.SetData("header", header.ToString());
headingBlock.SetData("anchor", anchor.ToString());
// Remove icon syntax from the header text when setting it as data
headingBlock.SetData("header", IconSyntax.Replace(header.ToString(), "").Trim());
return base.Close(processor, block);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Elastic.Markdown/Myst/MarkdownParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
using Elastic.Markdown.Myst.Linters;
using Elastic.Markdown.Myst.Renderers;
using Elastic.Markdown.Myst.Roles.AppliesTo;

using Elastic.Markdown.Myst.Roles.Icons;
using Markdig;
using Markdig.Extensions.EmphasisExtras;
using Markdig.Parsers;
Expand Down Expand Up @@ -146,6 +146,7 @@ public static MarkdownPipeline Pipeline
.UseHeadingsWithSlugs()
.UseEmphasisExtras(EmphasisExtraOptions.Default)
.UseInlineAppliesTo()
.UseInlineIcons()
.UseSubstitution()
.UseComments()
.UseYamlFrontMatter()
Expand Down
60 changes: 60 additions & 0 deletions src/Elastic.Markdown/Myst/Roles/Icons/IconRole.cs
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}";
}
40 changes: 40 additions & 0 deletions src/Elastic.Markdown/Myst/Roles/Icons/IconRoleRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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>
{

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());
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/Elastic.Markdown/Myst/Roles/Icons/svgs/aggregate.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/Elastic.Markdown/Myst/Roles/Icons/svgs/analyzeEvent.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/Elastic.Markdown/Myst/Roles/Icons/svgs/annotation.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/Elastic.Markdown/Myst/Roles/Icons/svgs/apm_trace.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/Elastic.Markdown/Myst/Roles/Icons/svgs/app_add_data.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/Elastic.Markdown/Myst/Roles/Icons/svgs/app_agent.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/Elastic.Markdown/Myst/Roles/Icons/svgs/app_apm.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading