From 06eb6ba774b5f96a4446620f194d9d352b511346 Mon Sep 17 00:00:00 2001 From: Benni <> Date: Wed, 30 Aug 2023 00:40:10 +0200 Subject: [PATCH 1/3] Better content string slice of delimiter literals: instead of creating a new StringSlice only containing the delimiter chars, use the provided StringSlice from the match method with an appropriate start and end index --- src/Markdig.Tests/TestEmphasisPlus.cs | 18 +++++++++++++++++- .../Parsers/Inlines/EmphasisInlineParser.cs | 14 ++++++++------ .../Syntax/Inlines/EmphasisDelimiterInline.cs | 19 +++++++++++++++++-- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/Markdig.Tests/TestEmphasisPlus.cs b/src/Markdig.Tests/TestEmphasisPlus.cs index 9c2fdc776..fda75236b 100644 --- a/src/Markdig.Tests/TestEmphasisPlus.cs +++ b/src/Markdig.Tests/TestEmphasisPlus.cs @@ -1,7 +1,10 @@ // Copyright (c) Alexandre Mutel. All rights reserved. -// This file is licensed under the BSD-Clause 2 license. +// This file is licensed under the BSD-Clause 2 license. // See the license.txt file in the project root for more information. +using Markdig.Syntax; +using Markdig.Syntax.Inlines; + namespace Markdig.Tests; [TestFixture] @@ -18,4 +21,17 @@ public void NormalStrongNormal() { TestParser.TestSpec("normal ***Strong emphasis*** normal", "

normal Strong emphasis normal

