Skip to content

Commit

Permalink
Merge branch 'up-net7'
Browse files Browse the repository at this point in the history
  • Loading branch information
xxxxue committed Nov 26, 2022
2 parents c80c971 + 818a409 commit 470c758
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 131 deletions.
11 changes: 10 additions & 1 deletion DocsifyBuildSidebar/Config/Config.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
{
"HomePath": "E:\\Work\\coding\\xxxue"
"HomePath": "E:\\Work\\coding\\xxxue",
"IgnoreFile": [

],
"IgnoreDir": [
".git"
],
"IgnoreDirNameContain": [
".assets"
]
}
27 changes: 13 additions & 14 deletions DocsifyBuildSidebar/DocsifyBuildSidebar.csproj
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TrimMode>partial</TrimMode>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Spectre.Console" Version="0.32.1" />
<PackageReference Include="Spectre.Console.ImageSharp" Version="0.32.1" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Spectre.Console" Version="0.45.0" />
</ItemGroup>

<ItemGroup>
<None Update="Config\Config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Update="Config\Config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
79 changes: 0 additions & 79 deletions DocsifyBuildSidebar/JsonConfigHelper.cs

This file was deleted.

18 changes: 18 additions & 0 deletions DocsifyBuildSidebar/MyConfigModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace DocsifyBuildSidebar
{
public class MyConfigModel
{
public string HomePath { get; set; }

public List<string> IgnoreFile { get; set; }
public List<string> IgnoreDir { get; set; }
public List<string> IgnoreDirNameContain { get; set; }
}
}
70 changes: 38 additions & 32 deletions DocsifyBuildSidebar/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;

using Spectre.Console;

Expand All @@ -18,23 +15,24 @@ internal class Program
private static string _sidebarFileName = "_sidebar.md";
private static string _readmeFileName = "README.md";
private static string _jsonConfigPath = "./Config/Config.json";
private static List<string> _ignoreDirList = new List<string>(){
".git", //git目录
};

private static List<string> _ignoreFileList = new List<string>(){
// 规则: 全名相等
private static List<string> _ignoreFileList = new()
{
"_sidebar.md", // 侧边栏文件
"README.md",//侧边栏文件
};
"README.md" //侧边栏文件
};

// 规则: 全名相等
private static List<string> _ignoreDirList = new();

private static List<string> _ignoreStringList = new List<string>(){
".assets" //Typora软件 存放图片的目录 *.assets
};
// 规则: 名称包含
private static List<string> _ignoreDirNameContainList = new();

/// <summary>
/// 记录包含的子目录, 在这些目录中生成侧边栏文件
/// </summary>
private static List<string> _includeDirList = new List<string>();
private static List<string> _includeDirList = new();

/// <summary>
/// 文档目录级别
Expand All @@ -44,11 +42,10 @@ internal class Program
/// <summary>
/// 不符合规则的文件 (发出警告)
/// </summary>
private static List<string> _warnFileList = new List<string>();
private static List<string> _warnFileList = new();

private static string Entry(string rootPath, bool isHome = false)
{

var rootDir = new DirectoryInfo(rootPath);
var sidebarData = string.Empty;

Expand Down Expand Up @@ -79,7 +76,6 @@ private static string Entry(string rootPath, bool isHome = false)
{
sortDirList.Add(item);
}

}
fileList.Clear();
// 先放入 文件夹, 再放入 文件
Expand All @@ -105,16 +101,14 @@ private static string Entry(string rootPath, bool isHome = false)
continue;
}


sidebarData += $"{Utils.GenerateSpace(_level)}- [{file.GetFileNameWithoutExtension()}]({Utils.ReplaceSpace(file.GetFileRelativePath())})\n";

}
else if (Utils.IsDir(item))
{
// 文件夹处理
var dir = new DirectoryInfo(item);
// 检查忽略
if (!_ignoreDirList.Contains(dir.Name) && !_ignoreStringList.Exists(igString => dir.Name.Contains(igString)))
if (!_ignoreDirList.Contains(dir.Name) && !_ignoreDirNameContainList.Exists(igString => dir.Name.Contains(igString)))
{
// 只有 home path 记录子目录, 生成子目录时不需要.
if (isHome)
Expand All @@ -138,9 +132,7 @@ private static void Build()
// 生成home目录的 侧边栏
var homeData = Entry(_homePath, true);

// Console.WriteLine($"home menu :\n{homeData}");
WriteDataToFile(_homePath, homeData);
//Utils.WriteLogMessage("[home] Done!");

// 生成 子目录的 侧边栏
foreach (var item in _includeDirList)
Expand All @@ -158,9 +150,8 @@ private static void Build()
{
includeData = $"- [返回上一级 [{parentDir.Name}]]({parentDir.GetDirRelativePath()})\n" + includeData;
}
WriteDataToFile(item, includeData);
//Console.WriteLine($"child menu :\n{includeData}");

WriteDataToFile(item, includeData);
}
}

