Skip to content

Commit 8ee477f

Browse files
authored
Introduce an extension method for ReadOnlySpan<char> to acquire a trimmed relative path
1 parent 8c80506 commit 8ee477f

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

src/docs-assembler/Building/PublishEnvironmentUriResolver.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System.Collections.Frozen;
66
using Documentation.Assembler.Configuration;
7+
using Documentation.Assembler.Extensions;
78
using Elastic.Markdown.Links.CrossLinks;
89

910
namespace Documentation.Assembler.Building;
@@ -142,10 +143,9 @@ private string GetSubPathPrefix(Uri crossLinkUri, ref string path)
142143
if (originalPath == toc.SourcePathPrefix)
143144
return string.Empty;
144145

145-
var newRelativePath = path.TrimStart('/').AsSpan().TrimStart(originalPath).ToString();
146-
path = Path.Combine(toc.SourcePathPrefix, newRelativePath.TrimStart('/'));
146+
var newRelativePath = path.AsSpan().GetTrimmedRelativePath(originalPath);
147+
path = Path.Combine(toc.SourcePathPrefix, newRelativePath);
147148

148149
return string.Empty;
149150
}
150-
151151
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
namespace Documentation.Assembler.Extensions;
6+
7+
public static class SpanExtensions
8+
{
9+
public static string GetTrimmedRelativePath(this ReadOnlySpan<char> relativePath, string originalPath)
10+
{
11+
var trimmedInput = relativePath.TrimStart('/');
12+
var newRelativePath = trimmedInput.StartsWith(originalPath, StringComparison.Ordinal)
13+
? trimmedInput.Slice(originalPath.Length).TrimStart('/').ToString()
14+
: trimmedInput.ToString();
15+
return newRelativePath;
16+
}
17+
}

src/docs-assembler/Navigation/GlobalNavigationPathProvider.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System.Collections.Immutable;
66
using System.IO.Abstractions;
7+
using Documentation.Assembler.Extensions;
78
using Elastic.Markdown;
89
using Elastic.Markdown.Diagnostics;
910
using Elastic.Markdown.Extensions.DetectionRules;
@@ -118,9 +119,7 @@ public GlobalNavigationPathProvider(GlobalNavigationFile navigationFile, Assembl
118119

119120
var originalPath = Path.Combine(match.Host, match.AbsolutePath.Trim('/')).TrimStart('/');
120121
var relativePathSpan = relativePath.AsSpan();
121-
var newRelativePath = relativePathSpan.StartsWith(originalPath, StringComparison.Ordinal)
122-
? relativePathSpan.Slice(originalPath.Length).TrimStart('/').ToString()
123-
: relativePathSpan.TrimStart(originalPath).TrimStart('/').ToString();
122+
var newRelativePath = relativePathSpan.GetTrimmedRelativePath(originalPath);
124123

125124
var path = fs.Path.Combine(outputDirectory.FullName, toc.SourcePathPrefix, newRelativePath);
126125

0 commit comments

Comments
 (0)