From 97ce0da564437cf23a73e12dfe1e65d9d5ebe5ef Mon Sep 17 00:00:00 2001 From: Alexandre Mutel Date: Thu, 18 May 2017 10:33:16 +0200 Subject: [PATCH] Update JiraLinks, allow `(` and `)`, use StringSlice instead of strings for key/issue, add support for attributes, add tests --- src/Markdig.Tests/Specs/JiraLinks.md | 75 ++++++- src/Markdig.Tests/Specs/Specs.cs | 192 ++++++++++++++++++ src/Markdig.Tests/Specs/Specs.tt | 1 + src/Markdig.Tests/TestParser.cs | 12 +- src/Markdig/Extensions/JiraLinks/JiraLink.cs | 11 +- .../Extensions/JiraLinks/JiraLinkExtension.cs | 7 +- .../JiraLinks/JiraLinkInlineParser.cs | 43 ++-- .../Extensions/JiraLinks/JiraLinkOptions.cs | 40 +++- .../Extensions/JiraLinks/JiraLinkRenderer.cs | 9 +- 9 files changed, 342 insertions(+), 48 deletions(-) diff --git a/src/Markdig.Tests/Specs/JiraLinks.md b/src/Markdig.Tests/Specs/JiraLinks.md index f8ef6215..a618e96b 100644 --- a/src/Markdig.Tests/Specs/JiraLinks.md +++ b/src/Markdig.Tests/Specs/JiraLinks.md @@ -4,21 +4,74 @@ The JiraLinks extension will automatically add links to JIRA issue items within ``` var pipeline = new MarkdownPipelineBuilder() - .UseJiraLinks(new JiraLinkOptions() - { - Url = "https://company.atlassian.net" - }) + .UseJiraLinks(new JiraLinkOptions("http://your.company.abc")) .Build(); ``` -The project key must be capitalised for the link to be detected, it can be any length. The following are valid examples. +The rules for detecting a link are: -* EG-123 -* TEST-123 -* DIG-123 +- The project key must be composed of onre or more capitalised ASCII letter `[A-Z]+` +- A single hypen `-` must separate the project key and issue number. +- The issue number is composed of 1 or more digits `[0, 9]+` +- The reference must be preceeded by either `(` or whitespace or EOF. +- The reference must be followed by either `)` or whitespace or EOF. -A single hypen must separate the project key and issue number. The issue number can be any length. +The following are valid examples: -The reference must be preceeded by either nothing or whitespace. +```````````````````````````````` example +This is a ABCD-123 issue +. +

This is a ABCD-123 issue

+```````````````````````````````` -The reference must be followed by either nothing or whitespace. \ No newline at end of file +```````````````````````````````` example +This is a KIRA-1 issue +. +

This is a KIRA-1 issue

+```````````````````````````````` + +```````````````````````````````` example +This is a Z-1 issue +. +

This is a Z-1 issue

+```````````````````````````````` + +These are also valid links with `(` and `)`: + +```````````````````````````````` example +This is a (ABCD-123) issue +. +

This is a (ABCD-123) issue

+```````````````````````````````` + +```````````````````````````````` example +This is a (KIRA-1) issue +. +

This is a (KIRA-1) issue

+```````````````````````````````` + +```````````````````````````````` example +This is a (Z-1) issue +. +

This is a (Z-1) issue

+```````````````````````````````` + +These are not valid links: + +```````````````````````````````` example +This is not aJIRA-123 issue +. +

This is not aJIRA-123 issue

+```````````````````````````````` + +```````````````````````````````` example +This is not JIRA-123a issue +. +

This is not JIRA-123a issue

+```````````````````````````````` + +```````````````````````````````` example +This is not JIRA- issue +. +

This is not JIRA- issue

