Skip to content

Commit

Permalink
处理base url
Browse files Browse the repository at this point in the history
niltor committed Mar 25, 2024
1 parent 8781cb9 commit 505434e
Showing 12 changed files with 57 additions and 537 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -5,6 +5,8 @@

# User-specific files
_site/
src/wwwroot/blogs/
src/wwwroot/data/
*.rsuser
*.suo
*.user
49 changes: 36 additions & 13 deletions Lib/BuildSite/HtmlBuilder.cs
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ public partial class HtmlBuilder
public string ContentPath { get; init; }
public string DataPath { get; init; }
public string WwwrootPath { get; init; }
public string BaseUrl { get; set; } = "/";

private readonly JsonSerializerOptions _jsonSerializerOptions = new()
{
@@ -31,16 +32,18 @@ public HtmlBuilder()
}
public void BuildBlogs()
{
BaseUrl = GetBaseUrl();
// 配置markdown管道
MarkdownPipeline pipeline = new MarkdownPipelineBuilder()
.UseAdvancedExtensions()
.UseBetterCodeBlock()
.Build();

// 读取所有要处理的md文件
List<string> files = Directory.EnumerateFiles(ContentPath, "*.md", SearchOption.AllDirectories)
.ToList();
try
{
MarkdownPipeline pipeline = new MarkdownPipelineBuilder()
.UseAdvancedExtensions()
.UseBetterCodeBlock()
.Build();

List<string> files = Directory.EnumerateFiles(ContentPath, "*.md", SearchOption.AllDirectories)
.ToList();

files.ForEach(file =>
{
string markdown = File.ReadAllText(file);
@@ -57,9 +60,8 @@ public void BuildBlogs()
File.WriteAllText(relativePath, html, Encoding.UTF8);
});
}
catch (Exception e)
catch (Exception)
{
Console.WriteLine(e.ToString());
throw;
}
}
@@ -70,12 +72,15 @@ public void BuildData()
TraverseDirectory(ContentPath, rootCatalog);
string json = JsonSerializer.Serialize(rootCatalog, _jsonSerializerOptions);

if (!Directory.Exists(DataPath))
{
Directory.CreateDirectory(DataPath);
}
string blogData = Path.Combine(DataPath, "blogs.json");
File.WriteAllText(blogData, json, Encoding.UTF8);
}


public void TraverseDirectory(string directoryPath, Catalog parentCatalog)
private void TraverseDirectory(string directoryPath, Catalog parentCatalog)
{
foreach (string subDirectoryPath in Directory.GetDirectories(directoryPath))
{
@@ -108,7 +113,7 @@ public void TraverseDirectory(string directoryPath, Catalog parentCatalog)
}
}

public string GetFullPath(Catalog catalog)
private string GetFullPath(Catalog catalog)
{
var path = Uri.EscapeDataString(catalog.Name);
if (catalog.Parent != null)
@@ -117,6 +122,23 @@ public string GetFullPath(Catalog catalog)
}
return path.Replace("Root", "");
}
private string GetBaseUrl()
{
var indexPath = Path.Combine(WwwrootPath, "index.html");
if (File.Exists(indexPath))
{
// get base tag content
var content = File.ReadAllText(indexPath);
var regex = new Regex(@"<base href=""(.*)"" />");
var match = regex.Match(content);
if (match.Success)
{
return match.Groups[1].Value;
}

}
return "/";
}

/// <summary>
/// 获取标题
@@ -142,6 +164,7 @@ private string AddHtmlTags(string content, string title = "")
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<base href="{BaseUrl}" />
<link rel="stylesheet" href="/css/app.css">
<link rel="stylesheet" href="/css/markdown.css">
<title>{title}</title>
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -10,8 +10,26 @@

点击Fork按钮,并创建自己的仓库。

修改 base
### 修改 base href

当你使用Github Page或使用ISS子应用部署时,需要调整base href。

该文件位于`Blog/wwwroot/index.html`

请将该值调整成你的子目录名称,如`/blazor-blog/`,请注意,尾部的`/`是必需的。

```html
<base href="/blazor-blog/" />
```

如果你使用自定义域名,且没有子目录,则将该值保持为`/`

```html
<base href="/" />
```

> [!NOTE]
> 可以查看[官方文档](https://learn.microsoft.com/zh-cn/aspnet/core/blazor/host-and-deploy/?view=aspnetcore-3.1&tabs=visual-studio#configure-the-app-base-path)来了解更多内容。
## 自定义

123 changes: 0 additions & 123 deletions src/wwwroot/blogs/6.3 业务实现.html

This file was deleted.

16 changes: 0 additions & 16 deletions src/wwwroot/blogs/C#/csharp.html

This file was deleted.

105 changes: 0 additions & 105 deletions src/wwwroot/blogs/blazor.html

This file was deleted.

16 changes: 0 additions & 16 deletions src/wwwroot/blogs/introduction.html

This file was deleted.

12 changes: 0 additions & 12 deletions src/wwwroot/blogs/test.html

This file was deleted.

150 changes: 0 additions & 150 deletions src/wwwroot/blogs/快速入门.html

This file was deleted.

20 changes: 0 additions & 20 deletions src/wwwroot/blogs/随笔/随笔.html

This file was deleted.

76 changes: 0 additions & 76 deletions src/wwwroot/data/blogs.json

This file was deleted.

5 changes: 0 additions & 5 deletions src/wwwroot/data/webinfo.json

This file was deleted.

0 comments on commit 505434e

Please sign in to comment.