diff --git a/src/Markdig.Tests/Specs/AbbreviationSpecs.md b/src/Markdig.Tests/Specs/AbbreviationSpecs.md index e2576dce..f65224da 100644 --- a/src/Markdig.Tests/Specs/AbbreviationSpecs.md +++ b/src/Markdig.Tests/Specs/AbbreviationSpecs.md @@ -57,3 +57,13 @@ We can abbreviate 1A, 1A1 and 1A2! .

We can abbreviate 1A, 1A1 and 1A2!

```````````````````````````````` + +Abbreviations should match whole word only: + +```````````````````````````````` example +*[1A]: First + +We should not abbreviate 1.1A or 11A! +. +

We should not abbreviate 1.1A or 11A!

+```````````````````````````````` diff --git a/src/Markdig.Tests/Specs/Specs.cs b/src/Markdig.Tests/Specs/Specs.cs index 9b487dcb..b9f88f88 100644 --- a/src/Markdig.Tests/Specs/Specs.cs +++ b/src/Markdig.Tests/Specs/Specs.cs @@ -18583,6 +18583,28 @@ namespace Markdig.Tests Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 5, "Extensions Abbreviation"); TestParser.TestSpec("*[1A]: First\n*[1A1]: Second\n*[1A2]: Third\n\nWe can abbreviate 1A, 1A1 and 1A2!", "

We can abbreviate 1A, 1A1 and 1A2!

", "abbreviations|advanced"); } + } + // Abbreviations should match whole word only: + [TestFixture] + public partial class TestExtensionsAbbreviation + { + [Test] + public void Example006() + { + // Example 6 + // Section: Extensions Abbreviation + // + // The following CommonMark: + // *[1A]: First + // + // We should not abbreviate 1.1A or 11A! + // + // Should be rendered as: + //

We should not abbreviate 1.1A or 11A!

+ + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 6, "Extensions Abbreviation"); + TestParser.TestSpec("*[1A]: First\n\nWe should not abbreviate 1.1A or 11A!", "

We should not abbreviate 1.1A or 11A!

", "abbreviations|advanced"); + } } // # Extensions // diff --git a/src/Markdig/Extensions/Abbreviations/AbbreviationParser.cs b/src/Markdig/Extensions/Abbreviations/AbbreviationParser.cs index eab4c798..61907265 100644 --- a/src/Markdig/Extensions/Abbreviations/AbbreviationParser.cs +++ b/src/Markdig/Extensions/Abbreviations/AbbreviationParser.cs @@ -101,20 +101,9 @@ namespace Markdig.Extensions.Abbreviations for (int i = content.Start; i < content.End; i++) { string match; - if (matcher.TryMatch(text, i, content.End - i + 1, out match)) + if (matcher.TryMatch(text, i, content.End - i + 1, out match) && IsValidAbbreviation(match, content, i)) { - // The word matched must be embraced by punctuation or whitespace or \0. - var c = content.PeekCharAbsolute(i - 1); - if (!(c == '\0' || c.IsAsciiPunctuation() || c.IsWhitespace())) - { - continue; - } var indexAfterMatch = i + match.Length; - c = content.PeekCharAbsolute(indexAfterMatch); - if (!(c == '\0' || c.IsAsciiPunctuation() || c.IsWhitespace())) - { - continue; - } // We should have a match, but in case... Abbreviation abbr; @@ -196,5 +185,39 @@ namespace Markdig.Extensions.Abbreviations } }; } + + private static bool IsValidAbbreviation(string match, StringSlice content, int matchIndex) + { + // The word matched must be embraced by punctuation or whitespace or \0. + var index = matchIndex - 1; + while (index > content.Start) + { + var c = content.PeekCharAbsolute(index); + if (!(c == '\0' || c.IsAsciiPunctuation() || c.IsWhitespace())) + { + return false; + } + if (!c.IsAsciiPunctuation()) + { + break; + } + index--; + } + index = matchIndex + match.Length; + while (index < content.End) + { + var c = content.PeekCharAbsolute(index); + if (!(c == '\0' || c.IsAsciiPunctuation() || c.IsWhitespace())) + { + return false; + } + if (!c.IsAsciiPunctuation()) + { + break; + } + index++; + } + return true; + } } } \ No newline at end of file