Expand All @@ -178,23 +169,32 @@ private static void WriteDataToFile(string homePath, string data)
File.WriteAllText(readmePath, data);
}



private static void Init()
{
AnsiConsole.MarkupLine("[yellow]Start ReadConfig...[/]");
var configJson = new JsonConfigHelper(_jsonConfigPath);

var homePath = configJson["HomePath"];
// 读取 Config.json
var fileData = File.ReadAllText(_jsonConfigPath);

if (string.IsNullOrWhiteSpace(homePath))
// json 转为对象
var config = JsonSerializer.Deserialize<MyConfigModel>(fileData);

if (string.IsNullOrWhiteSpace(config.HomePath))
{
throw new Exception("Config.json HomePath 获取失败");
}
_homePath = homePath;

// 将 Config.json 内容保存到全局变量中
_homePath = config.HomePath;
_ignoreFileList.AddRange(config.IgnoreFile);
_ignoreDirList.AddRange(config.IgnoreDir);
_ignoreDirNameContainList.AddRange(config.IgnoreDirNameContain);

Utils.WriteLogMessage("HomePath: " + _homePath);
AnsiConsole.MarkupLine("[yellow]ReadConfig Done![/]");
Utils.WriteDivider();

}

private static void ShowWarnFileList()
Expand All @@ -210,16 +210,22 @@ private static void ShowWarnFileList()
AnsiConsole.MarkupLine($"[yellow]{item}[/]");
}
}

private static void Main(string[] args)
{
try
{
Utils.ShowLogo();
AnsiConsole.MarkupLine("[yellow]Initializing sidebar[/]...");

// 初始化配置项
Init();

// 开始构建
Build();

ShowWarnFileList();

Utils.WriteDivider();

AnsiConsole.MarkupLine($"○ [green]{_homePath} ->>> Done![/]");
Expand All @@ -232,6 +238,6 @@ private static void Main(string[] args)
Console.ReadLine();
}
}

}

}
4 changes: 2 additions & 2 deletions DocsifyBuildSidebar/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ public static void ShowLogo()
var logo = new FigletText("build sidebar")
.LeftAligned()
.Color(Color.Yellow);
AnsiConsole.Render(logo);
AnsiConsole.Write(logo);

var rule = new Rule("[red]build sidebar for c#[/]");
rule.Alignment = Justify.Center;
rule.Style = Style.Parse("red dim");

AnsiConsole.Render(rule);
AnsiConsole.Write(rule);
}
}
}
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ Releases中文件支持 3大主流平台, golang可执行文件较小,方便下

# 使用方法:
>
> 1. 在github页面右侧的"Releases" 中下载编译好的软件
> 2. 修改Config中的`HomePath``自己电脑上的目标根目录`
> 3. 双击exe执行
> 1. 在 github 页面右侧的 "Releases" 中下载编译好的软件
> 2. 修改 Config 中的 `HomePath` `自己电脑上的目标根目录`
> 3. 双击 exe 执行
# 小技巧:
可以右键单击exe,创建一个 快捷方式,
Expand All @@ -35,6 +35,18 @@ Releases中文件支持 3大主流平台, golang可执行文件较小,方便下
以后就不用每次都去找 exe 了



# Config.json 配置项

| 名词 | 说明 | 例子 |
| -------------------- | ------------------------------------------ | -------------------------- |
| HomePath | Docsify 项目的根目录 | `E:\\Work\\coding\\xxxue` |
| IgnoreFile | 忽略的文件 (判断规则: 相等) | `Readme.md` |
| IgnoreDir | 忽略的目录 (判断规则: 相等) | `.git` |
| IgnoreDirNameContain | 忽略的目录 (判断规则:目录名称任意位置包含) | `.assets` 匹配 `*.assets*` |



# 首页效果

```markdown
Expand Down

0 comments on commit 470c758

Please sign in to comment.