Skip to content
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

update CSS classes, attribute name refactoring #286

Merged
merged 6 commits into from
Nov 30, 2024
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions samples/MudBlazor.Markdown.Core/sample.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Some regular text. *Text in italics*, **text in bold** and ~~text in strikethrou
**Bold and *italics within***.
*Italics and **bold within***.
It is possible to `highlight some code`.
*~Invalid **or** unknown~ markdown is shown as it is*.
Please visit the [MudBlazor project site](https://mudblazor.com/).
***
> Quote someone.
Expand Down
10 changes: 5 additions & 5 deletions src/MudBlazor.Markdown/Components/MudLinkButton.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
{
var i = 0;

builder.OpenElement(i++, "span");
builder.AddAttribute(i++, "class", Classname);
builder.AddAttribute(i++, "style", Style);
builder.AddAttribute(i++, "onclick", EventCallback.Factory.Create(this, OnClick));
builder.OpenElement(i++, ElementNames.Span);
builder.AddAttribute(i++, AttributeNames.Class, Classname);
builder.AddAttribute(i++, AttributeNames.Style, Style);
builder.AddAttribute(i++, AttributeNames.OnClick, EventCallback.Factory.Create(this, OnClick));
builder.AddContent(i++, ChildContent);
builder.CloseElement();
}
Expand All @@ -73,4 +73,4 @@ private void OnClick()
if (Command?.CanExecute(CommandParameter) ?? false)
Command.Execute(CommandParameter);
}
}
}
8 changes: 3 additions & 5 deletions src/MudBlazor.Markdown/Components/MudMathJax.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ private static MathDelimiter GetDelimiter(in string delimiter) =>
{
"$" => new MathDelimiter("\\(", "\\)"),
"$$" => new MathDelimiter(delimiter),
_ => new MathDelimiter(delimiter)
_ => new MathDelimiter(delimiter),
};

private readonly ref struct MathDelimiter
{
public readonly string Start, End;

public MathDelimiter(string delimiter)
{
Start = End = delimiter;
Expand All @@ -56,9 +58,5 @@ public MathDelimiter(string start, string end)
Start = start;
End = end;
}

public string Start { get; }

public string End { get; }
}
}
11 changes: 11 additions & 0 deletions src/MudBlazor.Markdown/Enums/AttributeNames.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace MudBlazor;

internal static class AttributeNames
{
public const string OnClick = "onclick",
Class = "class",
Id = "id",
LinkRelation = "rel",
Style = "style",
Start = "start";
}
4 changes: 2 additions & 2 deletions src/MudBlazor.Markdown/Enums/CodeBlockTheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,5 +243,5 @@ public enum CodeBlockTheme : ushort
WindowsNtBase16,
WoodlandBase16,
XcodeDuskBase16,
ZenburnBase16
}
ZenburnBase16,
}
6 changes: 6 additions & 0 deletions src/MudBlazor.Markdown/Enums/ElementNames.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace MudBlazor;

internal static class ElementNames
{
public const string Span = "span";
}
4 changes: 2 additions & 2 deletions src/MudBlazor.Markdown/Extensions/CodeBlockThemeEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,6 @@ public static string GetStylesheetPath(this CodeBlockTheme @this) =>
CodeBlockTheme.WoodlandBase16 => "base16/woodland.min.css",
CodeBlockTheme.XcodeDuskBase16 => "base16/xcode-dusk.min.css",
CodeBlockTheme.ZenburnBase16 => "base16/zenburn.min.css",
_ => string.Empty
_ => string.Empty,
};
}
}
8 changes: 4 additions & 4 deletions src/MudBlazor.Markdown/Extensions/EmphasisInlineEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ public static bool TryGetEmphasisElement(this EmphasisInline emphasis, out strin
{
1 => italics,
2 => bold,
_ => string.Empty
_ => string.Empty,
},
'_' => italics,
'~' => emphasis.DelimiterCount switch
{
2 => strikethrough,
_ => string.Empty
_ => string.Empty,
},
_ => string.Empty
_ => string.Empty,
};

return !string.IsNullOrEmpty(value);
}
}
}
4 changes: 2 additions & 2 deletions src/MudBlazor.Markdown/Extensions/HeadingBlockEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private static string GetStringContent(this Inline @this)
var slice = @this switch
{
LiteralInline x => x.Content,
_ => StringSlice.Empty
_ => StringSlice.Empty,
};

return PrepareStringContent(slice.ToString());
Expand All @@ -42,4 +42,4 @@ private static string PrepareStringContent(this string @this)

return str;
}
}
}
18 changes: 9 additions & 9 deletions src/MudBlazor.Markdown/MudMarkdown.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
return;

