Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add title to movie links in wiki #1750

Merged
merged 4 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions TASVideos.WikiEngine/MakeBracketed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,14 @@ private static IEnumerable<INode> MakeLinkOrImage(int charStart, int charEnd, st
{
if (pp.Length >= 2)
{
// same as the IsLink(pp[0]) && pp.Length >= 2 case, except add the '=' because it was implicitly resolved to an internal link
return new[] { MakeLink(charStart, charEnd, NormalizeUrl("=" + pp[0]), new Text(charStart, pp[1]) { CharEnd = charEnd }) };
// These need DB lookup for title attributes in some cases
return MakeModuleInternal(charStart, charEnd, "__wikiLink|href=" + NormalizeUrl("=" + pp[0]) + "|displaytext=" + pp[1]);
}

// If no labeling text was needed, a module is needed for DB lookups (eg `[4022S]`)
// DB lookup will be required for links like [4022S], so use __wikiLink
// TODO: __wikilink should probably be its own AST type??
return MakeModuleInternal(charStart, charEnd, "__wikiLink|href=" + NormalizeUrl("=" + pp[0]) + "|displaytext=" + pp[0]);
return MakeModuleInternal(charStart, charEnd, "__wikiLink|href=" + NormalizeUrl("=" + pp[0]));
}

// In other cases, return raw literal text. This doesn't quite match the old wiki, which could look for formatting in these, but should be good enough
Expand Down
2 changes: 1 addition & 1 deletion TASVideos/Pages/Shared/Components/WikiLink/Default.cshtml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
@model WikiLinkModel
<a href="@Model.Href">@Model.DisplayText</a>
<a href="@Model.Href" title="@Model.Title">@Model.DisplayText</a>
1 change: 1 addition & 0 deletions TASVideos/ViewComponents/Models/WikiLinkModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public class WikiLinkModel
{
public string Href { get; set; } = "";
public string DisplayText { get; set; } = "";
public string? Title { get; set; }
}
53 changes: 40 additions & 13 deletions TASVideos/ViewComponents/WikiLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,58 @@ public WikiLink(ApplicationDbContext db)

public async Task<IViewComponentResult> InvokeAsync(string href, string? displayText)
{
var model = new WikiLinkModel
{
Href = href,
DisplayText = string.IsNullOrWhiteSpace(displayText)
? href[1..] // almost always want to chop off the leading '/'
: displayText
};

int? id;
string? titleText = null;

if (model.DisplayText.StartsWith("user:"))
if (displayText?.StartsWith("user:") == true)
{
model.DisplayText = model.DisplayText[5..];
displayText = displayText[5..];
}
else if ((id = SubmissionHelper.IsSubmissionLink(href)).HasValue)
{
var title = await GetSubmissionTitle(id.Value);
if (!string.IsNullOrWhiteSpace(title))
{
model.DisplayText = title;
titleText = title;
}
}
else if ((id = SubmissionHelper.IsPublicationLink(href)).HasValue)
{
var title = await GetPublicationTitle(id.Value);
if (!string.IsNullOrWhiteSpace(title))
{
model.DisplayText = $"[{id.Value}] " + title;
titleText = $"[{id.Value}] " + title;
}
}
else if ((id = SubmissionHelper.IsGamePageLink(href)).HasValue)
{
var title = await GetGameTitle(id.Value);
if (!string.IsNullOrWhiteSpace(title))
{
titleText = title;
}
}

return View(model);
if (titleText != null)
{
if (string.IsNullOrWhiteSpace(displayText))
{
displayText = titleText;
titleText = null;
}
}

if (string.IsNullOrWhiteSpace(displayText))
{
displayText = href[1..];
}

return View(new WikiLinkModel
{
Href = href,
DisplayText = displayText,
Title = titleText,
});
}

private async Task<string?> GetPublicationTitle(int id)
Expand All @@ -64,4 +84,11 @@ public async Task<IViewComponentResult> InvokeAsync(string href, string? display
.Select(s => new { s.Id, s.Title })
.SingleOrDefaultAsync(s => s.Id == id))?.Title;
}

private async Task<string?> GetGameTitle(int id)
{
return (await _db.Games
.Select(g => new { g.Id, g.DisplayName })
.SingleOrDefaultAsync(g => g.Id == id))?.DisplayName;
}
}
Loading