From 505434e4ff68413adcacbf84c402d3d3b4247d76 Mon Sep 17 00:00:00 2001 From: niltor Date: Mon, 25 Mar 2024 11:40:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86base=20url?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + Lib/BuildSite/HtmlBuilder.cs | 49 ++++-- README.md | 20 ++- ...\345\212\241\345\256\236\347\216\260.html" | 123 -------------- src/wwwroot/blogs/C#/csharp.html | 16 -- src/wwwroot/blogs/blazor.html | 105 ------------ src/wwwroot/blogs/introduction.html | 16 -- src/wwwroot/blogs/test.html | 12 -- ...\351\200\237\345\205\245\351\227\250.html" | 150 ------------------ .../\351\232\217\347\254\224.html" | 20 --- src/wwwroot/data/blogs.json | 76 --------- src/wwwroot/data/webinfo.json | 5 - 12 files changed, 57 insertions(+), 537 deletions(-) delete mode 100644 "src/wwwroot/blogs/6.3 \344\270\232\345\212\241\345\256\236\347\216\260.html" delete mode 100644 src/wwwroot/blogs/C#/csharp.html delete mode 100644 src/wwwroot/blogs/blazor.html delete mode 100644 src/wwwroot/blogs/introduction.html delete mode 100644 src/wwwroot/blogs/test.html delete mode 100644 "src/wwwroot/blogs/\345\277\253\351\200\237\345\205\245\351\227\250.html" delete mode 100644 "src/wwwroot/blogs/\351\232\217\347\254\224/\351\232\217\347\254\224.html" delete mode 100644 src/wwwroot/data/blogs.json delete mode 100644 src/wwwroot/data/webinfo.json diff --git a/.gitignore b/.gitignore index 8ecd13e..42049c1 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,8 @@ # User-specific files _site/ +src/wwwroot/blogs/ +src/wwwroot/data/ *.rsuser *.suo *.user diff --git a/Lib/BuildSite/HtmlBuilder.cs b/Lib/BuildSite/HtmlBuilder.cs index c24877e..979d632 100644 --- a/Lib/BuildSite/HtmlBuilder.cs +++ b/Lib/BuildSite/HtmlBuilder.cs @@ -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 files = Directory.EnumerateFiles(ContentPath, "*.md", SearchOption.AllDirectories) + .ToList(); try { - MarkdownPipeline pipeline = new MarkdownPipelineBuilder() - .UseAdvancedExtensions() - .UseBetterCodeBlock() - .Build(); - - List 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(@""); + var match = regex.Match(content); + if (match.Success) + { + return match.Groups[1].Value; + } + + } + return "/"; + } /// /// 获取标题 @@ -142,6 +164,7 @@ private string AddHtmlTags(string content, string title = "") + {title} diff --git a/README.md b/README.md index 46e5003..72a2dba 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,26 @@ 点击Fork按钮,并创建自己的仓库。 -修改 base +### 修改 base href +当你使用Github Page或使用ISS子应用部署时,需要调整base href。 + +该文件位于`Blog/wwwroot/index.html`。 + +请将该值调整成你的子目录名称,如`/blazor-blog/`,请注意,尾部的`/`是必需的。 + +```html + +``` + +如果你使用自定义域名,且没有子目录,则将该值保持为`/`。 + +```html + +``` + +> [!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)来了解更多内容。 ## 自定义 diff --git "a/src/wwwroot/blogs/6.3 \344\270\232\345\212\241\345\256\236\347\216\260.html" "b/src/wwwroot/blogs/6.3 \344\270\232\345\212\241\345\256\236\347\216\260.html" deleted file mode 100644 index 628a9b1..0000000 --- "a/src/wwwroot/blogs/6.3 \344\270\232\345\212\241\345\256\236\347\216\260.html" +++ /dev/null @@ -1,123 +0,0 @@ - - - - - - - - - - -

业务实现

-

我们通过工具生成了针对实体模型最常见的增删查改逻辑和接口,但生成的内容不一定符合实际要求,所以我们要对每个接口进行确认和修改。

-

调整和实现业务逻辑和接口

-

主题

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
操作管理员用户
筛选查询
详情
添加
修改
删除
-

主题选项

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
操作管理员用户
筛选查询
详情
添加
修改
删除
投票
-

用户

