Add editable portal page content

This commit is contained in:
2026-08-27 10:32:08 +08:00
parent 04a6027e1d
commit 7b009a3ddc
7 changed files with 300 additions and 22 deletions
+28 -9
View File
@@ -10,7 +10,10 @@ internal static class WebPortal
private const string TemplateFileName = "index.template.html";
private const string OutputFileName = "index.html";
private const string ServiceCardsToken = "{{NGINXMAN_SERVICE_CARDS}}";
private const string TemplateVersionMarker = "<!-- NGINXMAN_TEMPLATE_VERSION:2 -->";
private const string SiteNameToken = "{{NGINXMAN_PORTAL_SITE_NAME}}";
private const string HeadlineToken = "{{NGINXMAN_PORTAL_HEADLINE}}";
private const string IntroductionToken = "{{NGINXMAN_PORTAL_INTRODUCTION}}";
private const string TemplateVersionMarker = "<!-- NGINXMAN_TEMPLATE_VERSION:3 -->";
private static readonly UTF8Encoding Utf8WithoutBom = new(false);
public static void ExtractTo(string dataDirectory)
@@ -58,26 +61,42 @@ internal static class WebPortal
}
}
public static string RenderTo(string dataDirectory, IReadOnlyList<RouteEntry> routes)
public static string RenderTo(
string dataDirectory,
PortalContentSettings portalContent,
IReadOnlyList<RouteEntry> routes)
{
var targetDirectory = Path.Combine(dataDirectory, "www");
var templatePath = Path.Combine(targetDirectory, TemplateFileName);
if (!File.Exists(templatePath))
if (!File.Exists(templatePath) ||
!File.ReadAllText(templatePath, Encoding.UTF8).Contains(
TemplateVersionMarker,
StringComparison.Ordinal))
{
ExtractTo(dataDirectory);
}
var template = File.ReadAllText(templatePath, Encoding.UTF8);
if (!template.Contains(ServiceCardsToken, StringComparison.Ordinal))
var requiredTokens = new[]
{
ServiceCardsToken,
SiteNameToken,
HeadlineToken,
IntroductionToken
};
var missingToken = requiredTokens.FirstOrDefault(
token => !template.Contains(token, StringComparison.Ordinal));
if (missingToken is not null)
{
throw new InvalidOperationException(
$"网页模板缺少入口占位符 {ServiceCardsToken}: {templatePath}");
$"网页模板缺少占位符 {missingToken}: {templatePath}");
}
var content = template.Replace(
ServiceCardsToken,
BuildServiceCards(routes),
StringComparison.Ordinal);
var content = template
.Replace(SiteNameToken, Encode(portalContent.SiteName), StringComparison.Ordinal)
.Replace(HeadlineToken, Encode(portalContent.Headline), StringComparison.Ordinal)
.Replace(IntroductionToken, Encode(portalContent.Introduction), StringComparison.Ordinal)
.Replace(ServiceCardsToken, BuildServiceCards(routes), StringComparison.Ordinal);
var outputPath = Path.Combine(targetDirectory, OutputFileName);
File.WriteAllText(outputPath, content, Utf8WithoutBom);
return outputPath;