Skip to content

Commit

Permalink
Added converting of format characters and hyperlinks from steps for Q…
Browse files Browse the repository at this point in the history
…aseExporter.
  • Loading branch information
pavel.butuzov committed Sep 19, 2024
1 parent 5ef0d2d commit 7f6ab9b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
1 change: 0 additions & 1 deletion Migrators/QaseExporter/Services/ExportService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.Extensions.Logging;
using Models;
using JsonWriter;
using Attribute = Models.Attribute;

namespace QaseExporter.Services;

Expand Down
16 changes: 12 additions & 4 deletions Migrators/QaseExporter/Services/StepService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public async Task<List<Step>> ConvertConditionSteps(string conditions, Guid test

var newStep = new Step
{
Action = action.Description,
Action = ConvertStepDesctiption(action.Description),
Expected = string.Empty,
TestData = string.Empty,
ActionAttachments = new List<string>(),
Expand All @@ -91,9 +91,9 @@ private async Task<Step> ConvertStep(QaseStep step, Guid testCaseId)

var newStep = new Step
{
Action = action.Description,
Expected = expected.Description,
TestData = testData.Description,
Action = ConvertStepDesctiption(action.Description),
Expected = ConvertStepDesctiption(expected.Description),
TestData = ConvertStepDesctiption(testData.Description),
ActionAttachments = new List<string>(),
ExpectedAttachments = new List<string>(),
TestDataAttachments = new List<string>()
Expand Down Expand Up @@ -131,4 +131,12 @@ private async Task<Step> ConvertStep(QaseStep step, Guid testCaseId)

return newStep;
}

private static string ConvertStepDesctiption(string description)
{
return
Utils.ConvertingHyperlinks(
Utils.ConvertingFormatCharacters(description)
);
}
}
59 changes: 57 additions & 2 deletions Migrators/QaseExporter/Services/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ namespace QaseExporter.Services;

public static class Utils
{
private const string ImgPattern = @"!\[[\S]*\]\([\S]*\)";
private const string UrlPattern = @"\(([\S]+)\)";
private const string ImgPattern = @"!\[[^\[\]]*\]\([^()\s]*\)";
private const string UrlPattern = @"\(([^()\s]+)\)";
private const string HyperlinkPattern = @"\[[^\[\]]*\]\([^()\s]*\)";
private const string titlePattern = @"\[([^\[\]]+)\]";
private const string HtmlPattern = @"<.*?>";
private const string FormatTabCharacter = "\t";
private const string FormatNewLineCharacter = "\n";

public static QaseDescriptionData ExtractAttachments(string? description)
{
Expand Down Expand Up @@ -54,4 +59,54 @@ public static QaseDescriptionData ExtractAttachments(string? description)

return data;
}

public static string ConvertingHyperlinks(string? description)
{
if (string.IsNullOrEmpty(description))
{
return string.Empty;
}

var hyperlinkRegex = new Regex(HyperlinkPattern);

var matches = hyperlinkRegex.Matches(description);

if (matches.Count == 0)
{
return description;
}

foreach (Match match in matches)
{
var urlRegex = new Regex(UrlPattern);
var urlMatch = urlRegex.Match(match.Value);

if (!urlMatch.Success) continue;

var url = urlMatch.Groups[1].Value;

var titleRegex = new Regex(titlePattern);
var titleMatch = titleRegex.Match(match.Value);

var title = titleMatch.Success ? titleMatch.Groups[1].Value : url;

description = description.Replace(match.Value, $"<a target=\"_blank\" rel=\"noopener noreferrer\" href=\"{url}\">{title}</a>");
}
return description;
}

public static string ConvertingFormatCharacters(string? description)
{
if (string.IsNullOrEmpty(description))
{
return string.Empty;
}

var descriptionLines = description.Split(FormatNewLineCharacter);

description = string.Join("", descriptionLines.Select(l => $"<p class=\"tiptap-text\">{l}</p>"));
description = description.Replace(FormatTabCharacter, " ");

return description;
}
}

0 comments on commit 7f6ab9b

Please sign in to comment.