- - - - - - - - - - - - - - - - - - - - - - - - - -
操作用户
注册
登录
补全信息
修改个人信息
- - - - \ No newline at end of file diff --git a/src/wwwroot/blogs/C#/csharp.html b/src/wwwroot/blogs/C#/csharp.html deleted file mode 100644 index 6ee0a27..0000000 --- a/src/wwwroot/blogs/C#/csharp.html +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - -

CSharp

-

🎊 C# 太好了

- - - - \ No newline at end of file diff --git a/src/wwwroot/blogs/blazor.html b/src/wwwroot/blogs/blazor.html deleted file mode 100644 index 7c84527..0000000 --- a/src/wwwroot/blogs/blazor.html +++ /dev/null @@ -1,105 +0,0 @@ - - - - - - - - - - -

创建Blazor项目

-

FluentUI

-

使用TailwindCSS

-

安装

-

npx tailwindcss init

-

配置 tailwind.config.js

-
-module.exports = {
-  content: ['./**/*.{razor,html}'],
-  theme: {
-    extend: {},
-  },
-  plugins: [],
-}
-
-
-

创建一个开发时使用的app.css,并引入基础组件

-
-using System.Text;
-using Markdig;
-using Markdown.ColorCode;
-using Markdown.ColorCode.CSharpToColoredHtml;
-namespace BuildSite;
-public class HtmlBuilder
-{
-    public void BuildBlogs()
-    {
-        var contentPath = Path.Combine(Environment.CurrentDirectory, "..", "Content");
-        var wwwrootPath = Path.Combine(Environment.CurrentDirectory, "..", "src", "wwwroot");
-        try
-        {
-            var pipeline = new MarkdownPipelineBuilder()
-                .UseAdvancedExtensions()
-                .UseColorCodeWithCSharpToColoredHtml(HtmlFormatterType.CssWithCSharpToColoredHtml)
-                .Build();
-            var files = Directory.EnumerateFiles(contentPath, "*.md", SearchOption.AllDirectories)
-                .ToList();
-            files.ForEach(file =>
-                {
-                    var markdown = File.ReadAllText(file);
-                    var html = Markdig.Markdown.ToHtml(markdown, pipeline);
-                    var relativePath = file.Replace(contentPath, wwwrootPath).Replace(".md", ".html");
-                    html = AddHtmlTags(html);
-                    var dir = Path.GetDirectoryName(relativePath);
-                    if (!Directory.Exists(dir))
-                    {
-                        Directory.CreateDirectory(dir);
-                    }
-                    File.WriteAllText(relativePath, html, Encoding.UTF8);
-                });
-        }
-        catch (Exception e)
-        {
-            Console.WriteLine(e.ToString());
-            throw;
-        }
-    }
-    private string AddHtmlTags(string content, string title = "")
-    {
-        var res = $"""
-            <!DOCTYPE html>
-            <html>
-            <head>
-              <meta charset="UTF-8">
-              <meta name="viewport" content="width=device-width, initial-scale=1.0">
-              <link rel="stylesheet" href="css/app.css">
-              <title>{title}</title>
-            </head>
-            <body>
-            {content}
-            </body>
-            </html>
-            """;
-        return res;
-    }
-}
-
-
-

Css

-
-@tailwind base;
-@tailwind components;
-@tailwind utilities;
-
-
-

开启监测模式,并设置输出路径

-

npx tailwindcss -i ./wwwroot/css/input.css -o ./wwwroot/css/output.css --watch

-

使用FluentUI

-

安装

-

Microsoft.FluentUI.AspNetCore.Components

-

Microsoft.FluentUI.AspNetCore.Components.Icons

- - - - \ No newline at end of file diff --git a/src/wwwroot/blogs/introduction.html b/src/wwwroot/blogs/introduction.html deleted file mode 100644 index 16d4a56..0000000 --- a/src/wwwroot/blogs/introduction.html +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - -

概要介绍

-

这是一个博客系统

- - - - \ No newline at end of file diff --git a/src/wwwroot/blogs/test.html b/src/wwwroot/blogs/test.html deleted file mode 100644 index e46e106..0000000 --- a/src/wwwroot/blogs/test.html +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - -
test
- - - diff --git "a/src/wwwroot/blogs/\345\277\253\351\200\237\345\205\245\351\227\250.html" "b/src/wwwroot/blogs/\345\277\253\351\200\237\345\205\245\351\227\250.html" deleted file mode 100644 index dfd912d..0000000 --- "a/src/wwwroot/blogs/\345\277\253\351\200\237\345\205\245\351\227\250.html" +++ /dev/null @@ -1,150 +0,0 @@ - - - - - - - - - - -

