Skip to content

Commit 97ede38

Browse files
authored
Add support for Myst inline role syntax with applies_to as first usecase (#875)
This PR adds support for Myst inline role syntax. The first `role` we support is `{applies_to}` and `{preview}``
1 parent a146524 commit 97ede38

35 files changed

+671
-213
lines changed

docs/syntax/applies.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,34 @@ stack: ga 9.1
154154

155155
This will allow the yaml inside the `{applies-to}` directive to be fully highlighted.
156156

157+
## Inline Applies To
158+
159+
Inline applies to can be placed anywhere using the following syntax
160+
161+
```markdown
162+
This can live inline {applies_to}`section: <life-cycle> [version]`
163+
```
164+
165+
An inline version example would be {applies_to}`stack: beta 9.1` this allows you to target elements more concretely visually.
166+
167+
A common use case would be to place them on definition lists:
168+
169+
Fruit {applies_to}`stack: preview 9.1`
170+
: A sweet and fleshy product of a tree or other plant that contains seed and can be eaten as food. Common examples include apples, oranges, and bananas. Most fruits are rich in vitamins, minerals and fiber.
171+
172+
Applies {preview}`9.1`
173+
: A sweet and fleshy product of a tree or other plant that contains seed and can be eaten as food. Common examples include apples, oranges, and bananas. Most fruits are rich in vitamins, minerals and fiber.
174+
175+
176+
A specialized `{preview}` role exist to quickly mark something as a technical preview. It takes a required version number
177+
as argument.
178+
179+
```markdown
180+
Property {preview}`<version>`
181+
: definition body
182+
```
183+
184+
157185

158186
## Examples
159187

@@ -162,6 +190,7 @@ This will allow the yaml inside the `{applies-to}` directive to be fully highlig
162190
stack: ga 9.1
163191
```
164192
193+
165194
#### Stack with deployment
166195
```yaml {applies_to}
167196
stack: ga 9.1

src/Elastic.Markdown/Assets/styles.css

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,19 @@
103103
@apply font-sans;
104104
border-bottom: 1px solid var(--color-grey-20);
105105
padding-bottom: calc(var(--spacing) * 3);
106+
font-variant: all-petite-caps;
106107

107-
.applies-to-label {
108-
display: block;
109-
font-size: 1.5em;
110-
font-weight: var(--font-weight-extrabold);
111-
padding-bottom: calc(var(--spacing) * 3);
108+
.applicable-meta-discontinued {
109+
color: var(--color-red-90);
112110
}
111+
.applicable-meta-coming {
112+
color: var(--color-blue-elastic-80);
113+
}
114+
.applicable-meta-technical-preview {
115+
color: var(--color-yellow-80);
116+
}
117+
118+
113119
.applicable-info {
114120
padding: calc(var(--spacing) * 0.5);
115121
padding-left: calc(var(--spacing) * 2);
@@ -121,6 +127,52 @@
121127
background-color: var(--color-white);
122128
border: 1px solid var(--color-grey-20);
123129
}
130+
.applicable-version {
131+
font-weight: bold;
132+
margin-left: calc(var(--spacing) * 0.5);
133+
font-variant: none;
134+
font-size: 0.87em;
135+
}
136+
.applicable-lifecycle {
137+
font-weight: bold;
138+
}
139+
}
140+
.applies-inline {
141+
@apply font-sans;
142+
font-variant: all-petite-caps;
143+
144+
.applicable-meta-discontinued {
145+
color: var(--color-red-90);
146+
}
147+
.applicable-meta-coming {
148+
color: var(--color-blue-elastic-80);
149+
}
150+
.applicable-meta-technical-preview {
151+
color: var(--color-blue-elastic-80);
152+
}
153+
154+
.applicable-info {
155+
padding: calc(var(--spacing) * 0.5);
156+
padding-left: calc(var(--spacing) * 2);
157+
padding-right: calc(var(--spacing) * 2);
158+
margin-left: calc(var(--spacing) * 0.5);
159+
margin-right: calc(var(--spacing) * 0.5);
160+
display: inline-block;
161+
font-size: 0.8em;
162+
border-radius: 0.4em;
163+
background-color: var(--color-white);
164+
border: 1px solid var(--color-grey-20);
165+
font-weight: normal;
166+
}
167+
.applicable-version {
168+
font-weight: bold;
169+
margin-left: calc(var(--spacing) * 0.5);
170+
font-variant: none;
171+
font-size: 0.87em;
172+
}
173+
.applicable-lifecycle {
174+
font-weight: bold;
175+
}
124176
}
125177
}
126178

src/Elastic.Markdown/Diagnostics/ProcessorDiagnosticExtensions.cs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace Elastic.Markdown.Diagnostics;
1212

1313
public static class ProcessorDiagnosticExtensions
1414
{
15+
private static string CreateExceptionMessage(string message, Exception? e) => message + (e != null ? Environment.NewLine + e : string.Empty);
16+
1517
public static void EmitError(this InlineProcessor processor, int line, int column, int length, string message)
1618
{
1719
var context = processor.GetContext();
@@ -55,7 +57,7 @@ public static void EmitError(this ParserContext context, string message, Excepti
5557
{
5658
Severity = Severity.Error,
5759
File = context.MarkdownSourcePath.FullName,
58-
Message = message + (e != null ? Environment.NewLine + e : string.Empty),
60+
Message = CreateExceptionMessage(message, e),
5961
};
6062
context.Build.Collector.Channel.Write(d);
6163
}
@@ -82,7 +84,7 @@ public static void EmitError(this BuildContext context, IFileInfo file, string m
8284
{
8385
Severity = Severity.Error,
8486
File = file.FullName,
85-
Message = message + (e != null ? Environment.NewLine + e : string.Empty),
87+
Message = CreateExceptionMessage(message, e),
8688
};
8789
context.Collector.Channel.Write(d);
8890
}
@@ -104,7 +106,7 @@ public static void EmitError(this DiagnosticsCollector collector, IFileInfo file
104106
{
105107
Severity = Severity.Error,
106108
File = file.FullName,
107-
Message = message + (e != null ? Environment.NewLine + e : string.Empty),
109+
Message = CreateExceptionMessage(message, e),
108110
};
109111
collector.Channel.Write(d);
110112
}
@@ -132,11 +134,12 @@ public static void EmitError(this IBlockExtension block, string message, Excepti
132134
Line = block.Line + 1,
133135
Column = block.Column,
134136
Length = block.OpeningLength + 5,
135-
Message = message + (e != null ? Environment.NewLine + e : string.Empty),
137+
Message = CreateExceptionMessage(message, e),
136138
};
137139
block.Build.Collector.Channel.Write(d);
138140
}
139141

142+
140143
public static void EmitWarning(this IBlockExtension block, string message)
141144
{
142145
if (block.SkipValidation)
@@ -154,12 +157,10 @@ public static void EmitWarning(this IBlockExtension block, string message)
154157
block.Build.Collector.Channel.Write(d);
155158
}
156159

157-
private static void LinkDiagnostic(InlineProcessor processor, Severity severity, LinkInline inline, string message)
160+
private static void LinkDiagnostic(InlineProcessor processor, Severity severity, Inline inline, int length, string message, Exception? e = null)
158161
{
159-
var url = inline.Url;
160162
var line = inline.Line + 1;
161163
var column = inline.Column;
162-
var length = url?.Length ?? 1;
163164

164165
var context = processor.GetContext();
165166
if (context.SkipValidation)
@@ -170,18 +171,27 @@ private static void LinkDiagnostic(InlineProcessor processor, Severity severity,
170171
File = processor.GetContext().MarkdownSourcePath.FullName,
171172
Column = Math.Max(column, 1),
172173
Line = line,
173-
Message = message,
174-
Length = length
174+
Message = CreateExceptionMessage(message, e),
175+
Length = Math.Max(length, 1)
175176
};
176177
context.Build.Collector.Channel.Write(d);
177178
}
178179

179180
public static void EmitError(this InlineProcessor processor, LinkInline inline, string message) =>
180-
LinkDiagnostic(processor, Severity.Error, inline, message);
181+
LinkDiagnostic(processor, Severity.Error, inline, inline.Url?.Length ?? 1, message);
181182

182183
public static void EmitWarning(this InlineProcessor processor, LinkInline inline, string message) =>
183-
LinkDiagnostic(processor, Severity.Warning, inline, message);
184+
LinkDiagnostic(processor, Severity.Warning, inline, inline.Url?.Length ?? 1, message);
184185

185186
public static void EmitHint(this InlineProcessor processor, LinkInline inline, string message) =>
186-
LinkDiagnostic(processor, Severity.Hint, inline, message);
187+
LinkDiagnostic(processor, Severity.Hint, inline, inline.Url?.Length ?? 1, message);
188+
189+
public static void EmitError(this InlineProcessor processor, Inline inline, int length, string message, Exception? e = null) =>
190+
LinkDiagnostic(processor, Severity.Error, inline, length, message, e);
191+
192+
public static void EmitWarning(this InlineProcessor processor, Inline inline, int length, string message) =>
193+
LinkDiagnostic(processor, Severity.Warning, inline, length, message);
194+
195+
public static void EmitHint(this InlineProcessor processor, Inline inline, int length, string message) =>
196+
LinkDiagnostic(processor, Severity.Hint, inline, length, message);
187197
}

src/Elastic.Markdown/Myst/CodeBlocks/EnhancedCodeBlock.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@
1010

1111
namespace Elastic.Markdown.Myst.CodeBlocks;
1212

13-
public class AppliesToDirective(BlockParser parser, ParserContext context)
14-
: EnhancedCodeBlock(parser, context)
15-
{
16-
public ApplicableTo? AppliesTo { get; set; }
17-
}
18-
1913
public class EnhancedCodeBlock(BlockParser parser, ParserContext context)
2014
: FencedCodeBlock(parser), IBlockExtension
2115
{

src/Elastic.Markdown/Myst/CodeBlocks/EnhancedCodeBlockHtmlRenderer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Diagnostics.CodeAnalysis;
66
using Elastic.Markdown.Diagnostics;
77
using Elastic.Markdown.Myst.Comments;
8+
using Elastic.Markdown.Myst.Directives;
89
using Elastic.Markdown.Slices.Directives;
910
using Markdig.Helpers;
1011
using Markdig.Renderers;
@@ -228,10 +229,10 @@ protected override void Write(HtmlRenderer renderer, EnhancedCodeBlock block)
228229
private static void RenderAppliesToHtml(HtmlRenderer renderer, AppliesToDirective appliesToDirective)
229230
{
230231
var appliesTo = appliesToDirective.AppliesTo;
231-
var slice2 = ApplicableTo.Create(appliesTo);
232+
var slice = ApplicableToDirective.Create(appliesTo);
232233
if (appliesTo is null || appliesTo == FrontMatter.ApplicableTo.All)
233234
return;
234-
var html = slice2.RenderAsync().GetAwaiter().GetResult();
235+
var html = slice.RenderAsync().GetAwaiter().GetResult();
235236
_ = renderer.Write(html);
236237
}
237238
}

src/Elastic.Markdown/Myst/CodeBlocks/EnhancedCodeBlockParser.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text.RegularExpressions;
66
using Elastic.Markdown.Diagnostics;
77
using Elastic.Markdown.Helpers;
8+
using Elastic.Markdown.Myst.Directives;
89
using Elastic.Markdown.Myst.FrontMatter;
910
using Markdig.Helpers;
1011
using Markdig.Parsers;

src/Elastic.Markdown/Myst/Directives/AppliesBlock.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using Elastic.Markdown.Myst.CodeBlocks;
6+
using Elastic.Markdown.Myst.FrontMatter;
7+
using Markdig.Parsers;
8+
9+
namespace Elastic.Markdown.Myst.Directives;
10+
11+
12+
public class AppliesToDirective(BlockParser parser, ParserContext context)
13+
: EnhancedCodeBlock(parser, context), IApplicableToElement
14+
{
15+
public ApplicableTo? AppliesTo { get; set; }
16+
}
17+

src/Elastic.Markdown/Myst/Directives/DirectiveBlockParser.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,6 @@ protected override DirectiveBlock CreateFencedBlock(BlockProcessor processor)
104104
if (info.IndexOf("{literalinclude}") > 0)
105105
return new LiteralIncludeBlock(this, context);
106106

107-
if (info.IndexOf("{applies}") > 0)
108-
return new AppliesBlock(this, context);
109-
110107
if (info.IndexOf("{settings}") > 0)
111108
return new SettingsBlock(this, context);
112109

src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
using System.Diagnostics.CodeAnalysis;
66
using Elastic.Markdown.Diagnostics;
7+
using Elastic.Markdown.Myst.InlineParsers.Substitution;
78
using Elastic.Markdown.Myst.Settings;
8-
using Elastic.Markdown.Myst.Substitution;
99
using Elastic.Markdown.Slices.Directives;
1010
using Markdig;
1111
using Markdig.Renderers;
@@ -32,8 +32,6 @@ protected override void Write(HtmlRenderer renderer, DirectiveBlock directiveBlo
3232
case MermaidBlock mermaidBlock:
3333
WriteMermaid(renderer, mermaidBlock);
3434
return;
35-
case AppliesBlock: //deprecated scheduled for removal
36-
return;
3735
case FigureBlock imageBlock:
3836
WriteFigure(renderer, imageBlock);
3937
return;

0 commit comments

Comments
 (0)