diff --git a/src/Markdig/Extensions/Abbreviations/AbbreviationParser.cs b/src/Markdig/Extensions/Abbreviations/AbbreviationParser.cs index 9c451eac7..863e882fb 100644 --- a/src/Markdig/Extensions/Abbreviations/AbbreviationParser.cs +++ b/src/Markdig/Extensions/Abbreviations/AbbreviationParser.cs @@ -20,7 +20,7 @@ public class AbbreviationParser : BlockParser /// public AbbreviationParser() { - OpeningCharacters = new[] { '*' }; + OpeningCharacters = ['*']; } public override BlockState TryOpen(BlockProcessor processor) diff --git a/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierExtension.cs b/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierExtension.cs index dfbf73ecf..00e81bbd9 100644 --- a/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierExtension.cs +++ b/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierExtension.cs @@ -2,8 +2,6 @@ // This file is licensed under the BSD-Clause 2 license. // See the license.txt file in the project root for more information. -using System.IO; - using Markdig.Helpers; using Markdig.Parsers; using Markdig.Renderers; diff --git a/src/Markdig/Extensions/AutoLinks/AutoLinkExtension.cs b/src/Markdig/Extensions/AutoLinks/AutoLinkExtension.cs index efb030a3c..d5d83cf56 100644 --- a/src/Markdig/Extensions/AutoLinks/AutoLinkExtension.cs +++ b/src/Markdig/Extensions/AutoLinks/AutoLinkExtension.cs @@ -11,14 +11,9 @@ namespace Markdig.Extensions.AutoLinks; /// Extension to automatically create when a link url http: or mailto: is found. /// /// -public class AutoLinkExtension : IMarkdownExtension +public class AutoLinkExtension(AutoLinkOptions? options) : IMarkdownExtension { - public readonly AutoLinkOptions Options; - - public AutoLinkExtension(AutoLinkOptions? options) - { - Options = options ?? new AutoLinkOptions(); - } + public readonly AutoLinkOptions Options = options ?? new AutoLinkOptions(); public void Setup(MarkdownPipelineBuilder pipeline) { diff --git a/src/Markdig/Extensions/AutoLinks/AutoLinkParser.cs b/src/Markdig/Extensions/AutoLinks/AutoLinkParser.cs index 64a84e45d..515c98826 100644 --- a/src/Markdig/Extensions/AutoLinks/AutoLinkParser.cs +++ b/src/Markdig/Extensions/AutoLinks/AutoLinkParser.cs @@ -22,14 +22,14 @@ public AutoLinkParser(AutoLinkOptions options) { Options = options ?? throw new ArgumentNullException(nameof(options)); - OpeningCharacters = new char[] - { + OpeningCharacters = + [ 'h', // for http:// and https:// 'f', // for ftp:// 'm', // for mailto: 't', // for tel: 'w', // for www. - }; + ]; _listOfCharCache = new ListOfCharCache(); } diff --git a/src/Markdig/Extensions/CustomContainers/CustomContainerParser.cs b/src/Markdig/Extensions/CustomContainers/CustomContainerParser.cs index f72038a08..2c0caeca4 100644 --- a/src/Markdig/Extensions/CustomContainers/CustomContainerParser.cs +++ b/src/Markdig/Extensions/CustomContainers/CustomContainerParser.cs @@ -17,7 +17,7 @@ public class CustomContainerParser : FencedBlockParserBase /// public CustomContainerParser() { - OpeningCharacters = new [] {':'}; + OpeningCharacters = [':']; // We don't need a prefix InfoPrefix = null; diff --git a/src/Markdig/Extensions/DefinitionLists/DefinitionListParser.cs b/src/Markdig/Extensions/DefinitionLists/DefinitionListParser.cs index 31d901db4..0985ed8df 100644 --- a/src/Markdig/Extensions/DefinitionLists/DefinitionListParser.cs +++ b/src/Markdig/Extensions/DefinitionLists/DefinitionListParser.cs @@ -18,7 +18,7 @@ public class DefinitionListParser : BlockParser /// public DefinitionListParser() { - OpeningCharacters = new [] {':', '~'}; + OpeningCharacters = [':', '~']; } public override BlockState TryOpen(BlockProcessor processor) diff --git a/src/Markdig/Extensions/Emoji/EmojiMapping.cs b/src/Markdig/Extensions/Emoji/EmojiMapping.cs index f20e1517a..08de98ee3 100644 --- a/src/Markdig/Extensions/Emoji/EmojiMapping.cs +++ b/src/Markdig/Extensions/Emoji/EmojiMapping.cs @@ -1786,6 +1786,6 @@ public EmojiMapping(IDictionary shortcodeToUnicode, IDictionary< ThrowHelper.ArgumentException(string.Format("Smiley {0} is already present in the emoji mapping", smiley.Key)); } - OpeningCharacters = new List(firstChars).ToArray(); + OpeningCharacters = [.. firstChars]; } } \ No newline at end of file diff --git a/src/Markdig/Extensions/Figures/FigureBlockParser.cs b/src/Markdig/Extensions/Figures/FigureBlockParser.cs index 1ad28bed4..744969e8b 100644 --- a/src/Markdig/Extensions/Figures/FigureBlockParser.cs +++ b/src/Markdig/Extensions/Figures/FigureBlockParser.cs @@ -18,7 +18,7 @@ public class FigureBlockParser : BlockParser /// public FigureBlockParser() { - OpeningCharacters = new[] { '^' }; + OpeningCharacters = ['^']; } public override BlockState TryOpen(BlockProcessor processor) diff --git a/src/Markdig/Extensions/Footers/FooterBlockParser.cs b/src/Markdig/Extensions/Footers/FooterBlockParser.cs index 8ab204445..ab6023e2f 100644 --- a/src/Markdig/Extensions/Footers/FooterBlockParser.cs +++ b/src/Markdig/Extensions/Footers/FooterBlockParser.cs @@ -19,7 +19,7 @@ public class FooterBlockParser : BlockParser /// public FooterBlockParser() { - OpeningCharacters = new[] {'^'}; + OpeningCharacters = ['^']; } public override BlockState TryOpen(BlockProcessor processor) diff --git a/src/Markdig/Extensions/Footnotes/FootnoteParser.cs b/src/Markdig/Extensions/Footnotes/FootnoteParser.cs index c393cb42b..6d184bdee 100644 --- a/src/Markdig/Extensions/Footnotes/FootnoteParser.cs +++ b/src/Markdig/Extensions/Footnotes/FootnoteParser.cs @@ -22,7 +22,7 @@ public class FootnoteParser : BlockParser public FootnoteParser() { - OpeningCharacters = new [] {'['}; + OpeningCharacters = ['[']; } public override BlockState TryOpen(BlockProcessor processor) @@ -49,7 +49,7 @@ private BlockState TryOpen(BlockProcessor processor, bool isContinue) // Advance the column int deltaColumn = processor.Start - start; - processor.Column = processor.Column + deltaColumn; + processor.Column += deltaColumn; processor.NextChar(); // Skip ':' @@ -170,10 +170,8 @@ private void Document_ProcessInlinesEnd(InlineProcessor state, Inline? inline) paragraphBlock = new ParagraphBlock(); footnote.Add(paragraphBlock); } - if (paragraphBlock.Inline == null) - { - paragraphBlock.Inline = new ContainerInline(); - } + + paragraphBlock.Inline ??= new ContainerInline(); foreach (var link in footnote.Links) { diff --git a/src/Markdig/Extensions/GenericAttributes/GenericAttributesParser.cs b/src/Markdig/Extensions/GenericAttributes/GenericAttributesParser.cs index e719335f8..ae34e21dd 100644 --- a/src/Markdig/Extensions/GenericAttributes/GenericAttributesParser.cs +++ b/src/Markdig/Extensions/GenericAttributes/GenericAttributesParser.cs @@ -23,7 +23,7 @@ public class GenericAttributesParser : InlineParser /// public GenericAttributesParser() { - OpeningCharacters = new[] { '{' }; + OpeningCharacters = ['{']; } public override bool Match(InlineProcessor processor, ref StringSlice slice) @@ -136,10 +136,7 @@ public static bool TryParse(ref StringSlice slice, [NotNullWhen(true)] out HtmlA var text = slice.Text.Substring(start, end - start + 1); if (isClass) { - if (classes is null) - { - classes = new List(); - } + classes ??= new List(); classes.Add(text); } else diff --git a/src/Markdig/Extensions/Mathematics/MathBlockParser.cs b/src/Markdig/Extensions/Mathematics/MathBlockParser.cs index 5b6061b13..590ba044b 100644 --- a/src/Markdig/Extensions/Mathematics/MathBlockParser.cs +++ b/src/Markdig/Extensions/Mathematics/MathBlockParser.cs @@ -20,7 +20,7 @@ public class MathBlockParser : FencedBlockParserBase /// public MathBlockParser() { - OpeningCharacters = new [] {'$'}; + OpeningCharacters = ['$']; // We expect to match only a $$, no less, no more MinimumMatchCount = 2; MaximumMatchCount = 2; diff --git a/src/Markdig/Extensions/Mathematics/MathInlineParser.cs b/src/Markdig/Extensions/Mathematics/MathInlineParser.cs index 13700bfb8..89f434bae 100644 --- a/src/Markdig/Extensions/Mathematics/MathInlineParser.cs +++ b/src/Markdig/Extensions/Mathematics/MathInlineParser.cs @@ -21,7 +21,7 @@ public class MathInlineParser : InlineParser /// public MathInlineParser() { - OpeningCharacters = new[] { '$' }; + OpeningCharacters = ['$']; DefaultClass = "math"; } diff --git a/src/Markdig/Extensions/MediaLinks/HostProviderBuilder.cs b/src/Markdig/Extensions/MediaLinks/HostProviderBuilder.cs index a95f5e429..419f38d22 100644 --- a/src/Markdig/Extensions/MediaLinks/HostProviderBuilder.cs +++ b/src/Markdig/Extensions/MediaLinks/HostProviderBuilder.cs @@ -11,23 +11,19 @@ namespace Markdig.Extensions.MediaLinks; public class HostProviderBuilder { - private sealed class DelegateProvider : IHostProvider + private sealed class DelegateProvider( + string hostPrefix, + Func handler, + bool allowFullscreen = true, + string? className = null) : IHostProvider { - public DelegateProvider(string hostPrefix, Func handler, bool allowFullscreen = true, string? className = null) - { - HostPrefix = hostPrefix; - Delegate = handler; - AllowFullScreen = allowFullscreen; - Class = className; - } - - public string HostPrefix { get; } + public string HostPrefix { get; } = hostPrefix; - public Func Delegate { get; } + public Func Delegate { get; } = handler; - public bool AllowFullScreen { get; } + public bool AllowFullScreen { get; } = allowFullscreen; - public string? Class { get; } + public string? Class { get; } = className; public bool TryHandle(Uri mediaUri, bool isSchemaRelative, [NotNullWhen(true)] out string? iframeUrl) { @@ -71,7 +67,7 @@ public static IHostProvider Create(string hostPrefix, Func handler #region Known providers - private static readonly string[] SplitAnd = { "&" }; + private static readonly string[] SplitAnd = ["&"]; private static string[] SplitQuery(Uri uri) { var query = uri.Query.Substring(uri.Query.IndexOf('?') + 1); diff --git a/src/Markdig/Extensions/NoRefLinks/NoFollowLinksExtension.cs b/src/Markdig/Extensions/NoRefLinks/NoFollowLinksExtension.cs index 9c7c1b7b2..c8e9c1084 100644 --- a/src/Markdig/Extensions/NoRefLinks/NoFollowLinksExtension.cs +++ b/src/Markdig/Extensions/NoRefLinks/NoFollowLinksExtension.cs @@ -17,7 +17,7 @@ public class NoFollowLinksExtension : IMarkdownExtension public NoFollowLinksExtension() { - _referralLinksExtension = new ReferralLinksExtension(new[] { "nofollow" }); + _referralLinksExtension = new ReferralLinksExtension(["nofollow"]); } public void Setup(MarkdownPipelineBuilder pipeline) diff --git a/src/Markdig/Extensions/SmartyPants/SmartyPantsInlineParser.cs b/src/Markdig/Extensions/SmartyPants/SmartyPantsInlineParser.cs index 641aa804c..97a716683 100644 --- a/src/Markdig/Extensions/SmartyPants/SmartyPantsInlineParser.cs +++ b/src/Markdig/Extensions/SmartyPants/SmartyPantsInlineParser.cs @@ -19,7 +19,7 @@ public class SmartyPantsInlineParser : InlineParser, IPostInlineProcessor /// public SmartyPantsInlineParser() { - OpeningCharacters = new[] {'\'', '"', '<', '>', '.', '-'}; + OpeningCharacters = ['\'', '"', '<', '>', '.', '-']; } public override bool Match(InlineProcessor processor, ref StringSlice slice) diff --git a/src/Markdig/Extensions/Tables/GridTableParser.cs b/src/Markdig/Extensions/Tables/GridTableParser.cs index ae6048eb7..173dc014e 100644 --- a/src/Markdig/Extensions/Tables/GridTableParser.cs +++ b/src/Markdig/Extensions/Tables/GridTableParser.cs @@ -12,7 +12,7 @@ public class GridTableParser : BlockParser { public GridTableParser() { - OpeningCharacters = new[] { '+' }; + OpeningCharacters = ['+']; } public override BlockState TryOpen(BlockProcessor processor) @@ -144,7 +144,7 @@ private static void SetRowSpanState(List columns, St line.Start = lineStart + columnSlice.Start + 1; line.End = lineStart + columnSlice.End - 1; line.Trim(); - if (line.IsEmptyOrWhitespace() || !IsRowSeperator(line)) + if (line.IsEmptyOrWhitespace() || !IsRowSeparator(line)) { hasRowSpan = true; columnSlice.CurrentCell.RowSpan++; @@ -158,7 +158,7 @@ private static void SetRowSpanState(List columns, St } } - private static bool IsRowSeperator(StringSlice slice) + private static bool IsRowSeparator(StringSlice slice) { char c = slice.CurrentChar; do @@ -263,7 +263,7 @@ private BlockState HandleContents(BlockProcessor processor, GridTableState table } sliceForCell.TrimEnd(); - if (!isRowLine || !IsRowSeperator(sliceForCell)) + if (!isRowLine || !IsRowSeparator(sliceForCell)) { if (columnSlice.CurrentCell is null) { @@ -273,10 +273,7 @@ private BlockState HandleContents(BlockProcessor processor, GridTableState table ColumnIndex = i }; - if (columnSlice.BlockProcessor is null) - { - columnSlice.BlockProcessor = processor.CreateChild(); - } + columnSlice.BlockProcessor ??= processor.CreateChild(); // Ensure that the BlockParser is aware that the TableCell is the top-level container columnSlice.BlockProcessor.Open(columnSlice.CurrentCell); diff --git a/src/Markdig/Extensions/Tables/GridTableState.cs b/src/Markdig/Extensions/Tables/GridTableState.cs index bb1f5f1ee..a4cfd6d66 100644 --- a/src/Markdig/Extensions/Tables/GridTableState.cs +++ b/src/Markdig/Extensions/Tables/GridTableState.cs @@ -10,21 +10,15 @@ namespace Markdig.Extensions.Tables; /// /// Internal state used by the /// -internal sealed class GridTableState +internal sealed class GridTableState(int start, bool expectRow) { - public GridTableState(int start, bool expectRow) - { - Start = start; - ExpectRow = expectRow; - } - - public int Start { get; } + public int Start { get; } = start; public StringLineGroup Lines; public List? ColumnSlices { get; private set; } - public bool ExpectRow { get; } + public bool ExpectRow { get; } = expectRow; public int StartRowGroup { get; set; } @@ -45,26 +39,18 @@ public void AddColumn(int start, int end, TableColumnAlign? align) ColumnSlices.Add(new ColumnSlice(start, end, align)); } - public sealed class ColumnSlice + public sealed class ColumnSlice(int start, int end, TableColumnAlign? align) { - public ColumnSlice(int start, int end, TableColumnAlign? align) - { - Start = start; - End = end; - Align = align; - CurrentColumnSpan = -1; - } - /// /// Gets or sets the index position of this column (after the |) /// - public int Start { get; } + public int Start { get; } = start; - public int End { get; } + public int End { get; } = end; - public TableColumnAlign? Align { get; } + public TableColumnAlign? Align { get; } = align; - public int CurrentColumnSpan { get; set; } + public int CurrentColumnSpan { get; set; } = -1; public int PreviousColumnSpan { get; set; } diff --git a/src/Markdig/Extensions/Tables/PipeTableBlockParser.cs b/src/Markdig/Extensions/Tables/PipeTableBlockParser.cs index 74b75ff91..3b7509220 100644 --- a/src/Markdig/Extensions/Tables/PipeTableBlockParser.cs +++ b/src/Markdig/Extensions/Tables/PipeTableBlockParser.cs @@ -22,7 +22,7 @@ public class PipeTableBlockParser : BlockParser /// public PipeTableBlockParser() { - OpeningCharacters = new[] {'-'}; + OpeningCharacters = ['-']; } public override BlockState TryOpen(BlockProcessor processor) diff --git a/src/Markdig/Extensions/Tables/PipeTableParser.cs b/src/Markdig/Extensions/Tables/PipeTableParser.cs index eb380768f..87bd4249d 100644 --- a/src/Markdig/Extensions/Tables/PipeTableParser.cs +++ b/src/Markdig/Extensions/Tables/PipeTableParser.cs @@ -25,12 +25,12 @@ public class PipeTableParser : InlineParser, IPostInlineProcessor /// /// Initializes a new instance of the class. /// - /// The linebreak parser to use + /// The line break parser to use /// The options. public PipeTableParser(LineBreakInlineParser lineBreakParser, PipeTableOptions? options = null) { this.lineBreakParser = lineBreakParser ?? throw new ArgumentNullException(nameof(lineBreakParser)); - OpeningCharacters = new[] { '|', '\n', '\r' }; + OpeningCharacters = ['|', '\n', '\r']; Options = options ?? new PipeTableOptions(); } @@ -637,10 +637,10 @@ private sealed class TableState public int LineIndex { get; set; } - public List ColumnAndLineDelimiters { get; } = new(); + public List ColumnAndLineDelimiters { get; } = []; - public List Cells { get; } = new(); + public List Cells { get; } = []; - public List EndOfLines { get; } = new(); + public List EndOfLines { get; } = []; } } \ No newline at end of file diff --git a/src/Markdig/Extensions/TaskLists/TaskListInlineParser.cs b/src/Markdig/Extensions/TaskLists/TaskListInlineParser.cs index a4fe4c164..33aeb1ba7 100644 --- a/src/Markdig/Extensions/TaskLists/TaskListInlineParser.cs +++ b/src/Markdig/Extensions/TaskLists/TaskListInlineParser.cs @@ -19,7 +19,7 @@ public class TaskListInlineParser : InlineParser /// public TaskListInlineParser() { - OpeningCharacters = new[] {'['}; + OpeningCharacters = ['[']; ListClass = "contains-task-list"; ListItemClass = "task-list-item"; } diff --git a/src/Markdig/Extensions/Yaml/YamlFrontMatterParser.cs b/src/Markdig/Extensions/Yaml/YamlFrontMatterParser.cs index c1cfddd80..f93635f87 100644 --- a/src/Markdig/Extensions/Yaml/YamlFrontMatterParser.cs +++ b/src/Markdig/Extensions/Yaml/YamlFrontMatterParser.cs @@ -26,7 +26,7 @@ public class YamlFrontMatterParser : BlockParser /// public YamlFrontMatterParser() { - this.OpeningCharacters = new[] { '-' }; + OpeningCharacters = ['-']; } /// diff --git a/src/Markdig/Helpers/BlockWrapper.cs b/src/Markdig/Helpers/BlockWrapper.cs index b5290fef2..b5ecd8967 100644 --- a/src/Markdig/Helpers/BlockWrapper.cs +++ b/src/Markdig/Helpers/BlockWrapper.cs @@ -7,14 +7,9 @@ namespace Markdig.Helpers; // Used to avoid the overhead of type covariance checks -internal readonly struct BlockWrapper : IEquatable +internal readonly struct BlockWrapper(Block block) : IEquatable { - public readonly Block Block; - - public BlockWrapper(Block block) - { - Block = block; - } + public readonly Block Block = block; public static implicit operator Block(BlockWrapper wrapper) => wrapper.Block; diff --git a/src/Markdig/Helpers/CharHelper.cs b/src/Markdig/Helpers/CharHelper.cs index 5d121bcc1..7e80bedba 100644 --- a/src/Markdig/Helpers/CharHelper.cs +++ b/src/Markdig/Helpers/CharHelper.cs @@ -722,11 +722,11 @@ public static bool IsLeftToRight(int c) } // Used by ListExtraItemParser to format numbers from 1 - 26 - private static readonly string[] smallNumberStringCache = { + private static readonly string[] smallNumberStringCache = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", - }; + ]; internal static string SmallNumberToString(int number) { diff --git a/src/Markdig/Helpers/CharacterMap.cs b/src/Markdig/Helpers/CharacterMap.cs index 33064bfc3..7980c9730 100644 --- a/src/Markdig/Helpers/CharacterMap.cs +++ b/src/Markdig/Helpers/CharacterMap.cs @@ -35,7 +35,7 @@ public CharacterMap(IEnumerable> maps) charSet.Add(map.Key); } - OpeningCharacters = charSet.ToArray(); + OpeningCharacters = [.. charSet]; Array.Sort(OpeningCharacters); _asciiMap = new T[128]; diff --git a/src/Markdig/Helpers/HtmlHelper.cs b/src/Markdig/Helpers/HtmlHelper.cs index 90cecf3ce..e3722df12 100644 --- a/src/Markdig/Helpers/HtmlHelper.cs +++ b/src/Markdig/Helpers/HtmlHelper.cs @@ -12,8 +12,8 @@ namespace Markdig.Helpers; /// public static class HtmlHelper { - private static readonly char[] SearchBackAndAmp = { '\\', '&' }; - private static readonly char[] SearchAmp = { '&' }; + private static readonly char[] SearchBackAndAmp = ['\\', '&']; + private static readonly char[] SearchAmp = ['&']; private static readonly string[] EscapeUrlsForAscii = new string[128]; static HtmlHelper() diff --git a/src/Markdig/Helpers/StringLineGroup.cs b/src/Markdig/Helpers/StringLineGroup.cs index 817ae6e15..b3ea4f551 100644 --- a/src/Markdig/Helpers/StringLineGroup.cs +++ b/src/Markdig/Helpers/StringLineGroup.cs @@ -187,16 +187,10 @@ public void Trim() } } - public struct Enumerator : IEnumerator + public struct Enumerator(StringLineGroup parent) : IEnumerator { - private readonly StringLineGroup _parent; - private int _index; - - public Enumerator(StringLineGroup parent) - { - _parent = parent; - _index = -1; - } + private readonly StringLineGroup _parent = parent; + private int _index = -1; public object Current => _parent.Lines[_index]; @@ -452,25 +446,21 @@ public bool TrimStart() } } - public readonly struct LineOffset + public readonly struct LineOffset( + int linePosition, + int column, + int offset, + int start, + int end) { - public LineOffset(int linePosition, int column, int offset, int start, int end) - { - LinePosition = linePosition; - Column = column; - Offset = offset; - Start = start; - End = end; - } - - public readonly int LinePosition; + public readonly int LinePosition = linePosition; - public readonly int Column; + public readonly int Column = column; - public readonly int Offset; + public readonly int Offset = offset; - public readonly int Start; + public readonly int Start = start; - public readonly int End; + public readonly int End = end; } } diff --git a/src/Markdig/MarkdownPipeline.cs b/src/Markdig/MarkdownPipeline.cs index 995d72c54..d96675005 100644 --- a/src/Markdig/MarkdownPipeline.cs +++ b/src/Markdig/MarkdownPipeline.cs @@ -95,18 +95,14 @@ internal RentedHtmlRenderer RentHtmlRenderer(TextWriter? writer = null) return new RentedHtmlRenderer(cache, renderer); } - internal sealed class HtmlRendererCache : ObjectCache + internal sealed class HtmlRendererCache( + MarkdownPipeline pipeline, + bool customWriter = false) : ObjectCache { - private static readonly TextWriter s_dummyWriter = new FastStringWriter(); + private static readonly FastStringWriter s_dummyWriter = new(); - private readonly MarkdownPipeline _pipeline; - private readonly bool _customWriter; - - public HtmlRendererCache(MarkdownPipeline pipeline, bool customWriter = false) - { - _pipeline = pipeline; - _customWriter = customWriter; - } + private readonly MarkdownPipeline _pipeline = pipeline; + private readonly bool _customWriter = customWriter; protected override HtmlRenderer NewInstance() { diff --git a/src/Markdig/MarkdownPipelineBuilder.cs b/src/Markdig/MarkdownPipelineBuilder.cs index 8f527f682..165365d82 100644 --- a/src/Markdig/MarkdownPipelineBuilder.cs +++ b/src/Markdig/MarkdownPipelineBuilder.cs @@ -24,8 +24,8 @@ public class MarkdownPipelineBuilder public MarkdownPipelineBuilder() { // Add all default parsers - BlockParsers = new OrderedList() - { + BlockParsers = + [ new ThematicBreakParser(), new HeadingBlockParser(), new QuoteBlockParser(), @@ -35,10 +35,10 @@ public MarkdownPipelineBuilder() new FencedCodeBlockParser(), new IndentedCodeBlockParser(), new ParagraphBlockParser(), - }; + ]; - InlineParsers = new OrderedList() - { + InlineParsers = + [ new HtmlEntityParser(), new LinkInlineParser(), new EscapeInlineParser(), @@ -46,7 +46,7 @@ public MarkdownPipelineBuilder() new CodeInlineParser(), new AutolinkInlineParser(), new LineBreakInlineParser(), - }; + ]; Extensions = new OrderedList(); } diff --git a/src/Markdig/Parsers/BlockProcessor.cs b/src/Markdig/Parsers/BlockProcessor.cs index fe0493d3f..225ed971e 100644 --- a/src/Markdig/Parsers/BlockProcessor.cs +++ b/src/Markdig/Parsers/BlockProcessor.cs @@ -152,7 +152,7 @@ public Block? NextContinue /// /// Gets the current stack of being processed. /// - private List OpenedBlocks { get; } = new(); + private List OpenedBlocks { get; } = []; private bool ContinueProcessingLine { get; set; } diff --git a/src/Markdig/Parsers/FencedCodeBlockParser.cs b/src/Markdig/Parsers/FencedCodeBlockParser.cs index 706f52a76..50d8b91d8 100644 --- a/src/Markdig/Parsers/FencedCodeBlockParser.cs +++ b/src/Markdig/Parsers/FencedCodeBlockParser.cs @@ -20,7 +20,7 @@ public class FencedCodeBlockParser : FencedBlockParserBase /// public FencedCodeBlockParser() { - OpeningCharacters = new[] {'`', '~'}; + OpeningCharacters = ['`', '~']; InfoPrefix = DefaultInfoPrefix; } diff --git a/src/Markdig/Parsers/HeadingBlockParser.cs b/src/Markdig/Parsers/HeadingBlockParser.cs index bf00e685e..9f99e539b 100644 --- a/src/Markdig/Parsers/HeadingBlockParser.cs +++ b/src/Markdig/Parsers/HeadingBlockParser.cs @@ -20,7 +20,7 @@ public class HeadingBlockParser : BlockParser, IAttributesParseable /// public HeadingBlockParser() { - OpeningCharacters = new[] {'#'}; + OpeningCharacters = ['#']; } /// diff --git a/src/Markdig/Parsers/HtmlBlockParser.cs b/src/Markdig/Parsers/HtmlBlockParser.cs index 978441456..0afe86484 100644 --- a/src/Markdig/Parsers/HtmlBlockParser.cs +++ b/src/Markdig/Parsers/HtmlBlockParser.cs @@ -18,7 +18,7 @@ public class HtmlBlockParser : BlockParser /// public HtmlBlockParser() { - OpeningCharacters = new[] { '<' }; + OpeningCharacters = ['<']; } public override BlockState TryOpen(BlockProcessor processor) diff --git a/src/Markdig/Parsers/InlineProcessor.cs b/src/Markdig/Parsers/InlineProcessor.cs index 2806f1bb5..8309dc03e 100644 --- a/src/Markdig/Parsers/InlineProcessor.cs +++ b/src/Markdig/Parsers/InlineProcessor.cs @@ -26,7 +26,7 @@ namespace Markdig.Parsers; /// public class InlineProcessor { - private readonly List lineOffsets = new(); + private readonly List lineOffsets = []; private int previousSliceOffset; private int previousLineIndexForSliceOffset; diff --git a/src/Markdig/Parsers/Inlines/AutolinkInlineParser.cs b/src/Markdig/Parsers/Inlines/AutolinkInlineParser.cs index 79ce9f6fd..0fd369c25 100644 --- a/src/Markdig/Parsers/Inlines/AutolinkInlineParser.cs +++ b/src/Markdig/Parsers/Inlines/AutolinkInlineParser.cs @@ -19,7 +19,7 @@ public class AutolinkInlineParser : InlineParser /// public AutolinkInlineParser() { - OpeningCharacters = new[] {'<'}; + OpeningCharacters = ['<']; EnableHtmlParsing = true; } diff --git a/src/Markdig/Parsers/Inlines/CodeInlineParser.cs b/src/Markdig/Parsers/Inlines/CodeInlineParser.cs index 588bc5c4c..25cbad2d3 100644 --- a/src/Markdig/Parsers/Inlines/CodeInlineParser.cs +++ b/src/Markdig/Parsers/Inlines/CodeInlineParser.cs @@ -21,7 +21,7 @@ public class CodeInlineParser : InlineParser /// public CodeInlineParser() { - OpeningCharacters = new[] { '`' }; + OpeningCharacters = ['`']; } public override bool Match(InlineProcessor processor, ref StringSlice slice) diff --git a/src/Markdig/Parsers/Inlines/EmphasisInlineParser.cs b/src/Markdig/Parsers/Inlines/EmphasisInlineParser.cs index 599aca3b3..4fc6268c4 100644 --- a/src/Markdig/Parsers/Inlines/EmphasisInlineParser.cs +++ b/src/Markdig/Parsers/Inlines/EmphasisInlineParser.cs @@ -20,7 +20,7 @@ namespace Markdig.Parsers.Inlines; public class EmphasisInlineParser : InlineParser, IPostInlineProcessor { private CharacterMap? emphasisMap; - private readonly DelimitersObjectCache inlinesCache = new DelimitersObjectCache(); + private readonly DelimitersObjectCache inlinesCache = new(); [Obsolete("Use TryCreateEmphasisInlineDelegate instead", error: false)] public delegate EmphasisInline CreateEmphasisInlineDelegate(char emphasisChar, bool isStrong); @@ -31,11 +31,11 @@ public class EmphasisInlineParser : InlineParser, IPostInlineProcessor /// public EmphasisInlineParser() { - EmphasisDescriptors = new List() - { + EmphasisDescriptors = + [ new EmphasisDescriptor('*', 1, 2, true), new EmphasisDescriptor('_', 1, 2, false) - }; + ]; } /// @@ -65,7 +65,7 @@ public bool HasEmphasisChar(char c) /// [Obsolete("Use TryCreateEmphasisInlineList instead", error: false)] public CreateEmphasisInlineDelegate? CreateEmphasisInline { get; set; } - public readonly List TryCreateEmphasisInlineList = new List(); + public readonly List TryCreateEmphasisInlineList = []; public override void Initialize() { diff --git a/src/Markdig/Parsers/Inlines/EscapeInlineParser.cs b/src/Markdig/Parsers/Inlines/EscapeInlineParser.cs index 665bacd43..122b2c971 100644 --- a/src/Markdig/Parsers/Inlines/EscapeInlineParser.cs +++ b/src/Markdig/Parsers/Inlines/EscapeInlineParser.cs @@ -15,7 +15,7 @@ public class EscapeInlineParser : InlineParser { public EscapeInlineParser() { - OpeningCharacters = new[] {'\\'}; + OpeningCharacters = ['\\']; } public override bool Match(InlineProcessor processor, ref StringSlice slice) diff --git a/src/Markdig/Parsers/Inlines/HtmlEntityParser.cs b/src/Markdig/Parsers/Inlines/HtmlEntityParser.cs index a52f4a358..1ea30a487 100644 --- a/src/Markdig/Parsers/Inlines/HtmlEntityParser.cs +++ b/src/Markdig/Parsers/Inlines/HtmlEntityParser.cs @@ -21,7 +21,7 @@ public class HtmlEntityParser : InlineParser /// public HtmlEntityParser() { - OpeningCharacters = new[] {'&'}; + OpeningCharacters = ['&']; } public static bool TryParse(ref StringSlice slice, [NotNullWhen(true)] out string? literal, out int match) diff --git a/src/Markdig/Parsers/Inlines/LineBreakInlineParser.cs b/src/Markdig/Parsers/Inlines/LineBreakInlineParser.cs index 6bfdd9ffe..8f6c4d437 100644 --- a/src/Markdig/Parsers/Inlines/LineBreakInlineParser.cs +++ b/src/Markdig/Parsers/Inlines/LineBreakInlineParser.cs @@ -18,7 +18,7 @@ public class LineBreakInlineParser : InlineParser /// public LineBreakInlineParser() { - OpeningCharacters = new[] { '\n', '\r' }; + OpeningCharacters = ['\n', '\r']; } /// diff --git a/src/Markdig/Parsers/Inlines/LinkInlineParser.cs b/src/Markdig/Parsers/Inlines/LinkInlineParser.cs index 211f552f8..0caa1ef46 100644 --- a/src/Markdig/Parsers/Inlines/LinkInlineParser.cs +++ b/src/Markdig/Parsers/Inlines/LinkInlineParser.cs @@ -19,7 +19,7 @@ public class LinkInlineParser : InlineParser /// public LinkInlineParser() { - OpeningCharacters = new[] {'[', ']', '!'}; + OpeningCharacters = ['[', ']', '!']; } public override bool Match(InlineProcessor processor, ref StringSlice slice) @@ -322,7 +322,7 @@ private bool TryProcessLinkOrImage(InlineProcessor inlineState, ref StringSlice if (label != null || LinkHelper.TryParseLabelTrivia(ref text, true, out label, out labelSpan)) { - SourceSpan labelWithTrivia = new SourceSpan(labelSpan.Start, labelSpan.End); + var labelWithTrivia = new SourceSpan(labelSpan.Start, labelSpan.End); if (isLabelSpanLocal) { labelSpan = inlineState.GetSourcePositionFromLocalSpan(labelSpan); diff --git a/src/Markdig/Parsers/MarkdownParser.cs b/src/Markdig/Parsers/MarkdownParser.cs index 851c9d222..206bcc1b9 100644 --- a/src/Markdig/Parsers/MarkdownParser.cs +++ b/src/Markdig/Parsers/MarkdownParser.cs @@ -210,16 +210,10 @@ private static void ProcessInlines(InlineProcessor inlineProcessor, MarkdownDocu } } - private struct ContainerItem + private struct ContainerItem(ContainerBlock container) { - public ContainerItem(ContainerBlock container) - { - Container = container; - Index = 0; - } - - public readonly ContainerBlock Container; + public readonly ContainerBlock Container = container; - public int Index; + public int Index = 0; } } \ No newline at end of file diff --git a/src/Markdig/Parsers/OrderedListItemParser.cs b/src/Markdig/Parsers/OrderedListItemParser.cs index 8bf1e7626..2afc85b20 100644 --- a/src/Markdig/Parsers/OrderedListItemParser.cs +++ b/src/Markdig/Parsers/OrderedListItemParser.cs @@ -15,7 +15,7 @@ public abstract class OrderedListItemParser : ListItemParser /// protected OrderedListItemParser() { - OrderedDelimiters = new[] { '.', ')' }; + OrderedDelimiters = ['.', ')']; } /// diff --git a/src/Markdig/Parsers/QuoteBlockParser.cs b/src/Markdig/Parsers/QuoteBlockParser.cs index 7cca8c99c..f30a969a9 100644 --- a/src/Markdig/Parsers/QuoteBlockParser.cs +++ b/src/Markdig/Parsers/QuoteBlockParser.cs @@ -18,7 +18,7 @@ public class QuoteBlockParser : BlockParser /// public QuoteBlockParser() { - OpeningCharacters = new[] {'>'}; + OpeningCharacters = ['>']; } public override BlockState TryOpen(BlockProcessor processor) diff --git a/src/Markdig/Parsers/ThematicBreakParser.cs b/src/Markdig/Parsers/ThematicBreakParser.cs index 343329029..5108a33b6 100644 --- a/src/Markdig/Parsers/ThematicBreakParser.cs +++ b/src/Markdig/Parsers/ThematicBreakParser.cs @@ -16,14 +16,14 @@ public class ThematicBreakParser : BlockParser /// /// A singleton instance used by other parsers. /// - public static readonly ThematicBreakParser Default = new ThematicBreakParser(); + public static readonly ThematicBreakParser Default = new(); /// /// Initializes a new instance of the class. /// public ThematicBreakParser() { - OpeningCharacters = new[] {'-', '_', '*'}; + OpeningCharacters = ['-', '_', '*']; } public override BlockState TryOpen(BlockProcessor processor) diff --git a/src/Markdig/Parsers/UnorderedListItemParser.cs b/src/Markdig/Parsers/UnorderedListItemParser.cs index 73cf85d6f..702854b74 100644 --- a/src/Markdig/Parsers/UnorderedListItemParser.cs +++ b/src/Markdig/Parsers/UnorderedListItemParser.cs @@ -15,7 +15,7 @@ public class UnorderedListItemParser : ListItemParser /// public UnorderedListItemParser() { - OpeningCharacters = new [] {'-', '+', '*'}; + OpeningCharacters = ['-', '+', '*']; } public override bool TryParse(BlockProcessor state, char pendingBulletType, out ListInfo result) diff --git a/src/Markdig/Polyfills/NullableAttributes.cs b/src/Markdig/Polyfills/NullableAttributes.cs index 81baed28e..c65100332 100644 --- a/src/Markdig/Polyfills/NullableAttributes.cs +++ b/src/Markdig/Polyfills/NullableAttributes.cs @@ -23,7 +23,7 @@ internal sealed class AllowNullAttribute : Attribute { } #if !NET5_0_OR_GREATER internal sealed class MemberNotNullAttribute : Attribute { - public MemberNotNullAttribute(string member) => Members = new[] { member }; + public MemberNotNullAttribute(string member) => Members = [member]; public MemberNotNullAttribute(params string[] members) => Members = members; diff --git a/src/Markdig/Renderers/Html/HeadingRenderer.cs b/src/Markdig/Renderers/Html/HeadingRenderer.cs index e676bc081..8a285bb29 100644 --- a/src/Markdig/Renderers/Html/HeadingRenderer.cs +++ b/src/Markdig/Renderers/Html/HeadingRenderer.cs @@ -12,14 +12,14 @@ namespace Markdig.Renderers.Html; /// public class HeadingRenderer : HtmlObjectRenderer { - private static readonly string[] HeadingTexts = { + private static readonly string[] HeadingTexts = [ "h1", "h2", "h3", "h4", "h5", "h6", - }; + ]; protected override void Write(HtmlRenderer renderer, HeadingBlock obj) { diff --git a/src/Markdig/Renderers/HtmlRenderer.cs b/src/Markdig/Renderers/HtmlRenderer.cs index 27b5d3616..085a4e971 100644 --- a/src/Markdig/Renderers/HtmlRenderer.cs +++ b/src/Markdig/Renderers/HtmlRenderer.cs @@ -144,6 +144,8 @@ public HtmlRenderer WriteEscape(string content, int offset, int length, bool sof return this; } + private static readonly SearchValues s_escapedChars = SearchValues.Create("<>&\""); + /// /// Writes the content escaped for HTML. /// @@ -159,7 +161,7 @@ public void WriteEscape(ReadOnlySpan content, bool softEscape = false) { int indexOfCharToEscape = softEscape ? content.IndexOfAny('<', '&') - : content.IndexOfAny("<>&\""); + : content.IndexOfAny(s_escapedChars); if ((uint)indexOfCharToEscape >= (uint)content.Length) { diff --git a/src/Markdig/Renderers/Normalize/HeadingRenderer.cs b/src/Markdig/Renderers/Normalize/HeadingRenderer.cs index 8155eddd6..657df0046 100644 --- a/src/Markdig/Renderers/Normalize/HeadingRenderer.cs +++ b/src/Markdig/Renderers/Normalize/HeadingRenderer.cs @@ -12,14 +12,14 @@ namespace Markdig.Renderers.Normalize; /// public class HeadingRenderer : NormalizeObjectRenderer { - private static readonly string[] HeadingTexts = { + private static readonly string[] HeadingTexts = [ "#", "##", "###", "####", "#####", "######", - }; + ]; protected override void Write(NormalizeRenderer renderer, HeadingBlock obj) { diff --git a/src/Markdig/Renderers/Roundtrip/HeadingRenderer.cs b/src/Markdig/Renderers/Roundtrip/HeadingRenderer.cs index 4bef06872..8471330a1 100644 --- a/src/Markdig/Renderers/Roundtrip/HeadingRenderer.cs +++ b/src/Markdig/Renderers/Roundtrip/HeadingRenderer.cs @@ -12,14 +12,14 @@ namespace Markdig.Renderers.Roundtrip; /// public class HeadingRenderer : RoundtripObjectRenderer { - private static readonly string[] HeadingTexts = { + private static readonly string[] HeadingTexts = [ "#", "##", "###", "####", "#####", "######", - }; + ]; protected override void Write(RoundtripRenderer renderer, HeadingBlock obj) { diff --git a/src/Markdig/Syntax/CodeBlock.cs b/src/Markdig/Syntax/CodeBlock.cs index 2d82bd061..97dfa8ee9 100644 --- a/src/Markdig/Syntax/CodeBlock.cs +++ b/src/Markdig/Syntax/CodeBlock.cs @@ -21,7 +21,7 @@ public class CodeBlockLine } private List? _codeBlockLines; - public List CodeBlockLines => _codeBlockLines ??= new(); + public List CodeBlockLines => _codeBlockLines ??= []; /// /// Initializes a new instance of the class. diff --git a/src/Markdig/Syntax/ContainerBlock.cs b/src/Markdig/Syntax/ContainerBlock.cs index 11a9430d7..fb5e1595d 100644 --- a/src/Markdig/Syntax/ContainerBlock.cs +++ b/src/Markdig/Syntax/ContainerBlock.cs @@ -27,7 +27,7 @@ public abstract class ContainerBlock : Block, IList, IReadOnlyList /// The parser used to create this block. protected ContainerBlock(BlockParser? parser) : base(parser) { - _children = Array.Empty(); + _children = []; SetTypeKind(isInline: false, isContainer: true); } @@ -301,14 +301,9 @@ void IEnumerator.Reset() #endregion - private sealed class BlockComparisonWrapper : IComparer + private sealed class BlockComparisonWrapper(Comparison comparison) : IComparer { - private readonly Comparison _comparison; - - public BlockComparisonWrapper(Comparison comparison) - { - _comparison = comparison; - } + private readonly Comparison _comparison = comparison; public int Compare(BlockWrapper x, BlockWrapper y) { @@ -316,14 +311,9 @@ public int Compare(BlockWrapper x, BlockWrapper y) } } - private sealed class BlockComparerWrapper : IComparer + private sealed class BlockComparerWrapper(IComparer comparer) : IComparer { - private readonly IComparer _comparer; - - public BlockComparerWrapper(IComparer comparer) - { - _comparer = comparer; - } + private readonly IComparer _comparer = comparer; public int Compare(BlockWrapper x, BlockWrapper y) { diff --git a/src/Markdig/Syntax/MarkdownObject.cs b/src/Markdig/Syntax/MarkdownObject.cs index da5bfa050..5b8a6bd5f 100644 --- a/src/Markdig/Syntax/MarkdownObject.cs +++ b/src/Markdig/Syntax/MarkdownObject.cs @@ -155,16 +155,10 @@ public string ToPositionText() private class DataEntriesAndTrivia { - private struct DataEntry + private struct DataEntry(object key, object value) { - public readonly object Key; - public object Value; - - public DataEntry(object key, object value) - { - Key = key; - Value = value; - } + public readonly object Key = key; + public object Value = value; } private DataEntry[]? _entries; diff --git a/src/Markdig/Syntax/QuoteBlock.cs b/src/Markdig/Syntax/QuoteBlock.cs index 6a1234679..30379aae2 100644 --- a/src/Markdig/Syntax/QuoteBlock.cs +++ b/src/Markdig/Syntax/QuoteBlock.cs @@ -42,7 +42,7 @@ public QuoteBlock(BlockParser parser) : base(parser) public class QuoteBlockLine { /// - /// Gets or sets trivia occuring before the first quote character. + /// Gets or sets trivia occurring before the first quote character. /// public StringSlice TriviaBefore { get; set; } diff --git a/src/Markdig/Syntax/SourceSpan.cs b/src/Markdig/Syntax/SourceSpan.cs index c63702d24..01cae3888 100644 --- a/src/Markdig/Syntax/SourceSpan.cs +++ b/src/Markdig/Syntax/SourceSpan.cs @@ -37,26 +37,26 @@ public SourceSpan(int start, int end) /// /// Gets the character length of this element within the original source code. /// - public int Length => End - Start + 1; + public readonly int Length => End - Start + 1; - public bool IsEmpty => Start > End; + public readonly bool IsEmpty => Start > End; public SourceSpan MoveForward(int count) { return new SourceSpan(Start + count, End + count); } - public bool Equals(SourceSpan other) + public readonly bool Equals(SourceSpan other) { return Start == other.Start && End == other.End; } - public override bool Equals(object? obj) + public override readonly bool Equals(object? obj) { return obj is SourceSpan sourceSpan && Equals(sourceSpan); } - public override int GetHashCode() + public override readonly int GetHashCode() { unchecked { @@ -74,7 +74,7 @@ public override int GetHashCode() return !left.Equals(right); } - public override string ToString() + public override readonly string ToString() { return $"{Start}-{End}"; }