Update JiraLinks, allow ( and ), use StringSlice instead of strings for key/issue, add support for attributes, add tests

This commit is contained in:
Alexandre Mutel
2017-05-18 10:33:16 +02:00
parent 26c5ce59b5
commit 97ce0da564
9 changed files with 342 additions and 48 deletions

View File

@@ -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
.
<p>This is a <a href="http://your.company.abc/browse/ABCD-123" target="blank">ABCD-123</a> issue</p>
````````````````````````````````
The reference must be followed by either nothing or whitespace.
```````````````````````````````` example
This is a KIRA-1 issue
.
<p>This is a <a href="http://your.company.abc/browse/KIRA-1" target="blank">KIRA-1</a> issue</p>
````````````````````````````````
```````````````````````````````` example
This is a Z-1 issue
.
<p>This is a <a href="http://your.company.abc/browse/Z-1" target="blank">Z-1</a> issue</p>
````````````````````````````````
These are also valid links with `(` and `)`:
```````````````````````````````` example
This is a (ABCD-123) issue
.
<p>This is a (<a href="http://your.company.abc/browse/ABCD-123" target="blank">ABCD-123</a>) issue</p>
````````````````````````````````
```````````````````````````````` example
This is a (KIRA-1) issue
.
<p>This is a (<a href="http://your.company.abc/browse/KIRA-1" target="blank">KIRA-1</a>) issue</p>
````````````````````````````````
```````````````````````````````` example
This is a (Z-1) issue
.
<p>This is a (<a href="http://your.company.abc/browse/Z-1" target="blank">Z-1</a>) issue</p>
````````````````````````````````
These are not valid links:
```````````````````````````````` example
This is not aJIRA-123 issue
.
<p>This is not aJIRA-123 issue</p>
````````````````````````````````
```````````````````````````````` example
This is not JIRA-123a issue
.
<p>This is not JIRA-123a issue</p>
````````````````````````````````
```````````````````````````````` example
This is not JIRA- issue
.
<p>This is not JIRA- issue</p>
````````````````````````````````

View File

@@ -20529,4 +20529,196 @@ namespace Markdig.Tests
TestParser.TestSpec("Check **http://www.a.com** or __http://www.b.com__", "<p>Check <strong><a href=\"http://www.a.com\">http://www.a.com</a></strong> or <strong><a href=\"http://www.b.com\">http://www.b.com</a></strong></p>", "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:
// <p>This is a <a href="http://your.company.abc/browse/ABCD-123" target="blank">ABCD-123</a> issue</p>
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 1, " Jira Links");
TestParser.TestSpec("This is a ABCD-123 issue", "<p>This is a <a href=\"http://your.company.abc/browse/ABCD-123\" target=\"blank\">ABCD-123</a> issue</p>", "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:
// <p>This is a <a href="http://your.company.abc/browse/KIRA-1" target="blank">KIRA-1</a> issue</p>
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 2, " Jira Links");
TestParser.TestSpec("This is a KIRA-1 issue", "<p>This is a <a href=\"http://your.company.abc/browse/KIRA-1\" target=\"blank\">KIRA-1</a> issue</p>", "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:
// <p>This is a <a href="http://your.company.abc/browse/Z-1" target="blank">Z-1</a> issue</p>
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 3, " Jira Links");
TestParser.TestSpec("This is a Z-1 issue", "<p>This is a <a href=\"http://your.company.abc/browse/Z-1\" target=\"blank\">Z-1</a> issue</p>", "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:
// <p>This is a (<a href="http://your.company.abc/browse/ABCD-123" target="blank">ABCD-123</a>) issue</p>
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 4, " Jira Links");
TestParser.TestSpec("This is a (ABCD-123) issue", "<p>This is a (<a href=\"http://your.company.abc/browse/ABCD-123\" target=\"blank\">ABCD-123</a>) issue</p>", "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:
// <p>This is a (<a href="http://your.company.abc/browse/KIRA-1" target="blank">KIRA-1</a>) issue</p>
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 5, " Jira Links");
TestParser.TestSpec("This is a (KIRA-1) issue", "<p>This is a (<a href=\"http://your.company.abc/browse/KIRA-1\" target=\"blank\">KIRA-1</a>) issue</p>", "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:
// <p>This is a (<a href="http://your.company.abc/browse/Z-1" target="blank">Z-1</a>) issue</p>
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 6, " Jira Links");
TestParser.TestSpec("This is a (Z-1) issue", "<p>This is a (<a href=\"http://your.company.abc/browse/Z-1\" target=\"blank\">Z-1</a>) issue</p>", "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:
// <p>This is not aJIRA-123 issue</p>
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 7, " Jira Links");
TestParser.TestSpec("This is not aJIRA-123 issue", "<p>This is not aJIRA-123 issue</p>", "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:
// <p>This is not JIRA-123a issue</p>
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 8, " Jira Links");
TestParser.TestSpec("This is not JIRA-123a issue", "<p>This is not JIRA-123a issue</p>", "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:
// <p>This is not JIRA- issue</p>
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 9, " Jira Links");
TestParser.TestSpec("This is not JIRA- issue", "<p>This is not JIRA- issue</p>", "jiralinks");
}
}
}

