-
Notifications
You must be signed in to change notification settings - Fork 738
/
Copy pathHome.razor
118 lines (99 loc) · 4.77 KB
/
Home.razor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
@page "/"
@using System.Net
@using System.Text.RegularExpressions
@inject IHttpClientFactory ClientFactory
<EditForm Model="Model" OnSubmit="GetSearchResults" FormName="SearchForm">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Search</h3>
</div>
<div class="panel-body">
<div class="form-group">
<p>Provide a namespace, class, member, or <code>/dotnet/api/...</code> relative link.</p>
<InputText id="searchText" @bind-Value="Model!.SearchText" aria-label="Search Text" onfocus="this.value=''" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Search</button>
<button type="button" class="btn btn-secondary" onclick="document.getElementById('searchText').value=''">Clear</button>
</div>
</div>
</div>
</EditForm>
@if (SearchResultItems?.Results?.Count() > 0)
{
<ol style="margin-top:20px">
@foreach (var result in SearchResultItems.Results.Select((x, i) => new { Data = x, Index = i }))
{
<li style="margin-bottom:5px">
<div style="font-size:1.4em;color:green;font-weight:bold">@result.Data.DisplayName</div>
<div><b>Item Type:</b> @result.Data.ItemType</div>
<div style="width:90%"><b>Description:</b> @result.Data.Description</div>
<div><input id="m@(result.Index)0" value="<xref:@result.Data.Link>"></div>
<div><input id="m@(result.Index)1" value="<xref:@result.Data.Link?displayProperty=fullName>"></div>
<div><input id="m@(result.Index)2" value="<xref:@result.Data.Link?displayProperty=nameWithType>"></div>
<div><input id="m@(result.Index)3" value="[LINK_TEXT](xref:@result.Data.Link)"></div>
<div><input id="m@(result.Index)4" value="xref:@result.Data.Link"></div>
<div>
<button type="button" class="btn btn-primary" onclick=@($"copyToClipboard('m{result.Index}0')")>Copy</button>
<button type="button" class="btn btn-secondary" onclick=@($"copyToClipboard('m{result.Index}1')")>Copy (Full Name)</button>
<button type="button" class="btn btn-secondary" onclick=@($"copyToClipboard('m{result.Index}2')")>Copy (Name with Type)</button>
<button type="button" class="btn btn-secondary" onclick=@($"copyToClipboard('m{result.Index}3')")>Copy (Custom Link Text)</button>
<button type="button" class="btn btn-secondary" onclick=@($"copyToClipboard('m{result.Index}4')")>Copy (Member Only)</button>
</div>
</li>
}
</ol>
}
@message
@code {
[SupplyParameterFromForm]
private FormModel? Model { get; set; }
private SearchResults? SearchResultItems { get; set; }
public string? message;
protected override void OnInitialized() => Model ??= new();
public async Task GetSearchResults()
{
message = string.Empty;
var apiClient = ClientFactory.CreateClient("APIClient");
if (string.IsNullOrEmpty(Model?.SearchText) || apiClient == null)
{
return;
}
SearchResultItems = await apiClient.GetFromJsonAsync<SearchResults>($"api/apibrowser/dotnet/search?api-version=0.2&search={Model.SearchText}");
if (SearchResultItems?.Results != null)
{
foreach (var result in SearchResultItems.Results)
{
var client = ClientFactory.CreateClient();
var urlEncodedRequestUrl = WebUtility.UrlEncode($"https://learn.microsoft.com/en-us{result.Url}?view=aspnetcore-9.0");
var request = new HttpRequestMessage(HttpMethod.Get, $"https://corsproxy.io/?{urlEncodedRequestUrl}");
var apiBrowserPage = await client.SendAsync(request);
var metaTag = new Regex("<meta name=\"ms.assetid\" content=\"(.+?)\" />");
var match = metaTag.Match(await apiBrowserPage.Content.ReadAsStringAsync()).Groups[1].Value;
result.Link = match.Replace("*", "%2A").Replace("`", "%60");
}
}
else
{
message = "Request failed.";
}
Model.SearchText = string.Empty;
//StateHasChanged();
}
public class FormModel
{
public string? SearchText { get; set; } = string.Empty;
}
public class SearchResults
{
public IEnumerable<Result>? Results { get; set; }
}
public class Result
{
public string? DisplayName { get; set; }
public string? Url { get; set; }
public string? ItemType { get; set; }
public string? Description { get; set; }
public string? Link { get; set; }
}
}