Skip to content

Commit

Permalink
add base url
Browse files Browse the repository at this point in the history
  • Loading branch information
niltor committed Mar 29, 2024
1 parent 8d528fb commit 73fcd5c
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 29 deletions.
24 changes: 4 additions & 20 deletions Lib/BuildSite/HtmlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public HtmlBuilder(string input, string output)

public void BuildWebSite()
{
GetBaseUrl();
BuildData();
BuildBlogs();
BuildIndex();
Expand All @@ -44,7 +45,6 @@ public void BuildWebSite()
/// </summary>
public void BuildBlogs()
{
BaseUrl = GetBaseUrl();
// 配置markdown管道
MarkdownPipeline pipeline = new MarkdownPipelineBuilder()
.UseAutoIdentifiers(Markdig.Extensions.AutoIdentifiers.AutoIdentifierOptions.GitHub)
Expand Down Expand Up @@ -127,6 +127,7 @@ public void BuildIndex()
var siderBarHtml = GenSiderBar(rootCatalog);

indexHtml = indexHtml.Replace("@{Name}", webInfo.Name)
.Replace("@{BaseUrl}", BaseUrl)
.Replace("@{Description}", webInfo.Description)
.Replace("@{blogList}", blogHtml)
.Replace("@{siderbar}", siderBarHtml);
Expand All @@ -135,22 +136,6 @@ public void BuildIndex()
}
}

/// <summary>
/// 处理index.html中的base href
/// </summary>
public void BuildBaseHref()
{
var webInfoPath = Path.Combine(Environment.CurrentDirectory, "webinfo.json");
var content = File.ReadAllText(webInfoPath);
var webInfo = JsonSerializer.Deserialize<WebInfo>(content);
var indexPath = Path.Combine(Output, "index.html");
var indexContent = File.ReadAllText(indexPath);
indexContent = indexContent.Replace("<base href=\"/\" />", $"<base href=\"{webInfo?.BaseHref}\" />");

Console.WriteLine($"✍️ Using {webInfo?.BaseHref} as base href!");
File.WriteAllText(indexPath, indexContent, Encoding.UTF8);
}

private void TraverseDirectory(string directoryPath, Catalog parentCatalog)
{
foreach (string subDirectoryPath in Directory.GetDirectories(directoryPath))
Expand Down Expand Up @@ -196,16 +181,15 @@ private static string GetFullPath(Catalog catalog)
}
return path.Replace("Root", "");
}
private string GetBaseUrl()
private void GetBaseUrl()
{
var webInfoPath = Path.Combine(DataPath, "webinfo.json");
if (File.Exists(webInfoPath))
{
var content = File.ReadAllText(webInfoPath);
var webInfo = JsonSerializer.Deserialize<WebInfo>(content);
return webInfo?.BaseHref ?? "/";
BaseUrl = webInfo?.BaseHref ?? "/";
}
return "/";
}

/// <summary>
Expand Down
6 changes: 0 additions & 6 deletions Lib/BuildSite/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@
var output = args.Skip(1).FirstOrDefault() ?? "./_site";
var environment = args.Skip(2).FirstOrDefault() ?? "Development";


var builder = new HtmlBuilder(input, output);

try
{
builder.BuildWebSite();

if (environment.Equals("Production", StringComparison.OrdinalIgnoreCase))
{
//builder.BuildBaseHref();
}
}
catch (Exception e)
{
Expand Down
4 changes: 2 additions & 2 deletions Lib/BuildSite/TemplateContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ internal class TemplateContent
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Ater Blog</title>
<base href="/" />
<link rel="stylesheet" href="css/app.css" />
<link rel="stylesheet" href="css/markdown.css" />
<link rel="stylesheet" href="@{BaseUrl}css/app.css" />
<link rel="stylesheet" href="@{BaseUrl}css/markdown.css" />
<link rel="icon" type="image/png" href="favicon.png" />
<script src="./js/index.js"></script>
</head>
Expand Down
2 changes: 1 addition & 1 deletion webinfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"Name": "Blazor Blog",
"Description": "Blazor Blog - Powered by Ater Blog",
"AuthorName": "NilTor",
"BaseHref": "/"
"BaseHref": "/blazor-blog/"
}

0 comments on commit 73fcd5c

Please sign in to comment.