View File

@@ -61,6 +61,7 @@ SOFTWARE.
new KeyValuePair<string, string>(Host.ResolvePath("NoHtmlSpecs.md"), "nohtml"),
new KeyValuePair<string, string>(Host.ResolvePath("YamlSpecs.md"), "yaml"),
new KeyValuePair<string, string>(Host.ResolvePath("AutoLinks.md"), "autolinks|advanced"),
new KeyValuePair<string, string>(Host.ResolvePath("JiraLinks.md"), "jiralinks"),
};
var emptyLines = false;
var displayEmptyLines = false;

View File

@@ -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<string, MarkdownPipeline>(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<string, MarkdownPipeline>(extensionsText, builder.Build());
}
}

View File

@@ -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
/// <summary>
/// JIRA Project Key
/// </summary>
public string ProjectKey { get; set; }
public StringSlice ProjectKey { get; set; }
/// <summary>
/// JIRA Issue Number
/// </summary>
public string Issue { get; set; }
public StringSlice Issue { get; set; }
}
}

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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
{
/// <summary>
/// e.g. https://mycompany.atlassian.net/
/// The base Url (e.g. `https://mycompany.atlassian.net`)
/// </summary>
public string Url { get; set; }
public string BaseUrl { get; set; }
/// <summary>
/// The base path after the base url (default is `/browse`)
/// </summary>
public string BasePath { get; set; }
/// <summary>
/// Should the link open in a new window when clicked
/// </summary>
public bool OpenInNewWindow { get; set; }
public JiraLinkOptions()
public JiraLinkOptions(string baseUrl)
{
OpenInNewWindow = true; //default
BaseUrl = baseUrl;
BasePath = "/browse";
}
/// <summary>
/// Gets the full url composed of the <see cref="BaseUrl"/> and <see cref="BasePath"/> with no trailing `/`
/// </summary>
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();
}
}
}

View File

@@ -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("<a href=\"").Write(Options.Url).Write("/browse/") // <a href="http:/xxx/browse/"
.Write("<a href=\"").Write(Options.GetUrl()).Write("/") // <a href="http:/xxx/browse/"
.Write(obj.ProjectKey).Write('-').Write(obj.Issue) // XX-1234 (link url)
.Write("\"");
@@ -30,6 +30,7 @@ namespace Markdig.Extensions.JiraLinks
}
renderer
.WriteAttributes(obj)
.Write(">") // >
.Write(obj.ProjectKey).Write('-').Write(obj.Issue) // XX-1234 (link text)
.Write("</a>"); //</a>