快速入门

-

前置条件:您已经安装并创建了项目模板

-

检查配置文件

-

模板默认使用PostgreSQL作为关系型数据库,使用Redis作为分布式缓存支持。

-

你需要在appsettings.json中配置连接字符串,如果需要,可自行替换默认数据库。

-

定义实体模型

-
    -
  • Entity项目下定义实体模型
  • -
  • EntityFramework项目下定义数据库上下文和DbSet
  • -
-
-

Important

-

请遵循Entity Framework Core的官方文档,对模型及关联关系进行定义。

-
-
-

Warning

-

Dangerous certain consequences of an action

-
-
-

Caution

-

Negative potential consequences of an action

-
-

生成基础代码

-

使用dry studio生成dto,Manager,Controller等基础代码。

-
-

Tip

-

关于dry cli的使用,可查看dry cli文档

-
-

实现自定义业务逻辑

-

Application项目Manager目录中实现业务逻辑,通常包括 筛选查询,添加实体,更新实体

-

筛选查询

-

构建自定义查询条件的步骤:

-
    -
  1. 构造自己的查询条件Queryable
  2. -
  3. 调用FilterAsync<T>方法获取结果
  4. -
-

代码示例:

-
-public override async Task<PageList<DiscussItemDto>> FilterAsync(DiscussFilterDto filter)
-{
-    // 根据实际业务构建筛选条件
-    if (filter.IsTop != null)
-    {
-        Queryable = Queryable.Where(q => q.IsTop == filter.IsTop);
-    }
-    if (filter.MemberId != null)
-    {
-        Queryable = Queryable.Where(q => q.Member.Id == filter.MemberId);
-    }
-    return await Query.FilterAsync<DiscussItemDto>(Queryable, filter.PageIndex, filter.PageSize);
-}
-
-
-

新增实体

-

代码示例:

-
-public override async Task<Discuss> AddAsync(Discuss entity)
-{
-    var res = await Command.CreateAsync(entity);
-    // TODO: do something
-    // 提交更新
-    await Command.SaveChangeAsync();
-    return res;
-}
-
-
-

更新实体

-

Manager提供了GetCurrentAsync方法来获取当前(可写数据库上下文)的实体。

-

在控制器中,会先获取实体,如果不存在,则直接返回404

-

实体更新的方法传递两个参数,一个是实体本身,一个是提交的DTO对象, -实体本身是在控制器当中使用GetCurrentAsync方法获取到的,直接作为参数传递过去即可。

-

代码示例:

-

以下方面演示如何更新关联的内容。

-
-public override async Task<ResourceTypeDefinition> UpdateAsync(ResourceTypeDefinition entity, ResourceTypeDefinitionUpdateDto dto)
-    {
-        // 更新关联的内容
-        entity!.AttributeDefines = null;
-        if (dto.AttributeDefineIds != null)
-        {
-            // 通过父类的 Stores 查询其他实体的内容
-            var attributeDefines = await Stores.ResourceAttributeDefineCommand.Db.Where(a => dto.AttributeDefineIds.Contains(a.Id)).ToListAsync();
-            entity.AttributeDefines = attributeDefines;
-        }
-        return await base.UpdateAsync(entity, dto);
-    }
-
-
-

详情查询

-

Manager提供了默认的详情查询方法,可直接传递查询条件.

-

若自定义查询,如查询关联的内容,需要添加新的方法来实现.

-

Manager提供了Query.Db成员,可直接对当前模型进行查询。

-

代码示例:

-
-public async Task<ResourceGroup?> FindAsync(Guid id)
-{
-    return await Query.Db
-        .Include(g => g.Environment)
-        .FirstOrDefaultAsync(g => g.Id == id);
-}
-
-
-

删除处理

-

删除默认为软删除,如果想修改该行为,可在自己的Manager类中将EnableSoftDelete设置为false。

-

Command.EnableSoftDelete = false;

-

删除有时会涉及到关联删除,示例代码:

-
-public override async Task<Resource?> DeleteAsync(Guid id)
-{
-    var resource = entity;
-    if (resource!.Attributes != null)
-    {
-        Stores.CommandContext.RemoveRange(resource.Attributes);
-    }
-    return await DeleteAsync(resource);
-}
-
-
-

在控制器中调用业务逻辑

-

当你在Application中实现了业务接口和实现后,可在Http.API中的接口类中通过依赖注入的方式调用相关的业务方法。

