From 3897e875eee24c3fc4538f9b71d4de5ae1d8df1d Mon Sep 17 00:00:00 2001 From: Alexandre Mutel Date: Sun, 15 Oct 2017 23:29:02 +0200 Subject: [PATCH] Add KeepOpeningDigits and DiscardDots options to AutoIdentifiers to match GitHub behavior --- .../AutoIdentifiers/AutoIdentifierExtension.cs | 9 +++++++-- .../AutoIdentifiers/AutoIdentifierOptions.cs | 17 ++++++++++++++++- src/Markdig/Helpers/LinkHelper.cs | 16 ++++++++-------- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierExtension.cs b/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierExtension.cs index 22fa75f1..e2134795 100644 --- a/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierExtension.cs +++ b/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierExtension.cs @@ -1,4 +1,4 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// Copyright (c) Alexandre Mutel. All rights reserved. // This file is licensed under the BSD-Clause 2 license. // See the license.txt file in the project root for more information. @@ -143,7 +143,12 @@ namespace Markdig.Extensions.AutoIdentifiers stripRenderer.Render(headingBlock.Inline); var headingText = headingWriter.ToString(); headingWriter.GetStringBuilder().Length = 0; - headingText = LinkHelper.Urilize(headingText, (options & AutoIdentifierOptions.AllowOnlyAscii) != 0); + + // TODO: Should we have a struct with more configure optionss for LinkHelper.Urilize? + headingText = LinkHelper.Urilize(headingText, + (options & AutoIdentifierOptions.AllowOnlyAscii) != 0, + (options & AutoIdentifierOptions.KeepOpeningDigits) != 0, + (options & AutoIdentifierOptions.DiscardDots) != 0); var baseHeadingId = string.IsNullOrEmpty(headingText) ? "section" : headingText; int index = 0; diff --git a/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierOptions.cs b/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierOptions.cs index 86d1b105..dde55ecc 100644 --- a/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierOptions.cs +++ b/src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierOptions.cs @@ -1,4 +1,4 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// Copyright (c) Alexandre Mutel. All rights reserved. // This file is licensed under the BSD-Clause 2 license. // See the license.txt file in the project root for more information. using System; @@ -21,6 +21,11 @@ namespace Markdig.Extensions.AutoIdentifiers /// Default = AutoLink | AllowOnlyAscii, + /// + /// Renders auto identifiers like GitHub. + /// + GitHub = Default | KeepOpeningDigits | DiscardDots, + /// /// Allows to link to a header by using the same text as the header for the link label. Default is true /// @@ -30,5 +35,15 @@ namespace Markdig.Extensions.AutoIdentifiers /// Allows only ASCII characters in the url (HTML 5 allows to have UTF8 characters). Default is true /// AllowOnlyAscii = 2, + + /// + /// Allows to keep digits starting a heading (by default, it keeps only characters starting from the first letter) + /// + KeepOpeningDigits = 4, + + /// + /// Discard dots when computing an identifier. + /// + DiscardDots = 8 } } \ No newline at end of file diff --git a/src/Markdig/Helpers/LinkHelper.cs b/src/Markdig/Helpers/LinkHelper.cs index 04014e72..875cf8c7 100644 --- a/src/Markdig/Helpers/LinkHelper.cs +++ b/src/Markdig/Helpers/LinkHelper.cs @@ -1,4 +1,4 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// Copyright (c) Alexandre Mutel. All rights reserved. // This file is licensed under the BSD-Clause 2 license. // See the license.txt file in the project root for more information. using System; @@ -18,10 +18,10 @@ namespace Markdig.Helpers return TryParseAutolink(ref text, out link, out isEmail); } - public static string Urilize(string headingText, bool allowOnlyAscii) + public static string Urilize(string headingText, bool allowOnlyAscii, bool keepOpeningDigits = false, bool discardDots = false) { var headingBuffer = StringBuilderCache.Local(); - bool hasLetter = false; + bool hasLetter = keepOpeningDigits && headingText.Length > 0 && char.IsLetterOrDigit(headingText[0]); bool previousIsSpace = false; for (int i = 0; i < headingText.Length; i++) { @@ -47,7 +47,7 @@ namespace Markdig.Helpers } else if (hasLetter) { - if (IsReservedPunctuation(c)) + if (IsReservedPunctuation(c, discardDots)) { if (previousIsSpace) { @@ -67,7 +67,7 @@ namespace Markdig.Helpers else if (!previousIsSpace && c.IsWhitespace()) { var pc = headingBuffer[headingBuffer.Length - 1]; - if (!IsReservedPunctuation(pc)) + if (!IsReservedPunctuation(pc, discardDots)) { headingBuffer.Append('-'); } @@ -81,7 +81,7 @@ namespace Markdig.Helpers while (headingBuffer.Length > 0) { var c = headingBuffer[headingBuffer.Length - 1]; - if (IsReservedPunctuation(c)) + if (IsReservedPunctuation(c, false)) { headingBuffer.Length--; } @@ -97,9 +97,9 @@ namespace Markdig.Helpers } [MethodImpl(MethodImplOptionPortable.AggressiveInlining)] - private static bool IsReservedPunctuation(char c) + private static bool IsReservedPunctuation(char c, bool discardDots) { - return c == '_' || c == '-' || c == '.'; + return c == '_' || c == '-' || (!discardDots && c == '.'); } public static bool TryParseAutolink(ref StringSlice text, out string link, out bool isEmail)