Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handles dashes in documentation in repository Wiki #159

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,40 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Removed

- Removed `build.psd1` as it is no longer required to build the project.

### Added

- Added a devcontainer for development.
- Added private function `ConvertTo-WikiSidebarLinkName` that converts a
name to a format suitable for use as a Wiki sidebar link.
- New tasks:
- `Prepare_Markdown_FileNames_For_GitHub_Publish` - This task will prepare
the markdown file names for publishing to the GitHub Wiki by replacing
hyphens with spaces and converting Unicode hyphens to standard hyphens.
It can be controlled by parameter `ReplaceHyphen` in the task, which
defaults to `$true`.
- `Clean_WikiContent_For_GitHub_Publish` - This task will remove the top
level header from any markdown file where the top level header equals the
filename (converting Unicode hyphen to ASCII hyphen before comparison).
It can be controlled by parameter `RemoveTopLevelHeader` in the task, which
defaults to `$true`.

### Changed

- `New-GitHubWikiSidebar`
- Replaces ASCII hyphens for the Wiki sidebar.
- Replaces Unicode hyphens with standard hyphens for the Wiki sidebar.
- Task `Generate_Wiki_Content`
- Now calls `Prepare_Markdown_FileNames_For_GitHub_Publish` after the
markdown files and external help file for command help has been generated.
- Now calls `Clean_WikiContent_For_GitHub_Publish` as the last step to
remove the top level header from any markdown file where the top level
header equals the filename.
- Task `Generate_Markdown_For_Public_Commands`
- Verbose output of the markdown files that was created.

### Fixed

Expand Down
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,29 @@ BuildWorkflow:
- Publish_GitHub_Wiki_Content
```

### `Clean_WikiContent_For_GitHub_Publish`

This build task will clean the markdown files for publishing to the GitHub Wiki.
It can be controlled by parameter `RemoveTopLevelHeader` in the task, which
defaults to `$true`.

Below is an example how the build task can be used when a repository is
based on the [Sampler](https://github.com/gaelcolas/Sampler) project.

```yaml
BuildWorkflow:
'.':
- build

build:
- Clean
- Build_Module_ModuleBuilder
- Build_NestedModules_ModuleBuilder
- Create_changelog_release_output
- Generate_Markdown_For_Public_Commands
- Clean_WikiContent_For_GitHub_Publish
```

### `Copy_Source_Wiki_Folder`

This build task will copy the content of the wiki source folder if it exist
Expand Down Expand Up @@ -345,9 +368,11 @@ This is a metatask that runs the task (in order):
- `Create_Wiki_Output_Folder`
- `Generate_Markdown_For_Public_Commands`
- `Generate_External_Help_File_For_Public_Commands`
- `Prepare_Markdown_FileNames_For_GitHub_Publish`
- `Clean_Markdown_Of_Public_Commands`
- `Generate_Markdown_For_DSC_Resources`
- `Copy_Source_Wiki_Folder`
- `Clean_WikiContent_For_GitHub_Publish`

Below is an example how the build task can be used when a repository is
based on the [Sampler](https://github.com/gaelcolas/Sampler) project.
Expand Down Expand Up @@ -430,6 +455,29 @@ BuildWorkflow:
- Publish_GitHub_Wiki_Content
```

### `Prepare_Markdown_FileNames_For_GitHub_Publish`

This build task will replace the hyphen in the markdown filenames with the
unicode non-breaking hyphen. It can be controlled by parameter
`ReplaceHyphen` in the task, which defaults to `$true`.