", ""); } + + [Test] + public void OpenEmphasisHasConvenientContentStringSlice() + { + var pipeline = new MarkdownPipelineBuilder().Build(); + + var document = Markdown.Parse("test*test", pipeline); + + var emphasisDelimiterLiteral = (LiteralInline)((ParagraphBlock)document.LastChild).Inline.ElementAt(1); + Assert.That(emphasisDelimiterLiteral.Content.Text == "test*test"); + Assert.That(emphasisDelimiterLiteral.Content.Start == 4); + Assert.That(emphasisDelimiterLiteral.Content.End == 4); + } } \ No newline at end of file diff --git a/src/Markdig/Parsers/Inlines/EmphasisInlineParser.cs b/src/Markdig/Parsers/Inlines/EmphasisInlineParser.cs index 11915dba7..599aca3b3 100644 --- a/src/Markdig/Parsers/Inlines/EmphasisInlineParser.cs +++ b/src/Markdig/Parsers/Inlines/EmphasisInlineParser.cs @@ -1,5 +1,5 @@ // Copyright (c) Alexandre Mutel. All rights reserved. -// This file is licensed under the BSD-Clause 2 license. +// This file is licensed under the BSD-Clause 2 license. // See the license.txt file in the project root for more information. using System.Diagnostics; @@ -109,7 +109,7 @@ public bool PostProcess(InlineProcessor state, Inline? root, Inline? lastChild, var child = container.FirstChild; while (child != null) { - // Stop the search on the delimitation child + // Stop the search on the delimitation child if (child == lastChild) { break; @@ -197,7 +197,7 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice) if (canOpen) delimiterType |= DelimiterType.Open; if (canClose) delimiterType |= DelimiterType.Close; - var delimiter = new EmphasisDelimiterInline(this, emphasisDesc) + var delimiter = new EmphasisDelimiterInline(this, emphasisDesc, new StringSlice(slice.Text, startPosition, slice.Start - 1)) { DelimiterCount = delimiterCount, Type = delimiterType, @@ -221,7 +221,7 @@ private void ProcessEmphasis(InlineProcessor processor, List= openDelimiterIndex + 1; k--) { var literalDelimiter = delimiters[k]; - literalDelimiter.ReplaceBy(literalDelimiter.AsLiteralInline()); + literalDelimiter.ReplaceBy(literalDelimiter.AsLiteralInline()); delimiters.RemoveAt(k); i--; } diff --git a/src/Markdig/Syntax/Inlines/EmphasisDelimiterInline.cs b/src/Markdig/Syntax/Inlines/EmphasisDelimiterInline.cs index aa70896e3..063776567 100644 --- a/src/Markdig/Syntax/Inlines/EmphasisDelimiterInline.cs +++ b/src/Markdig/Syntax/Inlines/EmphasisDelimiterInline.cs @@ -1,5 +1,5 @@ // Copyright (c) Alexandre Mutel. All rights reserved. -// This file is licensed under the BSD-Clause 2 license. +// This file is licensed under the BSD-Clause 2 license. // See the license.txt file in the project root for more information. using Markdig.Helpers; @@ -29,6 +29,16 @@ public EmphasisDelimiterInline(InlineParser parser, EmphasisDescriptor descripto DelimiterChar = descriptor.Character; } + internal EmphasisDelimiterInline(InlineParser parser, EmphasisDescriptor descriptor, StringSlice content) : base(parser) + { + if (descriptor is null) + ThrowHelper.ArgumentNullException(nameof(descriptor)); + + Descriptor = descriptor; + DelimiterChar = descriptor.Character; + Content = content; + } + /// /// Gets the descriptor for this emphasis. /// @@ -44,6 +54,11 @@ public EmphasisDelimiterInline(InlineParser parser, EmphasisDescriptor descripto /// public int DelimiterCount { get; set; } + /// + /// The content as a . + /// + public StringSlice Content; + public override string ToLiteral() { return DelimiterCount > 0 ? new string(DelimiterChar, DelimiterCount) : string.Empty; @@ -53,7 +68,7 @@ public LiteralInline AsLiteralInline() { return new LiteralInline() { - Content = new StringSlice(ToLiteral()), + Content = Content, IsClosed = true, Span = Span, Line = Line, From 6d75eed3bb3ec7fd32ad539195a721e7726c896b Mon Sep 17 00:00:00 2001 From: Benni <> Date: Wed, 30 Aug 2023 00:53:05 +0200 Subject: [PATCH 2/3] Don't break external users of the public constructor --- src/Markdig/Syntax/Inlines/EmphasisDelimiterInline.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Markdig/Syntax/Inlines/EmphasisDelimiterInline.cs b/src/Markdig/Syntax/Inlines/EmphasisDelimiterInline.cs index 063776567..41cf2adb8 100644 --- a/src/Markdig/Syntax/Inlines/EmphasisDelimiterInline.cs +++ b/src/Markdig/Syntax/Inlines/EmphasisDelimiterInline.cs @@ -27,6 +27,7 @@ public EmphasisDelimiterInline(InlineParser parser, EmphasisDescriptor descripto Descriptor = descriptor; DelimiterChar = descriptor.Character; + Content = new StringSlice(ToLiteral()); } internal EmphasisDelimiterInline(InlineParser parser, EmphasisDescriptor descriptor, StringSlice content) : base(parser) From dba94a2371ffbb51dc8c0a68a89289d784b8fc1c Mon Sep 17 00:00:00 2001 From: Benni <> Date: Wed, 30 Aug 2023 00:55:35 +0200 Subject: [PATCH 3/3] Add documentation for the new constructor. --- src/Markdig/Syntax/Inlines/EmphasisDelimiterInline.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Markdig/Syntax/Inlines/EmphasisDelimiterInline.cs b/src/Markdig/Syntax/Inlines/EmphasisDelimiterInline.cs index 41cf2adb8..b472eb441 100644 --- a/src/Markdig/Syntax/Inlines/EmphasisDelimiterInline.cs +++ b/src/Markdig/Syntax/Inlines/EmphasisDelimiterInline.cs @@ -30,6 +30,13 @@ public EmphasisDelimiterInline(InlineParser parser, EmphasisDescriptor descripto Content = new StringSlice(ToLiteral()); } + /// + /// Initializes a new instance of the class. + /// + /// The parser. + /// The descriptor. + /// The content. + /// internal EmphasisDelimiterInline(InlineParser parser, EmphasisDescriptor descriptor, StringSlice content) : base(parser) { if (descriptor is null)