Skip to content

Commit 66b21b9

Browse files
authored
Handles dashes in documentation in repository Wiki (#159)
1 parent 2cfa8d8 commit 66b21b9

15 files changed

+726
-16
lines changed

CHANGELOG.md

+31
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,40 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
55

66
## [Unreleased]
77

8+
### Removed
9+
10+
- Removed `build.psd1` as it is no longer required to build the project.
11+
812
### Added
913

1014
- Added a devcontainer for development.
15+
- Added private function `ConvertTo-WikiSidebarLinkName` that converts a
16+
name to a format suitable for use as a Wiki sidebar link.
17+
- New tasks:
18+
- `Prepare_Markdown_FileNames_For_GitHub_Publish` - This task will prepare
19+
the markdown file names for publishing to the GitHub Wiki by replacing
20+
hyphens with spaces and converting Unicode hyphens to standard hyphens.
21+
It can be controlled by parameter `ReplaceHyphen` in the task, which
22+
defaults to `$true`.
23+
- `Clean_WikiContent_For_GitHub_Publish` - This task will remove the top
24+
level header from any markdown file where the top level header equals the
25+
filename (converting Unicode hyphen to ASCII hyphen before comparison).
26+
It can be controlled by parameter `RemoveTopLevelHeader` in the task, which
27+
defaults to `$true`.
28+
29+
### Changed
30+
31+
- `New-GitHubWikiSidebar`
32+
- Replaces ASCII hyphens for the Wiki sidebar.
33+
- Replaces Unicode hyphens with standard hyphens for the Wiki sidebar.
34+
- Task `Generate_Wiki_Content`
35+
- Now calls `Prepare_Markdown_FileNames_For_GitHub_Publish` after the
36+
markdown files and external help file for command help has been generated.
37+
- Now calls `Clean_WikiContent_For_GitHub_Publish` as the last step to
38+
remove the top level header from any markdown file where the top level
39+
header equals the filename.
40+
- Task `Generate_Markdown_For_Public_Commands`
41+
- Verbose output of the markdown files that was created.
1142

1243
### Fixed
1344

README.md

+56
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,33 @@ BuildWorkflow:
136136
- Publish_GitHub_Wiki_Content
137137
```
138138

139+
### `Clean_WikiContent_For_GitHub_Publish`
140+
141+
>[!IMPORTANT]
142+
>This task does not work in Windows PowerShell due to it not supporting
143+
>unicode in filenames.
144+
145+
This build task will clean the markdown files for publishing to the GitHub Wiki.
146+
It can be controlled by parameter `RemoveTopLevelHeader` in the task, which
147+
defaults to `$true`.
148+
149+
Below is an example how the build task can be used when a repository is
150+
based on the [Sampler](https://github.com/gaelcolas/Sampler) project.
151+
152+
```yaml
153+
BuildWorkflow:
154+
'.':
155+
- build
156+
157+
build:
158+
- Clean
159+
- Build_Module_ModuleBuilder
160+
- Build_NestedModules_ModuleBuilder
161+
- Create_changelog_release_output
162+
- Generate_Markdown_For_Public_Commands
163+
- Clean_WikiContent_For_GitHub_Publish
164+
```
165+
139166
### `Copy_Source_Wiki_Folder`
140167

141168
This build task will copy the content of the wiki source folder if it exist
@@ -345,9 +372,11 @@ This is a metatask that runs the task (in order):
345372
- `Create_Wiki_Output_Folder`
346373
- `Generate_Markdown_For_Public_Commands`
347374
- `Generate_External_Help_File_For_Public_Commands`
375+
- `Prepare_Markdown_FileNames_For_GitHub_Publish`
348376
- `Clean_Markdown_Of_Public_Commands`
349377
- `Generate_Markdown_For_DSC_Resources`
350378
- `Copy_Source_Wiki_Folder`
379+
- `Clean_WikiContent_For_GitHub_Publish`
351380

352381
Below is an example how the build task can be used when a repository is
353382
based on the [Sampler](https://github.com/gaelcolas/Sampler) project.
@@ -430,6 +459,33 @@ BuildWorkflow:
430459
- Publish_GitHub_Wiki_Content
431460
```
432461

462+
### `Prepare_Markdown_FileNames_For_GitHub_Publish`
463+
464+
>[!IMPORTANT]
465+
>This task does not work in Windows PowerShell due to it not supporting
466+
>unicode in filenames.
467+
468+
This build task will replace the hyphen in the markdown filenames with the
469+
unicode non-breaking hyphen. It can be controlled by parameter
470+
`ReplaceHyphen` in the task, which defaults to `$true`.
471+
472+
Below is an example how the build task can be used when a repository is
473+
based on the [Sampler](https://github.com/gaelcolas/Sampler) project.
474+
475+
```yaml
476+
BuildWorkflow:
477+
'.':
478+
- build
479+
480+
build:
481+
- Clean
482+
- Build_Module_ModuleBuilder
483+
- Build_NestedModules_ModuleBuilder
484+
- Create_changelog_release_output
485+
- Generate_Markdown_For_Public_Commands
486+
- Prepare_Markdown_FileNames_For_GitHub_Publish
487+
```
488+
433489
### `Publish_GitHub_Wiki_Content`
434490

435491
This build task runs the command `Publish-WikiContent`. The task will only
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<#
2+
.SYNOPSIS
3+
Converts a name to a format suitable for use as a Wiki sidebar link.
4+
5+
.DESCRIPTION
6+
The ConvertTo-WikiSidebarLinkName function takes a string input and converts
7+
it to a format that is suitable for use as a link name in a Wiki sidebar. It
8+
replaces hyphens with spaces and converts Unicode hyphens to standard hyphens.
9+
10+
.PARAMETER Name
11+
The string to be converted. This parameter is mandatory and can be passed via
12+
the pipeline.
13+
14+
.EXAMPLE
15+
PS C:\> ConvertTo-WikiSidebarLinkName -Name "My-Page-Name"
16+
17+
Returns: "My Page Name"
18+
19+
.EXAMPLE
20+
PS C:\> ('Unicode{0}Hyphen' -f [System.Char]::ConvertFromUtf32(0x2011)) | ConvertTo-WikiSidebarLinkName
21+
22+
Returns: "Unicode-Hyphen"
23+
24+
.INPUTS
25+
System.String
26+
27+
.OUTPUTS
28+
System.String
29+
30+
.NOTES
31+
This function is used internally by the New-GitHubWikiSidebar function to
32+
format link names in the generated sidebar.
33+
#>
34+
function ConvertTo-WikiSidebarLinkName
35+
{
36+
[CmdletBinding()]
37+
param
38+
(
39+
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
40+
[System.String]
41+
$Name
42+
)
43+
44+
process
45+
{
46+
if ($PSVersionTable.PSVersion -ge '6.0')
47+
{
48+
# Replace hyphens with spaces
49+
$convertedName = $Name -replace '-', ' '
50+
51+
# Replace Unicode hyphen (U+2010) with a standard hyphen
52+
$convertedName = $convertedName -replace [System.Char]::ConvertFromUtf32(0x2011), '-'
53+
}
54+
else
55+
{
56+
$convertedName = $Name
57+
}
58+
59+
return $convertedName
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<#
2+
.SYNOPSIS
3+
This is the alias to the build task Clean_WikiContent_For_GitHub_Publish
4+
script file.
5+
6+
.DESCRIPTION
7+
This makes available the alias 'Task.Clean_WikiContent_For_GitHub_Publish'
8+
that is exported in the module manifest so that the build task can be
9+
correctly imported using for example Invoke-Build.
10+
11+
.NOTES
12+
This is using the pattern lined out in the Invoke-Build repository
13+
https://github.com/nightroman/Invoke-Build/tree/master/Tasks/Import.
14+
#>
15+
16+
Set-Alias -Name 'Task.Clean_WikiContent_For_GitHub_Publish' -Value "$PSScriptRoot/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<#
2+
.SYNOPSIS
3+
This is the alias to the build task Prepare_Markdown_Filenames_For_GitHub_Publish
4+
script file.
5+
6+
.DESCRIPTION
7+
This makes available the alias 'Task.Prepare_Markdown_Filenames_For_GitHub_Publish'
8+
that is exported in the module manifest so that the build task can be correctly
9+
imported using for example Invoke-Build.
10+
11+
.NOTES
12+
This is using the pattern lined out in the Invoke-Build repository
13+
https://github.com/nightroman/Invoke-Build/tree/master/Tasks/Import.
14+
#>
15+
16+
Set-Alias -Name 'Task.Prepare_Markdown_Filenames_For_GitHub_Publish' -Value "$PSScriptRoot/tasks/Prepare_Markdown_Filenames_For_GitHub_Publish.build.ps1"

source/Public/New-GitHubWikiSidebar.ps1

+6-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ Get-MarkdownMetadata -Path $($file.FullName) -ErrorAction 'Stop'
169169

170170
foreach ($link in $sortedListItem)
171171
{
172-
$null = $output.AppendLine('- [' + $link + '](' + $link + ')')
172+
$linkName = ConvertTo-WikiSidebarLinkName -Name $link
173+
174+
$null = $output.AppendLine('- [' + $linkName + '](' + $link + ')')
173175
}
174176

175177
$null = $output.AppendLine()
@@ -188,7 +190,9 @@ Get-MarkdownMetadata -Path $($file.FullName) -ErrorAction 'Stop'
188190

189191
foreach ($link in $sidebarCategories.$category | Sort-Object)
190192
{
191-
$null = $output.AppendLine('- [' + $link + '](' + $link + ')')
193+
$linkName = ConvertTo-WikiSidebarLinkName -Name $link
194+
195+
$null = $output.AppendLine('- [' + $linkName + '](' + $link + ')')
192196
}
193197

194198
$null = $output.AppendLine()

source/build.psd1

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<#
2+
.SYNOPSIS
3+
This is a build task that modifies the wiki content to enhance the content
4+
for use in GitHub repository Wikis.
5+
6+
.PARAMETER ProjectPath
7+
The root path to the project. Defaults to $BuildRoot.
8+
9+
.PARAMETER OutputDirectory
10+
The base directory of all output. Defaults to folder 'output' relative to
11+
the $BuildRoot.
12+
13+
.PARAMETER BuiltModuleSubdirectory
14+
Sub folder where you want to build the Module to (instead of $OutputDirectory/$ModuleName).
15+
This is especially useful when you want to build DSC Resources, but you don't want the
16+
`Get-DscResource` command to find several instances of the same DSC Resources because
17+
of the overlapping $Env:PSmodulePath (`$buildRoot/output` for the built module and `$buildRoot/output/RequiredModules`).
18+
19+
In most cases I would recommend against setting $BuiltModuleSubdirectory.
20+
21+
.PARAMETER VersionedOutputDirectory
22+
Whether the Module is built with its versioned Subdirectory, as you would see it on a System.
23+
For instance, if VersionedOutputDirectory is $true, the built module's ModuleBase would be: `output/MyModuleName/2.0.1/`
24+
25+
.PARAMETER ProjectName
26+
The project name. Defaults to the empty string.
27+
28+
.PARAMETER SourcePath
29+
The path to the source folder name. Defaults to the empty string.
30+
The task does not use this parameter, see the notes below.
31+
32+
.PARAMETER DocOutputFolder
33+
The path to the where the markdown documentation is written. Defaults to the
34+
folder `./output/WikiContent`.
35+
36+
.PARAMETER BuildInfo
37+
The build info object from ModuleBuilder. Defaults to an empty hashtable.
38+
39+
.NOTES
40+
This is a build task that is primarily meant to be run by Invoke-Build but
41+
wrapped by the Sampler project's build.ps1 (https://github.com/gaelcolas/Sampler).
42+
43+
Parameter SourcePath is intentionally added to the task even if it is not used,
44+
otherwise the tests fails. Most likely because the script Set-SamplerTaskVariable
45+
expects the variable to always be available.
46+
#>
47+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('DscResource.AnalyzerRules/Measure-ParameterBlockParameterAttribute', '', Justification = 'For boolean values when using (property $true $false) fails in conversion between string and boolean when environment variable is used if set as advanced parameter ([Parameter()])')]
48+
param
49+
(
50+
[Parameter()]
51+
[System.String]
52+
$ProjectPath = (property ProjectPath $BuildRoot),
53+
54+
[Parameter()]
55+
[System.String]
56+
$OutputDirectory = (property OutputDirectory (Join-Path $BuildRoot 'output')),
57+
58+
[Parameter()]
59+
[System.String]
60+
$BuiltModuleSubdirectory = (property BuiltModuleSubdirectory ''),
61+
62+
[Parameter()]
63+
[System.Management.Automation.SwitchParameter]
64+
$VersionedOutputDirectory = (property VersionedOutputDirectory $true),
65+
66+
[Parameter()]
67+
[System.String]
68+
$ProjectName = (property ProjectName ''),
69+
70+
[Parameter()]
71+
[System.String]
72+
$SourcePath = (property SourcePath ''),
73+
74+
[Parameter()]
75+
[System.String]
76+
$DocOutputFolder = (property DocOutputFolder 'WikiContent'),
77+
78+
[Parameter()]
79+
[System.Management.Automation.SwitchParameter]
80+
$RemoveTopLevelHeader = (property RemoveTopLevelHeader $true),
81+
82+
[Parameter()]
83+
[System.Collections.Hashtable]
84+
$BuildInfo = (property BuildInfo @{ })
85+
)
86+
87+
# Synopsis: Modifies the wiki content to enhance the content for use in GitHub repository Wikis.
88+
Task Clean_WikiContent_For_GitHub_Publish {
89+
if ($PSVersionTable.PSVersion -lt '6.0')
90+
{
91+
Write-Warning -Message 'This task is not supported in Windows PowerShell.'
92+
93+
return
94+
}
95+
96+
# Get the values for task variables, see https://github.com/gaelcolas/Sampler#task-variables.
97+
. Set-SamplerTaskVariable
98+
99+
$DocOutputFolder = Get-SamplerAbsolutePath -Path $DocOutputFolder -RelativeTo $OutputDirectory
100+
101+
"`tDocs output folder path = '$DocOutputFolder'"
102+
''
103+
104+
if ($RemoveTopLevelHeader)
105+
{
106+
$markdownFiles = Get-ChildItem -Path $DocOutputFolder -Filter '*.md'
107+
108+
Write-Build -Color 'Magenta' -Text 'Removing top level header from markdown files if it is the same as the filename.'
109+
110+
$markdownFiles |
111+
ForEach-Object -Process {
112+
$content = Get-Content -Path $_.FullName -Raw
113+
114+
$hasTopHeader = $content -match '(?m)^#\s+([^\r\n]+)'
115+
116+
$baseNameWithoutNonBreakingHyphen = $_.BaseName -replace [System.Char]::ConvertFromUtf32(0x2011), '-'
117+
118+
if ($hasTopHeader -and $Matches[1] -eq $baseNameWithoutNonBreakingHyphen)
119+
{
120+
Write-Build -Color DarkGray -Text ('Top level header is the same as the filename. Removing top level header from: {0}' -f $_.Name)
121+
122+
<#
123+
Remove only the top level header (# Header) and any empty lines
124+
following it. The regex should only target the first header found,
125+
without affecting later top level headers.
126+
#>
127+
$content = $content -replace '(?m)^#\s+(.*)\s*', ''
128+
129+
# Save the updated content back to the file
130+
Set-Content -Path $_.FullName -Value $content
131+
}
132+
elseif ($hasTopHeader)
133+
{
134+
Write-Build -Color DarkGray -Text ('Top level header is different from the filename. Skipping: {0}' -f $_.Name)
135+
}
136+
else
137+
{
138+
Write-Build -Color DarkGray -Text ('No top level header found in: {0}' -f $_.Name)
139+
}
140+
}
141+
}
142+
}

0 commit comments

Comments
 (0)