Below is an example how the build task can be used when a repository is
based on the [Sampler](https://github.com/gaelcolas/Sampler) project.

```yaml
BuildWorkflow:
'.':
- build

build:
- Clean
- Build_Module_ModuleBuilder
- Build_NestedModules_ModuleBuilder
- Create_changelog_release_output
- Generate_Markdown_For_Public_Commands
- Prepare_Markdown_FileNames_For_GitHub_Publish
```

### `Publish_GitHub_Wiki_Content`

This build task runs the command `Publish-WikiContent`. The task will only
Expand Down
54 changes: 54 additions & 0 deletions source/Private/ConvertTo-WikiSidebarLinkName.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<#
.SYNOPSIS
Converts a name to a format suitable for use as a Wiki sidebar link.

.DESCRIPTION
The ConvertTo-WikiSidebarLinkName function takes a string input and converts
it to a format that is suitable for use as a link name in a Wiki sidebar. It
replaces hyphens with spaces and converts Unicode hyphens to standard hyphens.

.PARAMETER Name
The string to be converted. This parameter is mandatory and can be passed via
the pipeline.

.EXAMPLE
PS C:\> ConvertTo-WikiSidebarLinkName -Name "My-Page-Name"

Returns: "My Page Name"

.EXAMPLE
PS C:\> "Unicode‐Hyphen" | ConvertTo-WikiSidebarLinkName

Returns: "Unicode-Hyphen"

.INPUTS
System.String

.OUTPUTS
System.String

.NOTES
This function is used internally by the New-GitHubWikiSidebar function to
format link names in the generated sidebar.
#>
function ConvertTo-WikiSidebarLinkName
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
[System.String]
$Name
)

process
{
# Replace hyphens with spaces
$convertedName = $Name -replace '-', ' '

# Replace Unicode hyphen (U+2010) with a standard hyphen
$convertedName = $convertedName -replace [System.Char]::ConvertFromUtf32(0x2010), '-'

return $convertedName
}
}
8 changes: 6 additions & 2 deletions source/Public/New-GitHubWikiSidebar.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ Get-MarkdownMetadata -Path $($file.FullName) -ErrorAction 'Stop'

foreach ($link in $sortedListItem)
{
$null = $output.AppendLine('- [' + $link + '](' + $link + ')')
$linkName = ConvertTo-WikiSidebarLinkName -Name $link

$null = $output.AppendLine('- [' + $linkName + '](' + $link + ')')
}

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

foreach ($link in $sidebarCategories.$category | Sort-Object)
{
$null = $output.AppendLine('- [' + $link + '](' + $link + ')')
$linkName = ConvertTo-WikiSidebarLinkName -Name $link

$null = $output.AppendLine('- [' + $linkName + '](' + $link + ')')
}

$null = $output.AppendLine()
Expand Down
6 changes: 0 additions & 6 deletions source/build.psd1

This file was deleted.

135 changes: 135 additions & 0 deletions source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<#
.SYNOPSIS
This is a build task that modifies the wiki content to enhance the content
for use in GitHub repository Wikis.

.PARAMETER ProjectPath
The root path to the project. Defaults to $BuildRoot.

.PARAMETER OutputDirectory
The base directory of all output. Defaults to folder 'output' relative to
the $BuildRoot.

.PARAMETER BuiltModuleSubdirectory
Sub folder where you want to build the Module to (instead of $OutputDirectory/$ModuleName).
This is especially useful when you want to build DSC Resources, but you don't want the
`Get-DscResource` command to find several instances of the same DSC Resources because
of the overlapping $Env:PSmodulePath (`$buildRoot/output` for the built module and `$buildRoot/output/RequiredModules`).

In most cases I would recommend against setting $BuiltModuleSubdirectory.

.PARAMETER VersionedOutputDirectory
Whether the Module is built with its versioned Subdirectory, as you would see it on a System.
For instance, if VersionedOutputDirectory is $true, the built module's ModuleBase would be: `output/MyModuleName/2.0.1/`

.PARAMETER ProjectName
The project name. Defaults to the empty string.

.PARAMETER SourcePath
The path to the source folder name. Defaults to the empty string.
The task does not use this parameter, see the notes below.

.PARAMETER DocOutputFolder
The path to the where the markdown documentation is written. Defaults to the
folder `./output/WikiContent`.

.PARAMETER BuildInfo
The build info object from ModuleBuilder. Defaults to an empty hashtable.

