forked from veleek/til
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Update-Readme.ps1
83 lines (72 loc) · 2.67 KB
/
Update-Readme.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
function Get-TilInfo()
{
$categoryLookup = @{
"csharp" = "C#";
"uwp" = "UWP";
"caliburn" = "Caliburn.Micro";
"git" = "Git";
"js" = "Javascript";
"linux" = "Linux";
"markdown" = "Markdown";
"mongo" = "Mongo";
"powershell" = "Powershell";
"python" = "Python";
"unity" = "Unity";
"vs" = "Visual Studio";
"windows" = "Windows";
}
$tilFiles = Get-ChildItem . -Recurse -Filter *.md | ? { $_.DirectoryName -ne $PSScriptRoot}
foreach($tilFile in $tilFiles)
{
$path = $tilFile.FullName.Replace("\", "/").Replace("$($PSScriptRoot)/", "")
$categoryParts = (Split-Path -Parent $path) -split "/"
$category = $categoryLookup[$categoryParts[0] ?? ""]
$subCategory = $categoryLookup[$categoryParts[1] ?? ""]
$title = (Get-Content $tilFile -TotalCount 1).TrimStart("# ")
@{
"fullPath" = $tilFile;
"path" = $path;
"category" = $category;
"subCategory" = $subCategory;
"title" = $title
}
}
}
$tilInfos = Get-TilInfo
$categoryIndex = [Text.StringBuilder]::new()
$tilIndex = [Text.StringBuilder]::new()
$currentCategory = $null
$currentSubCategory = $null
$tilInfos | Sort-Object category, title | ForEach-Object {
if($_.category -ne $currentCategory)
{
# Add category index
$categoryAnchor = ($_.category -replace "#","" -replace " ", "-").ToLower()
$categoryIndex.AppendLine("* [$($_.category)](#$($categoryAnchor))") | out-null
# Write category header
$tilIndex.AppendLine("`n---`n") | out-null
$tilIndex.AppendLine("### $($_.category -replace "#","\#")`n") | out-null
$currentCategory = $_.category
$currentSubCategory = $null
}
if($_.subCategory -and ($_.subCategory -ne $currentSubCategory))
{
# Add sub category index
$subCategoryAnchor = ($_.subCategory -replace "#","" -replace " ", "-").ToLower()
$categoryIndex.AppendLine(" * [$($_.subCategory)](#$($subCategoryAnchor))") | out-null
# Add sub-category header
$tilIndex.AppendLine("`n#### $($_.subCategory)`n") | out-null
$currentSubCategory = $_.subCategory
}
$tilIndex.AppendLine("* [$($_.title)]($($_.path))") | out-null
}
$updatedContent =
"<!-- index starts -->`n" +
"## Categories`n`n" +
"$categoryIndex" +
"$tilIndex`n" +
"<!-- index ends -->"
$indexRegex = "(?s)<!\-\- index starts \-\->.*<!\-\- index ends \-\->"
$content = Get-Content (Join-Path $PSScriptRoot "README.md") -Raw
$content = $content -replace $indexRegex,$updatedContent
$content | Set-Content README.md