-
-
Notifications
You must be signed in to change notification settings - Fork 267
Add new BitMarkdownViewer component (#12503) #12506
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
Open
msynk
wants to merge
17
commits into
bitfoundation:develop
Choose a base branch
from
msynk:12503-blazorui-new-markdownviewer
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7ff908e
add new BitMarkdownViewer component #12503
msynk 85f8d6e
resolve review comments
msynk f6fc298
resolve review comments II
msynk fe5a238
resolve review comments III
msynk 09321c5
resolve review comments IV
msynk 4fdaa56
resolve review comments V
msynk 3ceff51
resolve review comments VI
msynk f6dc73f
rename namespaces and add missing css
msynk a5e2d8a
resolve review comments VII
msynk ae3c5b5
resolve review comments VIII
msynk 3a6f9b7
resolve review comments IX
msynk 8686fdf
apply renames
msynk d09011a
resolve review comments X
msynk b759e8d
resolve review comments XI
msynk 003e81a
resolve review comments XII
msynk bec5fe6
resolve review comments XIII
msynk 5a83849
resolve review comments XIV
msynk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/BlazorUI/Bit.BlazorUI.Extras/Components/MarkdownViewer/BitMarkdownViewer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| using Bit.BlazorUI.Markdown.Rendering; | ||
| using Bit.BlazorUI.Markdown.Syntax; | ||
|
|
||
| namespace Bit.BlazorUI; | ||
|
|
||
| /// <summary> | ||
| /// BitMarkdownViewer is a native, SEO friendly Blazor component that renders Markdown | ||
| /// to HTML entirely in C#. There is no JavaScript interop and no third-party packages. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// By default the component understands only the basic CommonMark core. Richer flavors | ||
| /// (GitHub tables, strikethrough, task lists, autolinks, emoji, ...) are opt-in: supply | ||
| /// a <see cref="Pipeline"/> built with the desired extensions (for example | ||
| /// <see cref="BitMarkdownPipelines.GitHub"/>). | ||
| /// </para> | ||
| /// <para> | ||
| /// Parsing produces an AST which is walked with a <see cref="Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder"/>, | ||
| /// so the output is real DOM rather than an <c>innerHTML</c> blob. Raw HTML in the source | ||
| /// is treated as text and link / image URLs are sanitized, keeping the output safe from | ||
| /// script injection by default. | ||
| /// </para> | ||
| /// </remarks> | ||
| public partial class BitMarkdownViewer : BitComponentBase | ||
| { | ||
| private DocumentNode _document = new(); | ||
| private string? _parsedSource; | ||
| private BitMarkdownPipeline? _parsedWith; | ||
|
|
||
|
|
||
|
|
||
| /// <summary> | ||
| /// The Markdown string value to render as html elements. | ||
| /// </summary> | ||
| [Parameter] public string? Markdown { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// The processing pipeline (flavor set). Defaults to <see cref="BitMarkdownPipeline.Basic"/>, | ||
| /// i.e. the basic CommonMark core with no extensions. | ||
| /// </summary> | ||
| [Parameter] public BitMarkdownPipeline? Pipeline { get; set; } | ||
|
|
||
|
|
||
|
|
||
| protected override string RootElementClass => "bit-mdv"; | ||
|
|
||
| private BitMarkdownPipeline EffectivePipeline => Pipeline ?? BitMarkdownPipeline.Basic; | ||
|
|
||
| protected override void OnParametersSet() | ||
| { | ||
| var pipeline = EffectivePipeline; | ||
|
|
||
| // Re-parse only when the source or the pipeline reference changes. | ||
| if (_parsedSource != Markdown || ReferenceEquals(_parsedWith, pipeline) is false) | ||
| { | ||
| _document = pipeline.Parse(Markdown); | ||
| _parsedSource = Markdown; | ||
| _parsedWith = pipeline; | ||
| } | ||
|
|
||
| base.OnParametersSet(); | ||
| } | ||
|
|
||
| protected override void BuildRenderTree(RenderTreeBuilder builder) | ||
| { | ||
| var renderer = EffectivePipeline.CreateRenderer(); | ||
|
|
||
| builder.OpenElement(renderer.NextSeq(), "div"); | ||
|
|
||
| builder.AddMultipleAttributes(renderer.NextSeq(), HtmlAttributes); | ||
| builder.AddAttribute(renderer.NextSeq(), "id", _Id); | ||
| builder.AddAttribute(renderer.NextSeq(), "style", StyleBuilder.Value); | ||
| builder.AddAttribute(renderer.NextSeq(), "class", ClassBuilder.Value); | ||
| if (Dir is not null) | ||
| { | ||
| builder.AddAttribute(renderer.NextSeq(), "dir", Dir.Value.ToString().ToLower()); | ||
| } | ||
| builder.AddElementReferenceCapture(renderer.NextSeq(), v => RootElement = v); | ||
|
|
||
| renderer.WriteNodes(builder, _document.Children); | ||
|
|
||
| builder.CloseElement(); | ||
| } | ||
| } | ||
11 changes: 0 additions & 11 deletions
11
src/BlazorUI/Bit.BlazorUI.Extras/Components/MarkdownViewer/BitMarkdownViewer.razor
This file was deleted.
Oops, something went wrong.
127 changes: 0 additions & 127 deletions
127
src/BlazorUI/Bit.BlazorUI.Extras/Components/MarkdownViewer/BitMarkdownViewer.razor.cs
This file was deleted.
Oops, something went wrong.
27 changes: 0 additions & 27 deletions
27
src/BlazorUI/Bit.BlazorUI.Extras/Components/MarkdownViewer/BitMarkdownViewer.ts
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
...rUI/Bit.BlazorUI.Extras/Components/MarkdownViewer/BitMarkdownViewerJsRuntimeExtensions.cs
This file was deleted.
Oops, something went wrong.
66 changes: 66 additions & 0 deletions
66
...zorUI/Bit.BlazorUI.Extras/Components/MarkdownViewer/Extensions/AutoIdentifierExtension.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| using System.Text; | ||
| using Bit.BlazorUI.Markdown.Parsing; | ||
| using Bit.BlazorUI.Markdown.Syntax; | ||
|
|
||
| namespace Bit.BlazorUI.Markdown.Extensions; | ||
|
|
||
| /// <summary> | ||
| /// Assigns a URL-friendly <c>id</c> (GitHub-style slug) to every heading, ensuring | ||
| /// uniqueness within the document so headings can be deep-linked. | ||
| /// </summary> | ||
| public sealed class AutoIdentifierAstProcessor : AstProcessor | ||
| { | ||
| public override void Process(DocumentNode document, BitMarkdownPipeline pipeline) | ||
| { | ||
| var used = new Dictionary<string, int>(); | ||
| foreach (var heading in AstHelper.Descendants(document).OfType<HeadingNode>()) | ||
| { | ||
| string baseSlug = Slugify(InlineHelpers.PlainText(heading.Inlines)); | ||
| if (baseSlug.Length == 0) baseSlug = "section"; | ||
|
|
||
| string slug = baseSlug; | ||
| if (used.TryGetValue(baseSlug, out int count)) | ||
| { | ||
| used[baseSlug] = ++count; | ||
| slug = $"{baseSlug}-{count}"; | ||
| } | ||
| else | ||
| { | ||
| used[baseSlug] = 0; | ||
| } | ||
| heading.Id = slug; | ||
| } | ||
| } | ||
|
|
||
| private static string Slugify(string text) | ||
| { | ||
| var sb = new StringBuilder(text.Length); | ||
| bool lastDash = false; | ||
| foreach (char c in text.Trim().ToLowerInvariant()) | ||
| { | ||
| if (char.IsLetterOrDigit(c)) | ||
| { | ||
| sb.Append(c); | ||
| lastDash = false; | ||
| } | ||
| else if (c is ' ' or '-' or '_') | ||
| { | ||
| if (!lastDash && sb.Length > 0) | ||
| { | ||
| sb.Append('-'); | ||
| lastDash = true; | ||
| } | ||
| } | ||
| // other punctuation is dropped | ||
| } | ||
| if (lastDash && sb.Length > 0) sb.Length--; | ||
| return sb.ToString(); | ||
| } | ||
| } | ||
|
|
||
| /// <summary>Enables automatic heading <c>id</c> slugs.</summary> | ||
| public sealed class AutoIdentifierExtension : IBitMarkdownExtension | ||
| { | ||
| public void Setup(BitMarkdownPipelineBuilder builder) | ||
| => builder.AstProcessors.Add(new AutoIdentifierAstProcessor()); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.