.NOTES
This is a build task that is primarily meant to be run by Invoke-Build but
wrapped by the Sampler project's build.ps1 (https://github.com/gaelcolas/Sampler).

Parameter SourcePath is intentionally added to the task even if it is not used,
otherwise the tests fails. Most likely because the script Set-SamplerTaskVariable
expects the variable to always be available.
#>
[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()])')]
param
(
[Parameter()]
[System.String]
$ProjectPath = (property ProjectPath $BuildRoot),

Check warning on line 52 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L52

Added line #L52 was not covered by tests

[Parameter()]
[System.String]
$OutputDirectory = (property OutputDirectory (Join-Path $BuildRoot 'output')),

Check warning on line 56 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L56

Added line #L56 was not covered by tests

[Parameter()]
[System.String]
$BuiltModuleSubdirectory = (property BuiltModuleSubdirectory ''),

Check warning on line 60 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L60

Added line #L60 was not covered by tests

[Parameter()]
[System.Management.Automation.SwitchParameter]
$VersionedOutputDirectory = (property VersionedOutputDirectory $true),

Check warning on line 64 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L64

Added line #L64 was not covered by tests

[Parameter()]
[System.String]
$ProjectName = (property ProjectName ''),

Check warning on line 68 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L68

Added line #L68 was not covered by tests

[Parameter()]
[System.String]
$SourcePath = (property SourcePath ''),

Check warning on line 72 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L72

Added line #L72 was not covered by tests

[Parameter()]
[System.String]
$DocOutputFolder = (property DocOutputFolder 'WikiContent'),

Check warning on line 76 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L76

Added line #L76 was not covered by tests

[Parameter()]
[System.Management.Automation.SwitchParameter]
$RemoveTopLevelHeader = (property RemoveTopLevelHeader $true),

Check warning on line 80 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L80

Added line #L80 was not covered by tests

[Parameter()]
[System.Collections.Hashtable]
$BuildInfo = (property BuildInfo @{ })

Check warning on line 84 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L84

Added line #L84 was not covered by tests
)