+```````````````````````````````` diff --git a/src/Markdig.Tests/Specs/Specs.cs b/src/Markdig.Tests/Specs/Specs.cs index ccda17a5..6dedfb0a 100644 --- a/src/Markdig.Tests/Specs/Specs.cs +++ b/src/Markdig.Tests/Specs/Specs.cs @@ -20529,4 +20529,196 @@ namespace Markdig.Tests TestParser.TestSpec("Check **http://www.a.com** or __http://www.b.com__", "

Check http://www.a.com or http://www.b.com

", "autolinks|advanced"); } } + // ## Jira Links + // + // The JiraLinks extension will automatically add links to JIRA issue items within your markdown, e.g. XX-1234. For this to happen, you must configure the extension when adding to the pipeline, e.g. + // + // ``` + // var pipeline = new MarkdownPipelineBuilder() + // .UseJiraLinks(new JiraLinkOptions("http://your.company.abc")) + // .Build(); + // ``` + // + // The rules for detecting a link are: + // + // - The project key must be composed of onre or more capitalised ASCII letter `[A-Z]+` + // - A single hypen `-` must separate the project key and issue number. + // - The issue number is composed of 1 or more digits `[0, 9]+` + // - The reference must be preceeded by either `(` or whitespace or EOF. + // - The reference must be followed by either `)` or whitespace or EOF. + // + // The following are valid examples: + [TestFixture] + public partial class TestJiraLinks + { + [Test] + public void Example001() + { + // Example 1 + // Section: Jira Links + // + // The following CommonMark: + // This is a ABCD-123 issue + // + // Should be rendered as: + //

This is a ABCD-123 issue

+ + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 1, " Jira Links"); + TestParser.TestSpec("This is a ABCD-123 issue", "

This is a ABCD-123 issue

", "jiralinks"); + } + } + [TestFixture] + public partial class TestJiraLinks + { + [Test] + public void Example002() + { + // Example 2 + // Section: Jira Links + // + // The following CommonMark: + // This is a KIRA-1 issue + // + // Should be rendered as: + //

This is a KIRA-1 issue

+ + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 2, " Jira Links"); + TestParser.TestSpec("This is a KIRA-1 issue", "

This is a KIRA-1 issue

", "jiralinks"); + } + } + [TestFixture] + public partial class TestJiraLinks + { + [Test] + public void Example003() + { + // Example 3 + // Section: Jira Links + // + // The following CommonMark: + // This is a Z-1 issue + // + // Should be rendered as: + //

This is a Z-1 issue

+ + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 3, " Jira Links"); + TestParser.TestSpec("This is a Z-1 issue", "

This is a Z-1 issue

", "jiralinks"); + } + } + // These are also valid links with `(` and `)`: + [TestFixture] + public partial class TestJiraLinks + { + [Test] + public void Example004() + { + // Example 4 + // Section: Jira Links + // + // The following CommonMark: + // This is a (ABCD-123) issue + // + // Should be rendered as: + //

This is a (ABCD-123) issue

+ + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 4, " Jira Links"); + TestParser.TestSpec("This is a (ABCD-123) issue", "

This is a (ABCD-123) issue

", "jiralinks"); + } + } + [TestFixture] + public partial class TestJiraLinks + { + [Test] + public void Example005() + { + // Example 5 + // Section: Jira Links + // + // The following CommonMark: + // This is a (KIRA-1) issue + // + // Should be rendered as: + //

This is a (KIRA-1) issue

+ + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 5, " Jira Links"); + TestParser.TestSpec("This is a (KIRA-1) issue", "

This is a (KIRA-1) issue

", "jiralinks"); + } + } + [TestFixture] + public partial class TestJiraLinks + { + [Test] + public void Example006() + { + // Example 6 + // Section: Jira Links + // + // The following CommonMark: + // This is a (Z-1) issue + // + // Should be rendered as: + //

This is a (Z-1) issue

+ + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 6, " Jira Links"); + TestParser.TestSpec("This is a (Z-1) issue", "

This is a (Z-1) issue

", "jiralinks"); + } + } + // These are not valid links: + [TestFixture] + public partial class TestJiraLinks + { + [Test] + public void Example007() + { + // Example 7 + // Section: Jira Links + // + // The following CommonMark: + // This is not aJIRA-123 issue + // + // Should be rendered as: + //

This is not aJIRA-123 issue

+ + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 7, " Jira Links"); + TestParser.TestSpec("This is not aJIRA-123 issue", "

This is not aJIRA-123 issue

", "jiralinks"); + } + } + [TestFixture] + public partial class TestJiraLinks + { + [Test] + public void Example008() + { + // Example 8 + // Section: Jira Links + // + // The following CommonMark: + // This is not JIRA-123a issue + // + // Should be rendered as: + //

This is not JIRA-123a issue

+ + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 8, " Jira Links"); + TestParser.TestSpec("This is not JIRA-123a issue", "

This is not JIRA-123a issue

", "jiralinks"); + } + } + [TestFixture] + public partial class TestJiraLinks + { + [Test] + public void Example009() + { + // Example 9 + // Section: Jira Links + // + // The following CommonMark: + // This is not JIRA- issue + // + // Should be rendered as: + //

This is not JIRA- issue

+ + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 9, " Jira Links"); + TestParser.TestSpec("This is not JIRA- issue", "

This is not JIRA- issue

", "jiralinks"); + } + } } diff --git a/src/Markdig.Tests/Specs/Specs.tt b/src/Markdig.Tests/Specs/Specs.tt index 95efac0f..893204ad 100644 --- a/src/Markdig.Tests/Specs/Specs.tt +++ b/src/Markdig.Tests/Specs/Specs.tt @@ -61,6 +61,7 @@ SOFTWARE. new KeyValuePair(Host.ResolvePath("NoHtmlSpecs.md"), "nohtml"), new KeyValuePair(Host.ResolvePath("YamlSpecs.md"), "yaml"), new KeyValuePair(Host.ResolvePath("AutoLinks.md"), "autolinks|advanced"), + new KeyValuePair(Host.ResolvePath("JiraLinks.md"), "jiralinks"), }; var emptyLines = false; var displayEmptyLines = false; diff --git a/src/Markdig.Tests/TestParser.cs b/src/Markdig.Tests/TestParser.cs index 55f9aaa1..e11a4b0d 100644 --- a/src/Markdig.Tests/TestParser.cs +++ b/src/Markdig.Tests/TestParser.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; +using Markdig.Extensions.JiraLinks; namespace Markdig.Tests { @@ -71,8 +72,15 @@ namespace Markdig.Tests { var builder = new MarkdownPipelineBuilder(); builder.DebugLog = Console.Out; - var pipeline = extensionsText == "self" ? builder.UseSelfPipeline() : builder.Configure(extensionsText); - yield return new KeyValuePair(extensionsText, pipeline.Build()); + if (extensionsText == "jiralinks") + { + builder.UseJiraLinks(new JiraLinkOptions("http://your.company.abc")); + } + else + { + builder = extensionsText == "self" ? builder.UseSelfPipeline() : builder.Configure(extensionsText); + } + yield return new KeyValuePair(extensionsText, builder.Build()); } } diff --git a/src/Markdig/Extensions/JiraLinks/JiraLink.cs b/src/Markdig/Extensions/JiraLinks/JiraLink.cs index 965fd4fa..862b5ec1 100644 --- a/src/Markdig/Extensions/JiraLinks/JiraLink.cs +++ b/src/Markdig/Extensions/JiraLinks/JiraLink.cs @@ -1,7 +1,8 @@ -using System; -using System.Collections.Generic; +// 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.Diagnostics; -using System.Text; +using Markdig.Helpers; using Markdig.Syntax.Inlines; namespace Markdig.Extensions.JiraLinks @@ -15,11 +16,11 @@ namespace Markdig.Extensions.JiraLinks /// /// JIRA Project Key /// - public string ProjectKey { get; set; } + public StringSlice ProjectKey { get; set; } /// /// JIRA Issue Number /// - public string Issue { get; set; } + public StringSlice Issue { get; set; } } } diff --git a/src/Markdig/Extensions/JiraLinks/JiraLinkExtension.cs b/src/Markdig/Extensions/JiraLinks/JiraLinkExtension.cs index ac1b7476..02495ddf 100644 --- a/src/Markdig/Extensions/JiraLinks/JiraLinkExtension.cs +++ b/src/Markdig/Extensions/JiraLinks/JiraLinkExtension.cs @@ -1,7 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Markdig; +// 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 Markdig.Parsers.Inlines; using Markdig.Renderers; diff --git a/src/Markdig/Extensions/JiraLinks/JiraLinkInlineParser.cs b/src/Markdig/Extensions/JiraLinks/JiraLinkInlineParser.cs index be013b4d..3860f623 100644 --- a/src/Markdig/Extensions/JiraLinks/JiraLinkInlineParser.cs +++ b/src/Markdig/Extensions/JiraLinks/JiraLinkInlineParser.cs @@ -1,11 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Markdig.Extensions.TaskLists; +// 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; using Markdig.Helpers; using Markdig.Parsers; -using Markdig.Renderers.Html; -using Markdig.Syntax; namespace Markdig.Extensions.JiraLinks { @@ -22,19 +20,22 @@ namespace Markdig.Extensions.JiraLinks public override bool Match(InlineProcessor processor, ref StringSlice slice) { - if (!slice.PeekCharExtra(-1).IsWhiteSpaceOrZero()) + // Allow preceding whitespace or `(` + var pc = slice.PeekCharExtra(-1); + if (!pc.IsWhiteSpaceOrZero() && pc != '(') { - return false; //require whitespace/nothing before + return false; } - var key = String.Empty; - var issue = String.Empty; var current = slice.CurrentChar; + var startKey = slice.Start; + var endKey = slice.Start; + //read as many uppercase characters as required - project key while (current.IsAlphaUpper()) { - key += current; + endKey = slice.Start; current = slice.NextChar(); } @@ -44,16 +45,24 @@ namespace Markdig.Extensions.JiraLinks return false; } - current = slice.NextChar(); + current = slice.NextChar(); // skip - //read as many numbers as required - issue number + if (!current.IsDigit()) + { + return false; + } + + var startIssue = slice.Start; + var endIssue = slice.Start; + while (current.IsDigit()) { - issue += current; + endIssue = slice.Start; current = slice.NextChar(); } - if (!current.IsWhiteSpaceOrZero()) //must be followed by whitespace + if (!current.IsWhiteSpaceOrZero() && current != ')') //can be followed only by a whitespace or `)` { return false; } @@ -68,11 +77,11 @@ namespace Markdig.Extensions.JiraLinks }, Line = line, Column = column, - Issue = issue, - ProjectKey = key + Issue = new StringSlice(slice.Text, startIssue, endIssue), + ProjectKey = new StringSlice(slice.Text, startKey, endKey) }; - processor.Inline.Span.End = processor.Inline.Span.Start + issue.Length + key.Length + 1; //+1 for the '-' + processor.Inline.Span.End = processor.Inline.Span.Start + (endIssue - startKey); return true; } diff --git a/src/Markdig/Extensions/JiraLinks/JiraLinkOptions.cs b/src/Markdig/Extensions/JiraLinks/JiraLinkOptions.cs index 3d9dbeac..16f5d910 100644 --- a/src/Markdig/Extensions/JiraLinks/JiraLinkOptions.cs +++ b/src/Markdig/Extensions/JiraLinks/JiraLinkOptions.cs @@ -1,5 +1,6 @@ -using System; -using System.Collections.Generic; +// 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.Text; namespace Markdig.Extensions.JiraLinks @@ -10,18 +11,47 @@ namespace Markdig.Extensions.JiraLinks public class JiraLinkOptions { /// - /// e.g. https://mycompany.atlassian.net/ + /// The base Url (e.g. `https://mycompany.atlassian.net`) /// - public string Url { get; set; } + public string BaseUrl { get; set; } + + /// + /// The base path after the base url (default is `/browse`) + /// + public string BasePath { get; set; } /// /// Should the link open in a new window when clicked /// public bool OpenInNewWindow { get; set; } - public JiraLinkOptions() + public JiraLinkOptions(string baseUrl) { OpenInNewWindow = true; //default + BaseUrl = baseUrl; + BasePath = "/browse"; + } + + /// + /// Gets the full url composed of the and with no trailing `/` + /// + public virtual string GetUrl() + { + var url = new StringBuilder(); + var baseUrl = BaseUrl; + if (baseUrl != null) + { + url.Append(baseUrl.TrimEnd('/')); + } + + url.Append("/"); + + if (BasePath != null) + { + url.Append(BasePath.Trim('/')); + } + + return url.ToString(); } } } diff --git a/src/Markdig/Extensions/JiraLinks/JiraLinkRenderer.cs b/src/Markdig/Extensions/JiraLinks/JiraLinkRenderer.cs index 4466fff9..e373ab25 100644 --- a/src/Markdig/Extensions/JiraLinks/JiraLinkRenderer.cs +++ b/src/Markdig/Extensions/JiraLinks/JiraLinkRenderer.cs @@ -1,6 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Text; +// 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 Markdig.Renderers; using Markdig.Renderers.Html; @@ -20,7 +20,7 @@ namespace Markdig.Extensions.JiraLinks if (renderer.EnableHtmlForInline) { renderer - .Write("") // > .Write(obj.ProjectKey).Write('-').Write(obj.Issue) // XX-1234 (link text) .Write(""); //