-
-

Note

-

请保持控制器的代码整洁,避免直接在控制器进行 查询数据库、实现业务逻辑等操作。

-
-

执行迁移

-

当你新增或修改了实体之后,你需要执行数据库迁移,以使数据库结构更改生效。

-

你可以使用dotnet ef命令进行生成迁移脚本操作。

-
-

Important

-

需要在Http.API项目下进行数据库迁移的操作。

-
-

或者你可以直接执行我们提供的迁移生成脚本script/EFMigrations.ps1

-
-

Tip

-

你不需要执行dotnet ef update命令,因为程序在启动时,会自动提交变更。

-
- - - - \ No newline at end of file diff --git "a/src/wwwroot/blogs/\351\232\217\347\254\224/\351\232\217\347\254\224.html" "b/src/wwwroot/blogs/\351\232\217\347\254\224/\351\232\217\347\254\224.html" deleted file mode 100644 index 5e1fc6b..0000000 --- "a/src/wwwroot/blogs/\351\232\217\347\254\224/\351\232\217\347\254\224.html" +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - -

随笔

-

我的随笔

-
-

Note

-

仅测试

-
- - - - \ No newline at end of file diff --git a/src/wwwroot/data/blogs.json b/src/wwwroot/data/blogs.json deleted file mode 100644 index e556913..0000000 --- a/src/wwwroot/data/blogs.json +++ /dev/null @@ -1,76 +0,0 @@ -{ - "Name": "Root", - "Children": [ - { - "Name": "C#", - "Children": [], - "Blogs": [ - { - "Title": "csharp", - "Path": "/C%23/csharp.html", - "FileName": "csharp.md", - "PublishTime": "2024-03-20T10:57:01.2938617+08:00", - "CreatedTime": "2024-03-20T15:12:02.973836+08:00", - "UpdatedTime": "2024-03-20T10:57:01.2938617+08:00", - "Catalog": null - } - ], - "Parent": null - }, - { - "Name": "随笔", - "Children": [], - "Blogs": [ - { - "Title": "随笔", - "Path": "/%E9%9A%8F%E7%AC%94/%E9%9A%8F%E7%AC%94.html", - "FileName": "随笔.md", - "PublishTime": "2024-03-20T10:55:39.7196236+08:00", - "CreatedTime": "2024-03-20T15:12:02.9773445+08:00", - "UpdatedTime": "2024-03-20T10:55:39.7196236+08:00", - "Catalog": null - } - ], - "Parent": null - } - ], - "Blogs": [ - { - "Title": "6.3 业务实现", - "Path": "/6.3%20%E4%B8%9A%E5%8A%A1%E5%AE%9E%E7%8E%B0.html", - "FileName": "6.3 业务实现.md", - "PublishTime": "2024-01-24T14:16:24.276768+08:00", - "CreatedTime": "2024-03-23T16:01:00.7945925+08:00", - "UpdatedTime": "2024-01-24T14:16:24.276768+08:00", - "Catalog": null - }, - { - "Title": "blazor", - "Path": "/blazor.html", - "FileName": "blazor.md", - "PublishTime": "2024-03-20T16:04:08.9405377+08:00", - "CreatedTime": "2024-03-20T15:16:03.7830136+08:00", - "UpdatedTime": "2024-03-20T16:04:08.9405377+08:00", - "Catalog": null - }, - { - "Title": "introduction", - "Path": "/introduction.html", - "FileName": "introduction.md", - "PublishTime": "2024-03-20T10:56:15.858561+08:00", - "CreatedTime": "2024-03-20T15:12:02.9728384+08:00", - "UpdatedTime": "2024-03-20T10:56:15.858561+08:00", - "Catalog": null - }, - { - "Title": "快速入门", - "Path": "/%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8.html", - "FileName": "快速入门.md", - "PublishTime": "2024-03-23T17:07:57.0421509+08:00", - "CreatedTime": "2024-03-23T09:52:32.8608535+08:00", - "UpdatedTime": "2024-03-23T17:07:57.0421509+08:00", - "Catalog": null - } - ], - "Parent": null -} \ No newline at end of file diff --git a/src/wwwroot/data/webinfo.json b/src/wwwroot/data/webinfo.json deleted file mode 100644 index 8fd214c..0000000 --- a/src/wwwroot/data/webinfo.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "Name": "Niltor Blog", - "Description": "My Blog - Powered by Ater Blog", - "AuthorName": "Ater" -} \ No newline at end of file