Skip to content

Commit

Permalink
Fixed LinkGenerator behavior when using query and/or fragment compone…
Browse files Browse the repository at this point in the history
…nts (#170)
  • Loading branch information
daveaglick committed Feb 24, 2021
1 parent 0061112 commit 9727372
Show file tree
Hide file tree
Showing 3 changed files with 551 additions and 24 deletions.
2 changes: 2 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- Updated Buildalyzer to the most recent version and resolves Roslyn version incompatibilities in the `AnalyzeCSharp` module (#174, thanks @mholo65).
- Added an optional `makeAbsolute` parameter to `LinkGenerator.GetLink()` that allows keeping links as relative (#170).
- Fixed fragment support in the Markdown link rewriter (#170, #175, thanks @JoshClose).
- Fixed `LinkGenerator` behavior when using query and/or fragment components (#170).

# 1.0.0-beta.36

Expand Down
61 changes: 50 additions & 11 deletions src/core/Statiq.Common/Util/LinkGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,33 +79,72 @@ public static string GetLink(
}
}

// If we're not making it absolute then we're done if this isn't already an absolute path
if (!makeAbsolute && (link.Length == 0 || link[0] != NormalizedPath.Slash[0]))
// Extract the fragment
int fragmentIndex = link.IndexOf('#');
string fragment = null;
if (fragmentIndex > -1)
{
fragment = link.Substring(fragmentIndex);
link = link.Substring(0, fragmentIndex);
}

// Extract the query
int queryIndex = link.IndexOf('?');
string query = null;
if (queryIndex > -1)
{
return link;
query = link.Substring(queryIndex);
link = link.Substring(0, queryIndex);
}

// Collapse the root and combine
string rootLink = root.IsNull ? string.Empty : root.FullPath;
if (rootLink.EndsWith(NormalizedPath.Slash))
// If we're not making it absolute and it doesn't start with a slash, make sure to remove the slash when we're done,
// otherwise collapse with the root path and combine them
bool makeRelative = false;
if (!makeAbsolute && (link.Length == 0 || link[0] != NormalizedPath.Slash[0]))
{
makeRelative = true;
}
else
{
rootLink = rootLink.Substring(0, rootLink.Length - 1);
string rootLink = root.IsNull ? string.Empty : root.FullPath;
if (rootLink.EndsWith(NormalizedPath.Slash))
{
rootLink = rootLink.Substring(0, rootLink.Length - 1);
}
link = rootLink + link;
}

// Add the host and convert to URI for escaping
UriBuilder builder = new UriBuilder
{
Path = rootLink + link,
Scheme = scheme ?? "http"
Scheme = scheme ?? "http",
Path = link,
Query = query,
Fragment = fragment
};
bool hasHost = false;
if (!string.IsNullOrWhiteSpace(host))
if (!makeRelative && !string.IsNullOrWhiteSpace(host))
{
builder.Host = host;
hasHost = true;
}
Uri uri = builder.Uri;
string renderedLink = hasHost ? uri.AbsoluteUri : uri.AbsolutePath;
string renderedLink = hasHost
? uri.AbsoluteUri
: uri.GetComponents(UriComponents.Path | UriComponents.Query | UriComponents.Fragment, UriFormat.SafeUnescaped);

// Remove the slash prefix if we have one
if (makeRelative && renderedLink[0] == '/')
{
renderedLink = renderedLink.Substring(1);

// If the link started with a dot, add it back in
if (link.Length > 0 && link[0] == '.' && (renderedLink.Length == 0 || renderedLink[0] != '.'))
{
renderedLink = "." + renderedLink;
}
}

return lowercase ? renderedLink.ToLowerInvariant() : renderedLink;
}

Expand Down
Loading

0 comments on commit 9727372

Please sign in to comment.