Skip to content

Commit 45109b8

Browse files
authored
Update Xref Gen tool (#442)
1 parent 3d8e803 commit 45109b8

File tree

8 files changed

+57
-186
lines changed

8 files changed

+57
-186
lines changed

BlazorWebAssemblyXrefGenerator/BlazorWebAssemblyXrefGenerator.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
4-
VisualStudioVersion = 17.13.35617.110 d17.13
4+
VisualStudioVersion = 17.13.35617.110
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorWebAssemblyXrefGenerator", "BlazorWebAssemblyXrefGenerator.csproj", "{AC54A61E-49B7-4494-B9FF-978F64A9EF7C}"
77
EndProject

BlazorWebAssemblyXrefGenerator/Layout/MainLayout.razor

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
@inherits LayoutComponentBase
1+
@inherits LayoutComponentBase
22
<div class="page">
3-
<div class="sidebar">
4-
<NavMenu />
5-
</div>
6-
73
<main>
84
<div class="top-row px-4">
9-
<a href="https://learn.microsoft.com/aspnet/core/" target="_blank">About</a>
5+
<b>Xref Generator</b>
106
</div>
117

128
<article class="content px-4">

BlazorWebAssemblyXrefGenerator/Layout/NavMenu.razor

Lines changed: 0 additions & 29 deletions
This file was deleted.

BlazorWebAssemblyXrefGenerator/Layout/NavMenu.razor.css

Lines changed: 0 additions & 83 deletions
This file was deleted.
Lines changed: 35 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
@page "/"
2-
@using Microsoft.Extensions.Http
2+
@using System.Net
3+
@using System.Text.RegularExpressions
34
@inject IHttpClientFactory ClientFactory
45

5-
<div>
6+
<div style="margin-bottom:15px">
67
<h1>Namespace/Member</h1>
7-
8-
<label for="searchText"></label>
9-
<input id="searchText" style="width:300px" @bind="searchText" class="form-control" />
10-
<button type="button" @onclick="GetSearchResults" class="btn btn-default">Search</button>
11-
<button type="button" class="btn btn-default" onclick="document.getElementById('searchText').value=''">Clear</button>
8+
<input id="searchText" @bind="searchText" class="form-control" aria-label="Search Text" />
9+
<button type="button" @onclick="GetSearchResults" class="btn btn-primary">Search</button>
10+
<button type="button" class="btn btn-secondary" onclick="document.getElementById('searchText').value=''">Clear</button>
1211
</div>
1312

14-
1513
<h1>Links</h1>
1614

1715
@if (SearchResultItems?.Results?.Count() > 0)
@@ -21,32 +19,20 @@
2119
{
2220
<li style="margin-bottom:5px">
2321
<div style="font-size:1.4em;color:green;font-weight:bold">@result.Data.DisplayName</div>
24-
<div><b>Item Type:</b> @result.Data.ItemType <b>Item Kind:</b> @result.Data.ItemKind</div>
22+
<div><b>Item Type:</b> @result.Data.ItemType</div>
2523
<div><b>Description:</b> @result.Data.Description</div>
26-
27-
@if (result.Data.ItemKind == "Constructor")
28-
{
29-
30-
<div><input style="margin-bottom:3px;width:100%" id="m@(result.Index)0" value="[LINK_TEXT](@result.Data.Link)"></div>
31-
<div><input style="margin-bottom:3px;width:100%" id="m@(result.Index)1" value="@result.Data.Link"></div>
32-
<div>
33-
<button type="button" class="btn" onclick=@("copyToClipboard('m" + result.Index + "')0")>Copy (Custom Link Text)</button>
34-
<button type="button" class="btn" onclick=@($"copyToClipboard('m" + result.Index + "')1")>Copy (Member Only)</button>
35-
</div>
36-
}
37-
else
38-
{
39-
<div><input style="margin-bottom:3px;width:100%" id="m@(result.Index)0" value="<xref:@result.Data.Link>"></div>
40-
<div><input style="margin-bottom:3px;width:100%" id="m@(result.Index)1" value="<xref:@result.Data.Link?displayProperty=fullName>"></div>
41-
<div><input style="margin-bottom:3px;width:100%" id="m@(result.Index)2" value="[LINK_TEXT](xref:@result.Data.Link)"></div>
42-
<div><input style="margin-bottom:3px;width:100%" id="m@(result.Index)3" value="xref:@result.Data.Link"></div>
43-
<div>
44-
<button type="button" class="btn" onclick=@($"copyToClipboard('m" + result.Index + "0')")>Copy</button>
45-
<button type="button" class="btn" onclick=@($"copyToClipboard('m" + result.Index + "1')")>Copy (Full Name)</button>
46-
<button type="button" class="btn" onclick=@($"copyToClipboard('m" + result.Index + "2')")>Copy (Custom Link Text)</button>
47-
<button type="button" class="btn" onclick=@($"copyToClipboard('m" + result.Index + "3')")>Copy (Member Only)</button>
48-
</div>
49-
}
24+
<div><input id="m@(result.Index)0" value="<xref:@result.Data.Link>"></div>
25+
<div><input id="m@(result.Index)1" value="<xref:@result.Data.Link?displayProperty=fullName>"></div>
26+
<div><input id="m@(result.Index)2" value="<xref:@result.Data.Link?displayProperty=nameWithType>"></div>
27+
<div><input id="m@(result.Index)3" value="[LINK_TEXT](xref:@result.Data.Link)"></div>
28+
<div><input id="m@(result.Index)4" value="xref:@result.Data.Link"></div>
29+
<div>
30+
<button type="button" class="btn btn-primary" onclick=@($"copyToClipboard('m{result.Index}0')")>Copy</button>
31+
<button type="button" class="btn btn-secondary" onclick=@($"copyToClipboard('m{result.Index}1')")>Copy (Full Name)</button>
32+
<button type="button" class="btn btn-secondary" onclick=@($"copyToClipboard('m{result.Index}2')")>Copy (Name with Type)</button>
33+
<button type="button" class="btn btn-secondary" onclick=@($"copyToClipboard('m{result.Index}3')")>Copy (Custom Link Text)</button>
34+
<button type="button" class="btn btn-secondary" onclick=@($"copyToClipboard('m{result.Index}4')")>Copy (Member Only)</button>
35+
</div>
5036
</li>
5137
}
5238
</ol>
@@ -56,59 +42,46 @@
5642

5743
@code {
5844
private string? searchText;
59-
6045
private SearchResults? SearchResultItems { get; set; }
61-
6246
public string? message;
6347

6448
public async Task GetSearchResults()
6549
{
66-
var client = ClientFactory.CreateClient("APIClient");
50+
message = string.Empty;
51+
var apiClient = ClientFactory.CreateClient("APIClient");
6752

68-
if (searchText == null)
53+
if (string.IsNullOrEmpty(searchText) || apiClient == null)
6954
{
7055
return;
7156
}
7257

73-
SearchResultItems = await client.GetFromJsonAsync<SearchResults>($"api/apibrowser/dotnet/search?api-version=0.2&search={searchText}");
74-
75-
Console.WriteLine("HERE 1 !!!!!!!!!!!!");
58+
SearchResultItems = await apiClient.GetFromJsonAsync<SearchResults>($"api/apibrowser/dotnet/search?api-version=0.2&search={searchText}");
7659

7760
if (SearchResultItems?.Results != null)
7861
{
7962
foreach (var result in SearchResultItems.Results)
8063
{
81-
if (result.ItemType == "Constructor")
82-
{
83-
result.Link = "/dotnet/api/" + result?.DisplayName?.Substring(0, result.DisplayName.Length - 2).ToLowerInvariant() + ".-ctor";
84-
}
85-
else if (result.ItemType == "Method")
86-
{
87-
message = $"DisplayName: {result.DisplayName} Length: {result.DisplayName?.Length}";
88-
89-
result.Link = result?.DisplayName?.Substring(0, result.DisplayName.Length - 2) + "%2A";
90-
}
91-
else
92-
{
93-
result.Link = result.DisplayName;
94-
}
64+
var client = ClientFactory.CreateClient();
65+
66+
var urlEncodedRequestUrl = WebUtility.UrlEncode($"https://learn.microsoft.com/en-us{result.Url}?view=aspnetcore-9.0");
67+
var request = new HttpRequestMessage(HttpMethod.Get, $"https://corsproxy.io/?{urlEncodedRequestUrl}");
68+
69+
var apiBrowserPage = await client.SendAsync(request);
70+
71+
var metaTag = new Regex("<meta name=\"ms.assetid\" content=\"(.+?)\" />");
72+
var match = metaTag.Match(await apiBrowserPage.Content.ReadAsStringAsync()).Groups[1].Value;
73+
74+
result.Link = match.Replace("*", "%2A").Replace("`", "%60");
9575
}
9676
}
9777
else
9878
{
9979
message = "Request failed.";
10080
}
10181

102-
Console.WriteLine("HERE 2 !!!!!!!!!!!!");
103-
10482
searchText = string.Empty;
105-
message = string.Empty;
10683

10784
StateHasChanged();
108-
109-
Console.WriteLine("HERE 3 !!!!!!!!!!!!");
110-
111-
11285
}
11386

11487
public class SearchResults
@@ -119,12 +92,9 @@
11992
public class Result
12093
{
12194
public string? DisplayName { get; set; }
95+
public string? Url { get; set; }
12296
public string? ItemType { get; set; }
123-
public string? ItemKind { get; set; }
12497
public string? Description { get; set; }
12598
public string? Link { get; set; }
126-
public string? Id1 { get; set; }
127-
public string? Id2 { get; set; }
128-
public string? Id3 { get; set; }
12999
}
130100
}

BlazorWebAssemblyXrefGenerator/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
1010

11-
1211
builder.Services.AddHttpClient("APIClient", client => client.BaseAddress = new Uri("https://learn.microsoft.com"));
1312

13+
builder.Services.AddHttpClient("APIBrowserPageClient", client => client.BaseAddress = new Uri("https://learn.microsoft.com/en-us"));
14+
1415
await builder.Build().RunAsync();

BlazorWebAssemblyXrefGenerator/wwwroot/css/app.css

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ html, body {
22
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
33
}
44

5+
h1 {
6+
font-size: 2.0em;
7+
}
8+
59
h1:focus {
610
outline: none;
711
}
@@ -10,6 +14,11 @@ a, .btn-link {
1014
color: #0071c1;
1115
}
1216

17+
input {
18+
margin-bottom: 5px;
19+
width: 100%
20+
}
21+
1322
.btn-primary {
1423
color: #fff;
1524
background-color: #1b6ec2;
@@ -111,4 +120,4 @@ code {
111120

112121
.form-floating > .form-control-plaintext:focus::placeholder, .form-floating > .form-control:focus::placeholder {
113122
text-align: start;
114-
}
123+
}

BlazorWebAssemblyXrefGenerator/wwwroot/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@
8080
}
8181
});
8282
</script>
83+
<script>
84+
function copyToClipboard(e) {
85+
var copyText = document.getElementById(e);
86+
copyText.select();
87+
navigator.clipboard.writeText(copyText.value);
88+
}
89+
</script>
8390
</body>
8491

8592
</html>

0 commit comments

Comments
 (0)