Skip to content

Commit

Permalink
Merge pull request #7804 from dotnet/v3
Browse files Browse the repository at this point in the history
🛸🛸🛸
  • Loading branch information
AngryBerryMS authored Jan 6, 2022
2 parents 1331fc2 + 155dfff commit 4bd85a2
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 18 deletions.
4 changes: 3 additions & 1 deletion .github/actions/spelling/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ hubpage
hyperlink
hyperlinks
hypertrm
iaggregate
ico
idx
iframe
Expand Down Expand Up @@ -755,4 +756,5 @@ yunit
yyyy
zoneinfo
zonepivotgroups
zzz
zzz
istreamable
4 changes: 2 additions & 2 deletions docs/specs/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,8 @@ outputs:
"content": "
<p>
<a href=\"self-uid\" data-linktype=\"relative-path\"> root name </a>
<a href=\"self-uid#child_1\" data-linktype=\"relative-path\"> child-1 name </a>
<a href=\"self-uid#child_2\" data-linktype=\"relative-path\"> child-2 name </a>
<a href=\"self-uid#child-1\" data-linktype=\"relative-path\"> child-1 name </a>
<a href=\"self-uid#child-2\" data-linktype=\"relative-path\"> child-2 name </a>
</p>"
}
---
Expand Down
26 changes: 13 additions & 13 deletions docs/specs/xref.yml
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ inputs:
outputs:
docs/a.json:
docs/b.json: |
{"conceptual": "<p>Link to <a class=\"no-loc\" href=\"a.json#a_b\">a.b</a></p>\n"}
{"conceptual": "<p>Link to <a class=\"no-loc\" href=\"a.json#a-b\">a.b</a></p>\n"}
.xrefmap.json: |
{
"references": [{
Expand Down Expand Up @@ -1135,13 +1135,13 @@ outputs:
Link to <a class=\"no-loc\" href=\"a.json#child\" data-linktype=\"relative-path\">child</a>
Link to <a class=\"no-loc\" href=\"a.json#child\" data-linktype=\"relative-path\">child.c c</a>
Link to <a class=\"no-loc\" href=\"a.json#child\" data-linktype=\"relative-path\">child</a>
Link to <a class=\"no-loc\" href=\"a.json#children_a\" data-linktype=\"relative-path\">children.a</a>
Link to <a class=\"no-loc\" href=\"a.json#children_a\" data-linktype=\"relative-path\">children.a</a>
Link to <a class=\"no-loc\" href=\"a.json#children_a\" data-linktype=\"relative-path\">children.a</a>
Link to <a class=\"no-loc\" href=\"a.json#children_b\" data-linktype=\"relative-path\">children.b a</a>
Link to <a class=\"no-loc\" href=\"a.json#children_b\" data-linktype=\"relative-path\">children.b</a>
Link to <a class=\"no-loc\" href=\"a.json#children_b\" data-linktype=\"relative-path\">children.b</a>
Link to <a class=\"no-loc\" href=\"a.json#children_c\" data-linktype=\"relative-path\">children.c c</a>
Link to <a class=\"no-loc\" href=\"a.json#children-a\" data-linktype=\"relative-path\">children.a</a>
Link to <a class=\"no-loc\" href=\"a.json#children-a\" data-linktype=\"relative-path\">children.a</a>
Link to <a class=\"no-loc\" href=\"a.json#children-a\" data-linktype=\"relative-path\">children.a</a>
Link to <a class=\"no-loc\" href=\"a.json#children-b\" data-linktype=\"relative-path\">children.b a</a>
Link to <a class=\"no-loc\" href=\"a.json#children-b\" data-linktype=\"relative-path\">children.b</a>
Link to <a class=\"no-loc\" href=\"a.json#children-b\" data-linktype=\"relative-path\">children.b</a>
Link to <a class=\"no-loc\" href=\"a.json#children-c\" data-linktype=\"relative-path\">children.c c</a>
</p>"
}
.xrefmap.json: |
Expand All @@ -1160,16 +1160,16 @@ outputs:
},
{
"uid": "children.a",
"href": "https://docs.com/docs/a.json#children_a"
"href": "https://docs.com/docs/a.json#children-a"
},
{
"uid": "children.b",
"href": "https://docs.com/docs/a.json#children_b",
"href": "https://docs.com/docs/a.json#children-b",
"a": "children.b a"
},
{
"uid": "children.c",
"href": "https://docs.com/docs/a.json#children_c",
"href": "https://docs.com/docs/a.json#children-c",
"child.c": "children.c c"
}
]
Expand Down Expand Up @@ -1269,14 +1269,14 @@ outputs:
},
{
"uid": "children.a",
"href": "https://docs.com/docs/a.json#children_a",
"href": "https://docs.com/docs/a.json#children-a",
"list": [
"1.0"
]
},
{
"uid": "children.b",
"href": "https://docs.com/docs/a.json#children_b",
"href": "https://docs.com/docs/a.json#children-b",
"list": [
"2.0",
"3.0"
Expand Down
54 changes: 54 additions & 0 deletions src/docfx/lib/UrlUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,60 @@ public static string RemoveLeadingHostName(string url, string hostName, bool rem
: $"/{path}";
}

public static string GetBookmark(string uid)
{
var sb = new StringBuilder();
for (var i = 0; i < uid.Length; i++)
{
var ch = char.ToLowerInvariant(uid[i]);
switch (ch)
{
case '"'or '\'' or '%' or '^' or '\\':
continue;
case '<'or '[':
sb.Append('(');
break;
case '>' or ']':
sb.Append(')');
break;
case '{':
sb.Append("((");
break;
case '}':
sb.Append("))");
break;
case char c when (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'):
case '(' or ')' or '*' or '@':
sb.Append(ch);
break;
default:
if (sb.Length == 0 || sb[^1] == '-')
{
continue;
}
sb.Append('-');
break;
}
}

if (sb[^1] == '-')
{
for (var i = sb.Length - 1; i >= 0; i--)
{
if (sb[i] == '-')
{
sb.Remove(i, 1);
}
else
{
break;
}
}
}

return sb.ToString();
}

private static string ToQueryString(this NameValueCollection collection)
{
var result = new StringBuilder("?");
Expand Down
3 changes: 1 addition & 2 deletions src/docfx/lib/schema/JsonSchemaTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
using HtmlReaderWriter;
using Microsoft.Docs.Validation;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -296,7 +295,7 @@ private static bool IsXrefSpec(
private string GetXrefHref(FilePath file, string uid, int uidCount, bool isRootLevel)
{
var siteUrl = _documentProvider.GetSiteUrl(file);
return !isRootLevel && uidCount > 1 ? UrlUtility.MergeUrl(siteUrl, "", $"#{Regex.Replace(uid, @"\W", "_")}") : siteUrl;
return !isRootLevel && uidCount > 1 ? UrlUtility.MergeUrl(siteUrl, "", $"#{UrlUtility.GetBookmark(uid)}") : siteUrl;
}

private JToken LoadXrefProperty(
Expand Down
20 changes: 20 additions & 0 deletions test/docfx.Test/lib/UrlUtilityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,26 @@ public static void ParseAzureReposUrl(string remote, bool parsed, string expecte
Assert.False(parsed);
}

[Theory]
[InlineData("A.b[]", "a-b()")]
[InlineData("a b", "a-b")]
[InlineData("a\"b", "ab")]
[InlineData("a%b", "ab")]
[InlineData("a^b", "ab")]
[InlineData("a\\b", "ab")]
[InlineData("Dictionary<string, List<int>>*", "dictionary(string-list(int))*")]
[InlineData("a'b'c", "abc")]
[InlineData("{a|b_c'}", "((a-b-c))")]
[InlineData("---&&$$##List<string> test(int a`, int a@, string b*)---&&$$##", "list(string)-test(int-a-int-a@-string-b*)")]
[InlineData(
"Microsoft.StreamProcessing.Streamable.AggregateByKey``4(Microsoft.StreamProcessing.IStreamable{Microsoft.StreamProcessing.Empty,``0},System.Linq.Expressions.Expression{System.Func{``0,``1}},Microsoft.StreamProcessing.Aggregates.IAggregate{``0,``22,``23}},Microsoft.StreamProcessing.Aggregates.IAggregate{``0,``30,``31}},System.Linq.Expressions.Expression{System.Func{``3,``5,``7,``9,``11,``13,``15,``17,``19,``21,``23,``25,``27,``29,``31,``32}})",
"microsoft-streamprocessing-streamable-aggregatebykey-4(microsoft-streamprocessing-istreamable((microsoft-streamprocessing-empty-0))-system-linq-expressions-expression((system-func((-0-1))))-microsoft-streamprocessing-aggregates-iaggregate((-0-22-23))))-microsoft-streamprocessing-aggregates-iaggregate((-0-30-31))))-system-linq-expressions-expression((system-func((-3-5-7-9-11-13-15-17-19-21-23-25-27-29-31-32)))))")]
public static void StandardizeBookmarks(string uid, string expectedBookmark)
{
var bookmark = UrlUtility.GetBookmark(uid);
Assert.Equal(expectedBookmark, bookmark);
}

[Theory]
[InlineData("docs.com/en-us/c", "docs.com", true, "docs.com/en-us/c")]
[InlineData("https://docs.com/en-us/c", "docs.com", true, "/c")]
Expand Down

0 comments on commit 4bd85a2

Please sign in to comment.