Skip to content
This repository was archived by the owner on Dec 21, 2023. It is now read-only.

Conversation

vedxyz
Copy link
Contributor

@vedxyz vedxyz commented Jun 6, 2022

Tested with this sample
                <codeblock>heyo</codeblock>
                <codeblock class="mt-2" language="csharp">
                    using System;
                    using System.Linq;
                    using System.Text.RegularExpressions;
                    using System.Threading.Tasks;
                    using Microsoft.AspNetCore.Html;
                    using Microsoft.AspNetCore.Mvc.Rendering;
                    using Microsoft.AspNetCore.Razor.TagHelpers;
                            
                    namespace OpenTabletDriver.Web.TagHelpers
                    {
                        [HtmlTargetElement("codeblock")]
                        public class CodeBlockTagHelper : TagHelper
                        {
                            public string Language { set; get; } = "plaintext";

                            public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
                            {
                                output.TagName = "pre";

                                var rawContent = (await output.GetChildContentAsync()).GetContent();
                                var body = TrimBaseIndentation(rawContent);

                                var code = new TagBuilder("code");
                                code.AddCssClass("hljs");
                                code.AddCssClass($"language-{Language}");
                                code.InnerHtml.SetHtmlContent($"{body}\n");

                                output.Content.SetHtmlContent(code);
                            }

                            private string TrimBaseIndentation(string content)
                            {
                                var endsTrimmedContent = TrimStartRegex.Replace(content.TrimEnd(), "$1", 1);
                                var lines = endsTrimmedContent.Split(Environment.NewLine);
                                var baseIndentationLength = CountIndentation(lines[0]);

                                for (int i = 0; i != lines.Length; i++)
                                {
                                    var line = lines[i];

                                    var indentationLength = CountIndentation(line);
                                    if (indentationLength < baseIndentationLength)
                                    {
                                        if (indentationLength == line.Length)
                                        {
                                            lines[i] = "";
                                            continue;
                                        }

                                        return endsTrimmedContent;
                                    }

                                    lines[i] = line.Substring(Math.Min(baseIndentationLength, line.Length));
                                }

                                return String.Join(Environment.NewLine, lines).Trim();
                            }
                            
                            static private Regex TrimStartRegex = new("^\\s*\n(\\s*)(?=\\S*)", RegexOptions.Compiled);

                            private int CountIndentation(string line) => line.TakeWhile(Char.IsWhiteSpace).Count();
                        }
                    }

                </codeblock>
                <codeblock class="mt-2" language="csharp">
                    
                    if (true) {
                        Console.WriteLine("Hey!");
                    }
                </codeblock>
                <codeblock class="mt-2" language="csharp">
                    if (true) {
                        Console.WriteLine("Hey!");
                    }
                    
                </codeblock>
                <codeblock class="mt-2" language="csharp">
                    
                    if (true) {
                        Console.WriteLine("Hey!");
                    }
                    
                </codeblock>
                <codeblock class="mt-2" language="csharp">
                    
                    
                    if (true) {
                        Console.WriteLine("Hey!");
                    }
                    
                    
                </codeblock>
                <codeblock class="mt-2" language="csharp">
                    if (true) {

                        Console.WriteLine("Hey!");

                    }
                </codeblock>
                <codeblock class="mt-2" language="csharp">
                    
                    if (true) {

                        Console.WriteLine("Hey!");

                    }
                    
                </codeblock>
                <codeblock class="mt-2" language="csharp">
                    if (true) {
                        Console.WriteLine("Hey!");
                    }
                </codeblock>
                <codeblock class="mt-2" language="csharp">if (true) {
                        Console.WriteLine("Hey!");
                    }
                </codeblock>
                <codeblock class="mt-2" language="csharp">
                    if (true) {
                        Console.WriteLine("Hey!");
                    }</codeblock>
                <codeblock class="mt-2" language="csharp">
                    if (true) {
                Console.WriteLine("Hey!");
                    }
                </codeblock>
                <codeblock class="mt-2" language="csharp">
                    if (true) {
                    Console.WriteLine("Hey!");
                    }
                </codeblock>
                <codeblock class="mt-2" language="csharp">
if (true) {
    Console.WriteLine("Hey!");
}
                </codeblock>
                <codeblock class="mt-2" language="csharp">
                    if (true) {
                        Console.WriteLine("Hey!");
                }
                </codeblock>
                <codeblock class="mt-2" language="csharp">
                if (true) {
                        Console.WriteLine("Hey!");
                    }
                </codeblock>
                <codeblock class="mt-2" language="csharp">
        if (true) {
                        Console.WriteLine("Hey!");
                    }
                </codeblock>
                <codeblock class="mt-2" language="csharp">if (true) { Console.WriteLine("Hey!"); }
                </codeblock>
                <codeblock class="mt-2" language="csharp">if (true) { Console.WriteLine("Hey!"); }</codeblock>

Also tested with existing codeblocks.
Appears to be working smoothly on all instances.

Changes

  • Add default language plaintext to the tag helper
  • Remove redundant addition of hljs class
  • Replace indentation trimming implementation

@vedxyz vedxyz requested a review from InfinityGhost June 6, 2022 01:26
@InfinityGhost InfinityGhost added enhancement New feature or request wiki Improvements or additions to wiki labels Jun 6, 2022
@vedxyz vedxyz force-pushed the codeblock-trimming branch from d4050ee to 0beb348 Compare June 8, 2022 10:25
@vedxyz vedxyz force-pushed the codeblock-trimming branch from 0beb348 to 53e14cc Compare June 8, 2022 10:28
@vedxyz
Copy link
Contributor Author

vedxyz commented Jun 8, 2022

Rebased to resolve conflicts and optimize CountIndentation

@X9VoiD X9VoiD mentioned this pull request Jun 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request wiki Improvements or additions to wiki
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants