@@ -14,13 +14,13 @@ $Command,
1414[switch ]
1515$InvokeExample ,
1616
17- # The width of the sample code (in landscape mode), in characters
18- [int ]
19- $SampleCodeWidthLandscape = 80 ,
17+ # If set, will not show the command name
18+ [switch ]
19+ $HideName ,
2020
21- # The width of the sample code (in portrait mode), in characters
22- [int ]
23- $SampleCodeWidthPortrait = 50
21+ # The identifier for the palette `<select>`.
22+ [string ]
23+ $SelectPaletteId = ' SelectPalette '
2424)
2525
2626# Try to get command help
@@ -39,10 +39,53 @@ $synopsis = $CommandHelp.Synopsis
3939$description = $CommandHelp.description.text -join [Environment ]::NewLine
4040$notes = $CommandHelp.alertSet.alert.text -join [Environment ]::NewLine
4141
42+ filter looksLikeMarkdown {
43+ if (
44+ # If it's got a markdown-like link
45+ $_ -match ' \[[^\]]+\]\(' -or
46+ # Or any of the lines start with markdown special characters
47+ $_ -split ' (?>\r\n|\n)' -match ' \s{0,3}[\#*~`]'
48+ ) {
49+ # it's probably markdown
50+ $_
51+ }
52+ }
53+
54+ filter looksLikeTags {
55+ if ($_ -match ' ^\s{0,}<' ) {
56+ $_
57+ }
58+ }
59+
4260# Display the command name, synopsis, and description
43- " <h1>$ ( [Web.HttpUtility ]::HtmlEncode($CommandHelp.Name )) </h1>"
44- " <h2>$ ( [Web.HttpUtility ]::HtmlEncode($synopsis )) </h2>"
45- " <h3>$ ( [Web.HttpUtility ]::HtmlEncode($description )) </h3>"
61+ if (-not $HideName ) {
62+ " <h1>$ ( [Web.HttpUtility ]::HtmlEncode(
63+ $CommandHelp.Name -replace ' (?:.+?/){0,}' -replace ' \.ps1$' -replace ' \..+?$'
64+ )) </h1>"
65+ }
66+
67+ if ($synopsis ) {
68+ if ($synopsis | looksLikeMarkdown) {
69+ (ConvertFrom-Markdown - InputObject $synopsis ).Html
70+ }
71+ elseif ($synopsis | looksLikeTags) {
72+ $synopsis
73+ } else {
74+ " <h2>$ ( [Web.HttpUtility ]::HtmlEncode($synopsis )) </h2>"
75+ }
76+ }
77+
78+ if ($description ) {
79+ if ($description | lookslikeMarkdown) {
80+ (ConvertFrom-Markdown - InputObject $description ).Html
81+ }
82+ elseif ($description | looksLikeTags) {
83+ $description
84+ }
85+ else {
86+ " <h3>$ ( [Web.HttpUtility ]::HtmlEncode($description )) </h3>"
87+ }
88+ }
4689
4790if ($notes ) {
4891 # If there were notes, convert them from markdown
@@ -53,47 +96,54 @@ if ($notes) {
5396" <style>"
5497" .example-grid {
5598 width: 90vw;
56- margin-left: auto ;
57- margin-right: auto ;
99+ margin-left: 5vw ;
100+ margin-right: 5vw ;
58101 text-align: center;
59102}"
60103" .example {
61104 text-align: center;
62105}"
63- " .sampleCode {
64- text-align: center;
65- width: fit-content;
106+ " .example-code {
107+ text-align: center;
66108 font-size: .9rem;
67109 margin-left: auto;
68110 margin-right: auto;
69111}"
70112
71- <# "@media (orientation: portrait) {
72- sampleCode { width: ${SampleCodeWidthPortrait}ch }
73- }"#>
74-
75113" code { text-align: left}"
76114
115+ " .example-menu { text-align: right; margin-top: -.5rem; margin-bottom: -.5rem }"
116+ " .copy-button { float: right; }"
117+
77118" .example-outputs {
78119 display: flex;
79120 flex-direction: column;
80121 align-items: center;
81122 justify-content: center;
82- gap: 1.5rem ;
123+ gap: 1rem ;
83124 margin: 0.8rem;
84- padding: 0.4rem ;
125+ padding: 0.2rem ;
85126 width: 90vw;
86127}"
87128" .example-output { text-align: center }"
129+ "
130+ pre {
131+ position: relative;
132+ }
133+ "
88134" </style>"
89135# endregion Grid Styles
90136
137+ $exampleCount = @ ($CommandHelp.examples.example ).Length
138+ $progress = [Ordered ]@ {id = Get-Random }
139+ $progress.Activity = " $ ( $command ) examples"
91140# Create a grid for examples
92141" <div class='example-grid'>"
93142$exampleNumber = 0
94143# Walk over each example
95- foreach ($example in $CommandHelp.examples.example ) {
144+ foreach ($example in $CommandHelp.examples.example ) {
96145 $exampleNumber ++
146+
97147 # Combine the code and remarks
98148 $exampleLines =
99149 @ (
@@ -107,6 +157,11 @@ foreach ($example in $CommandHelp.examples.example) {
107157 # Anything until the first non-comment line is a markdown predicate to the example
108158 $nonCommentLine = $false
109159 $markdownLines = @ ()
160+
161+ $progress.PercentComplete = $exampleNumber * 100 / $exampleCount
162+ $progress.Activity = " $ ( $command ) examples $exampleNumber "
163+ $progress.Status = " $ ( $exampleLines [0 ]) "
164+ Write-Progress @progress
110165
111166 # Go thru each line in the example as part of a loop
112167 $codeBlock = @ (foreach ($exampleLine in $exampleLines ) {
@@ -129,7 +184,7 @@ foreach ($example in $CommandHelp.examples.example) {
129184 (ConvertFrom-Markdown - InputObject $Markdown ).Html
130185 }
131186 # followed by our sample code
132- " <div class='sampleCode '>"
187+ " <div class='example-code '>"
133188 " <pre>"
134189 " <code class='language-powershell'>" +
135190 [Web.HttpUtility ]::HtmlEncode($codeBlock ) +
@@ -142,8 +197,7 @@ foreach ($example in $CommandHelp.examples.example) {
142197 " </div>"
143198 " <hr/>"
144199 continue
145- }
146-
200+ }
147201 # Otherwise, try to make our example a script block
148202 $exampleCode =
149203 try {
@@ -153,8 +207,14 @@ foreach ($example in $CommandHelp.examples.example) {
153207 continue
154208 }
155209
210+ if (-not $global :ExampleOutputCache ) {
211+ $global :ExampleOutputCache = [Ordered ]@ {}
212+ }
213+ if (-not $global :ExampleOutputCache [$codeBlock ]) {
214+ $global :ExampleOutputCache [$codeBlock ] = @ (. $exampleCode )
215+ }
156216 # then run it and capture the output
157- $exampleOutputs = @ ( . $exampleCode )
217+ $exampleOutputs = $ script :ExampleOutputCache [ $codeBlock ]
158218
159219 # Keep track of our example output count
160220 $exampleOutputNumber = 0
@@ -195,4 +255,20 @@ foreach ($example in $CommandHelp.examples.example) {
195255 " <hr/>"
196256 " </div>"
197257}
198- " </div>"
258+ " </div>"
259+
260+ $progress.Remove (' PercentComplete' )
261+ $progress [' Completed' ] = $true
262+ Write-Progress @progress
263+
264+ @"
265+ <script>
266+ document.querySelectorAll('pre > code').forEach(element => {
267+ const copyCodeButton = document.createElement('div')
268+ copyCodeButton.classList.add('copy-button')
269+ copyCodeButton.onclick = () => navigator.clipboard.writeText(element.innerText)
270+ copyCodeButton.innerHTML = `` $ ( . $site.includes.Feather - Icon ' clipboard' ) ``
271+ element.parentNode.parentNode.prepend(copyCodeButton)
272+ });
273+ </script>
274+ "@
0 commit comments