# Synopsis: Modifies the wiki content to enhance the content for use in GitHub repository Wikis.
Task Clean_WikiContent_For_GitHub_Publish {

Check warning on line 88 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L88

Added line #L88 was not covered by tests
# Get the values for task variables, see https://github.com/gaelcolas/Sampler#task-variables.
. Set-SamplerTaskVariable

Check warning on line 90 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L90

Added line #L90 was not covered by tests

$DocOutputFolder = Get-SamplerAbsolutePath -Path $DocOutputFolder -RelativeTo $OutputDirectory

Check warning on line 92 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L92

Added line #L92 was not covered by tests

"`tDocs output folder path = '$DocOutputFolder'"
''

Check warning on line 95 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L94-L95

Added lines #L94 - L95 were not covered by tests

if ($RemoveTopLevelHeader)

Check warning on line 97 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L97

Added line #L97 was not covered by tests
{
$markdownFiles = Get-ChildItem -Path $DocOutputFolder -Filter '*.md'

Check warning on line 99 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L99

Added line #L99 was not covered by tests

Write-Build -Color 'Magenta' -Text 'Removing top level header from markdown files if it is the same as the filename.'

Check warning on line 101 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L101

Added line #L101 was not covered by tests

$markdownFiles |
ForEach-Object -Process {
$content = Get-Content -Path $_.FullName -Raw

Check warning on line 105 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L103-L105

Added lines #L103 - L105 were not covered by tests

$hasTopHeader = $content -match '(?m)^#\s+([^\r\n]+)'

Check warning on line 107 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L107

Added line #L107 was not covered by tests

$baseNameWithoutNonBreakingHyphen = $_.BaseName -replace [System.Char]::ConvertFromUtf32(0x2010), '-'

Check warning on line 109 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L109

Added line #L109 was not covered by tests

if ($hasTopHeader -and $Matches[1] -eq $baseNameWithoutNonBreakingHyphen)

Check warning on line 111 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L111

Added line #L111 was not covered by tests
{
Write-Build -Color DarkGray -Text ('Top level header is the same as the filename. Removing top level header from: {0}' -f $_.Name)

Check warning on line 113 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L113

Added line #L113 was not covered by tests

<#
Remove only the top level header (# Header) and any empty lines
following it. The regex should only target the first header found,
without affecting later top level headers.
#>
$content = $content -replace '(?m)^#\s+(.*)\s*', ''

Check warning on line 120 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L120

Added line #L120 was not covered by tests

# Save the updated content back to the file
Set-Content -Path $_.FullName -Value $content

Check warning on line 123 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L123

Added line #L123 was not covered by tests
}
elseif ($hasTopHeader)

Check warning on line 125 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L125

Added line #L125 was not covered by tests
{
Write-Build -Color DarkGray -Text ('Top level header is different from the filename. Skipping: {0}' -f $_.Name)

Check warning on line 127 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L127

Added line #L127 was not covered by tests
}
else
{
Write-Build -Color DarkGray -Text ('No top level header found in: {0}' -f $_.Name)

Check warning on line 131 in source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Clean_WikiContent_For_GitHub_Publish.build.ps1#L131

Added line #L131 was not covered by tests
}
}
}
}
12 changes: 9 additions & 3 deletions source/tasks/Generate_Markdown_For_Public_Commands.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Import-Module -name '$DependentModule'

$generateMarkdownScript += @"
`n# Import the module that help is generate for
`$importModule = Import-Module -Name '$ProjectName' -Passthru -ErrorAction 'Stop'
`$importModule = Import-Module -Name '$ProjectName' -PassThru -ErrorAction 'Stop'

if (-not `$importModule)
{
Expand Down Expand Up @@ -212,14 +212,20 @@ New-MarkdownHelp @newMarkdownHelpParams
The scriptblock is run in a separate process to avoid conflicts with
other modules that are loaded in the current process.
#>
& $pwshPath -Command $generateMarkdownScriptBlock -ExecutionPolicy 'ByPass' -NoProfile
$markdownFiles = & $pwshPath -Command $generateMarkdownScriptBlock -ExecutionPolicy 'ByPass' -NoProfile

Write-Build -Color DarkGray -Text "Generated markdown files:"

$markdownFiles | ForEach-Object -Process {
Write-Build -Color DarkGray -Text ("`t{0}" -f $_.FullName)
}

if (-not $?)
{
throw "Failed to generate markdown documentation for the public commands for module '$ProjectName'."
}
else
{
Write-Build -Color 'Green' -Text 'Markdown for command documentation created for module '$ProjectName'.'
Write-Build -Color Green -Text 'Markdown for command documentation created for module '$ProjectName'.'
}
}
2 changes: 1 addition & 1 deletion source/tasks/Generate_Wiki_Content.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@
$BuildInfo = (property BuildInfo @{ })
)

Task Generate_Wiki_Content Create_Wiki_Output_Folder, Generate_Markdown_For_Public_Commands, Generate_External_Help_File_For_Public_Commands, Clean_Markdown_Of_Public_Commands, Generate_Markdown_For_DSC_Resources, Copy_Source_Wiki_Folder
Task Generate_Wiki_Content Create_Wiki_Output_Folder, Generate_Markdown_For_Public_Commands, Generate_External_Help_File_For_Public_Commands, Prepare_Markdown_FileNames_For_GitHub_Publish, Clean_Markdown_Of_Public_Commands, Generate_Markdown_For_DSC_Resources, Copy_Source_Wiki_Folder, Clean_WikiContent_For_GitHub_Publish

Check warning on line 87 in source/tasks/Generate_Wiki_Content.build.ps1

View check run for this annotation

Codecov / codecov/patch

source/tasks/Generate_Wiki_Content.build.ps1#L87

Added line #L87 was not covered by tests
Loading
Loading