builder.OpenElement(ElementIndex++, "article");
builder.AddAttribute(ElementIndex++, "class", "mud-markdown-body");
builder.AddAttribute(ElementIndex++, AttributeNames.Class, "mud-markdown-body");
RenderMarkdown(parsedText, builder);
builder.CloseElement();
}
Expand Down Expand Up @@ -209,7 +209,7 @@ protected virtual void RenderParagraphBlock(LeafBlock paragraph, RenderTreeBuild
builder.OpenComponent<MudText>(ElementIndex++);

if (!string.IsNullOrEmpty(id))
builder.AddAttribute(ElementIndex++, "id", id);
builder.AddAttribute(ElementIndex++, AttributeNames.Id, id);

builder.AddAttribute(ElementIndex++, nameof(MudText.Typo), typo);
builder.AddAttribute(ElementIndex++, nameof(MudText.ChildContent), (RenderFragment)(contentBuilder => RenderInlines(paragraph.Inline, contentBuilder)));
Expand Down Expand Up @@ -250,7 +250,7 @@ protected virtual void RenderInlines(ContainerInline inlines, RenderTreeBuilder
if (!x.TryGetEmphasisElement(out var elementName))
{
var markdownValue = x.Span.TryGetText(Value);
TryRenderMarkdownError(markdownValue, builder, htmlElement: "span");
TryRenderMarkdownError(markdownValue, builder, ElementNames.Span);
continue;
}

Expand Down Expand Up @@ -286,14 +286,14 @@ protected virtual void RenderInlines(ContainerInline inlines, RenderTreeBuilder
if (url.IsExternalUri(NavigationManager?.BaseUri))
{
builder.AddAttribute(ElementIndex++, nameof(MudLink.Target), "_blank");
builder.AddAttribute(ElementIndex++, "rel", "noopener noreferrer");
builder.AddAttribute(ElementIndex++, AttributeNames.LinkRelation, "noopener noreferrer");
}
// (prevent scrolling to the top of the page)
// custom implementation only for links on the same page
else if (url?.StartsWith('#') ?? false)
{
builder.AddEventPreventDefaultAttribute(ElementIndex++, "onclick", true);
builder.AddAttribute(ElementIndex++, "onclick", EventCallback.Factory.Create<MouseEventArgs>(this, () =>
builder.AddEventPreventDefaultAttribute(ElementIndex++, AttributeNames.OnClick, true);
builder.AddAttribute(ElementIndex++, AttributeNames.OnClick, EventCallback.Factory.Create<MouseEventArgs>(this, () =>
{
if (NavigationManager == null)
return;
Expand Down Expand Up @@ -351,7 +351,7 @@ protected virtual void TryRenderMarkdownError(string? text, RenderTreeBuilder bu
return;

builder.OpenElement(ElementIndex++, htmlElement);
builder.AddAttribute(ElementIndex++, "class", "markdown-error");
builder.AddAttribute(ElementIndex++, AttributeNames.Class, "mud-markdown-error");
builder.AddContent(ElementIndex++, text);
builder.CloseElement();
}
Expand Down Expand Up @@ -403,7 +403,7 @@ protected virtual void RenderTableRow(TableRow row, string cellElementName, Rend
builder.OpenElement(ElementIndex++, cellElementName);

if (minWidth is > 0)
builder.AddAttribute(ElementIndex++, "style", $"min-width:{minWidth}px");
builder.AddAttribute(ElementIndex++, AttributeNames.Style, $"min-width:{minWidth}px");

if (cell.Count != 0 && cell[0] is ParagraphBlock paragraphBlock)
RenderParagraphBlock(paragraphBlock, builder);
Expand All @@ -426,7 +426,7 @@ protected virtual void RenderList(ListBlock list, RenderTreeBuilder builder)

if (orderStart > 1)
{
builder.AddAttribute(ElementIndex++, "start", orderStart);
builder.AddAttribute(ElementIndex++, AttributeNames.Start, orderStart);
}

for (var i = 0; i < list.Count; i++)
Expand Down
2 changes: 1 addition & 1 deletion src/MudBlazor.Markdown/Resources/MudBlazor.Markdown.css
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pre code.hljs {
margin-bottom: 0 !important;
}

.mud-markdown-body p .markdown-error {
.mud-markdown-body p .mud-markdown-error {
white-space: pre;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ public void RenderTableWithWeirdFormat()
<article class='mud-markdown-body'>
<h3 id='my-table' class='mud-typography mud-typography-h3'>My Table:</h3>
<p class='mud-typography mud-typography-body1'>
<div class='markdown-error'>
<div class='mud-markdown-error'>
| **Column 1** | **Column 2** | **Column 3** |
|--------------|----------------|-----------------------------|
| Row 1, Col 1 | Row 1, Col 2 | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ Some text <code>code</code> again text - <i>italics</i> text and also <b>bold</b
[InlineData("==")] // marked
public void RenderInvalidEmphasis(string emphasisDelimiter)
{
string value = $"I expect that {emphasisDelimiter}emphasis{emphasisDelimiter} will be rendered as escaped. {emphasisDelimiter}**Nested markdown is also escaped**{emphasisDelimiter}.";
string expected =
var value = $"I expect that {emphasisDelimiter}emphasis{emphasisDelimiter} will be rendered as escaped. {emphasisDelimiter}**Nested markdown is also escaped**{emphasisDelimiter}.";
var expected =
$"""
<article class='mud-markdown-body'>
<p class="mud-typography mud-typography-body1">
I expect that <span class="markdown-error">{emphasisDelimiter}emphasis{emphasisDelimiter}</span> will be rendered as escaped. <span class="markdown-error">{emphasisDelimiter}**Nested markdown is also escaped**{emphasisDelimiter}</span>.
I expect that <span class="mud-markdown-error">{emphasisDelimiter}emphasis{emphasisDelimiter}</span> will be rendered as escaped. <span class="mud-markdown-error">{emphasisDelimiter}**Nested markdown is also escaped**{emphasisDelimiter}</span>.
</p>
</article>
""";
Expand Down
Loading