SiteNavigation.psm1

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
# Default template specifications for page navigation
$SCRIPT:defaultNavTemplate = @{
     navitem      = '<button class="navitem"><a href="{{navurl}}">{{navtext}}</a></button>';
     navlabel     = '<div class="navlabel">{{navtext}}</div>';
     navseparator = '<hr/>';
     navheading   = '<span class="navitem{{level}}">{{navtext}}</span>'
}

# find headings h1 .. h6 in an HTML fragment.
$SCRIPT:hRE   = New-Object regex '<h(\d)[^<>]* id="([^"])+"[^<]*>(.+?)\s*(?=<\/h\d.*|$)'

# Match an hyperlink
$SCRIPT:aRE = New-Object regex '</{0,1} *a[^>]*>'

Set-Alias -Name 'ConvertTo-PageHeadingNavigation' -Value 'New-PageHeadingNavigation'

<#
.SYNOPSIS
Generate navigation specifications for specified headings found in an HTML
fragment.
 
.DESCRIPTION
Generates a link specification for each heading in an HTML fragment which
* has an `id` attribute
* matches heading level constraint
 
The link specifications have the format required by `ConvertTo-NavigationItem`
to generate navigation bar items.
 
.PARAMETER HTMLfragment
HTML fragment object whose HTMLfragment property contains a HTMLfragment string.
Such objects are generated by `Convert-MarkdownToHTMLFragment`.
 
For backward compatibility this parameter can also be a plain string representing
the HTML fragment. This is deprecated.
 
.PARAMETER NavitemSpecs
 
Navigation specification for links which are to appear on every page right
before the page heading navigation links.
 
Usually this specification is something like:
 
`@{ '---' = ''}`
: to add a separator line between the site navigation links from the
    page heading navigation links.
 
`@{ 'Page Contents' = ''}`
: to add the label _Page Contents_ separating the site navigation links from
    the page heading navigation links.
 
However, all link specifications supported by the `NavitemSpecs`
of the function `New-SiteNavigation` are supported for this parameter too.
 
**Note!** If this parameter is specified, the HTMLfragment parameter must be
a HTML fragment object (such as the one returned by
`Convert-MarkdownToHTMLFragment`).
 
.PARAMETER NavTemplate
An optional dictionary of the named HTML template needed to add a heading
link to the navigation bar. The dictionary should at least contain a
`navheading` specification:
 
+ :--------: + :----------: + ----------------------------------------
| Type | Key | HTML Template
+ ========== + ============ + ========================================
| Heading | "navheading" | ~~~ html
| link | |<span class='navitem{{level}}'>{{navtext}}</span>"
| template | | ~~~
+ ---------- + ------------ + ----------------------------------------
 
The HTML template above is the value defined in
`Build.json`. That value is used if the given dictionary does not define the
"navheading" mapping or if no dictionary is given at all. Additional mappings
contained in the dictionary not used by this function.
 
For more customization options see
[Static Site Project Customization](about_MarkdownToHTML.md#static-site-project-customization).
 
The css styles used in the template is defined in `md-styles.css`.
 
Following placeholders in the HTML template are recognized.
 
| Placeholder | Description
| :-----------: | -----------
| `{{level}}` | heading level.
| `{{navtext}}` | heading text
 
During the build process the placeholders are replaced with content taken from
the `NavSpec` parameter.
 
.PARAMETER HeadingLevels
A string of numbers denoting the levels of the headings to add to the navigation
bar. By default the headings at level 1,2 and 3 are added to the Navigation bar.
If headings should not be automatically added to the navigation bar, use the
empty string `''` for this parameter. If omitted, the default configuration
defined by option "capture_page_headings" in `Build.json` is used.
 
.INPUTS
None. This function does not read from a pipe.
 
.OUTPUTS
HTML elements representing navigation links to headings on the input HTML
fragment for use in the navigation bar of sites created by
`New-StaticHTMLSiteProject`.
 
.EXAMPLE
New-PageHeadingNavigation $fragment -HeadingLevels '234'
 
Create an HTML element for navigation to page headings `<h2>`, `<h3>`, `<h4>`.
All other headings are ignored. `$fragment` is a HTML framgment defined as:
 
~~~ PowerShell
$fragment = '<h2 id="bob">Hello World</h2>'
~~~
 
Output:
 
~~~ HTML
<button class="navitem">
   <a href="#bob"><span class="navitem2">Hello World</span></a>
</button>
~~~
 
Note that the heading level has been added to the css class.
 
.EXAMPLE
New-PageHeadingNavigation '<h2 id="bob">Hello World</h2>' -NavTemplate $custom
 
Create an HTML element for navigation to an heading using the custom template `$custom`.
 
~~~ PowerShell
    $custom = @{ navheading = '<span class="li-item{{level}}">{{navtext}}</span>'}
~~~
 
Output:
 
~~~ HTML
<button class="navitem">
    <a href="#bob"><span class="li-item2">Hello World</span></a>
</button>
~~~
 
Note that the heading level is used as a postfix for the css class.
 
.NOTES
This function is typically used in the build script `Build.ps1` to define
the contents of the navigation bar (placeholder `{{nav}}`).
 
.LINK
https://wethat.github.io/MarkdownToHtml/2.7/New-PageHeadingNavigation.html
.LINK
`Convert-MarkdownToHTMLFragment`
.LINK
`ConvertTo-NavigationItem`
.LINK
`New-SiteNavigation`
.LINK
`New-StaticHTMLSiteProject`
#>

function New-PageHeadingNavigation {
    [OutputType([string])]
    [CmdletBinding()]
    param(
        [parameter(Mandatory=$true,ValueFromPipeline=$false)]
        [ValidateNotNull()]
        [object]$HTMLfragment,

        [parameter(Mandatory=$false,ValueFromPipeline=$false)]
        [object[]]$NavitemSpecs,

        [parameter(Mandatory=$false,ValueFromPipeline=$false)]
        [object]$NavTemplate,

        [parameter(Mandatory=$false,ValueFromPipeline=$false)]
        [string]$HeadingLevels
    )
    # determine the values of missing parameters
    if ($cfg = $HTMLFragment.EffectiveConfiguration) {
        if (!$NavTemplate -and ($navbar = $cfg.navigation_bar )) {
            $NavTemplate = $navbar.templates
        }
        if (!$HeadingLevels -and $navbar -and $navbar.capture_page_headings) {
            $HeadingLevels = $navbar.capture_page_headings
        }
    }
    # provide defaults if needed
    if (!$NavTemplate) {
            $NavTemplate = $SCRIPT:defaultNavTemplate
    }
    if (!$HeadingLevels) {
            $HeadingLevels = "123" # capture levels 1 .. 3 by default
    }

    # Emit the page header navbar items

    if ($NavitemSpecs -and $HTMLfragment.RelativePath) {
        New-SiteNavigation -NavitemSpecs $NavitemSpecs `
                    -RelativePath $HTMLfragment.RelativePath `
                    -NavTemplate  $NavTemplate
    }
    if ($HTMLfragment -isnot [string]) {
        # fall back to string for backwards compatibility
        $HTMLfragment = $HTMLfragment.HTMLfragment
    }

    $HTMLfragment -split "`n" | ForEach-Object {
        $m = $hRE.Match($_)
        if ($m.Success -and $m.Groups.Count -eq 4) {
            # found an heading with an id attribute
            $level = $m.Groups[1].Captures.Value
            if ($headingLevels.Contains($level)) {
                # this level is enabled
                $id = $m.Groups[2].Captures -join ''
                $txt = $m.Groups[3].Captures -join ''
                # strip hyperlinks in heading text
                $txt = $aRE.Replace($txt,'')
                [string]$navheading = $NavTemplate.navheading
                if (!$navheading) {
                    $navheading = $SCRIPT:defaultNavTemplate.navheading
                }
                @{ $navheading.Replace('{{level}}',$level).Replace('{{navtext}}',$txt) = "#$($id)"}
            }
        }
    } | ConvertTo-NavigationItem -NavTemplate $NavTemplate
}

function Get-NavSpecKey {
    param(
        [parameter(Mandatory=$false,ValueFromPipeline=$false)]
        [ValidateNotNull()]
        [object]$NavSpec
    )
    if ($NavSpec -is [hashtable]) {
        $NavSpec.Keys | Where-Object {!$_.Equals('NavRoot')}
    } else { # json object
        (Get-Member -InputObject $NavSpec -MemberType NoteProperty | Where-Object {!$_.Name.Equals('NavRoot')}).Name
    }
}

<#
.SYNOPSIS
Convert a navigation specification to a single item in a navigation bar of pages
in a static HTML site project created by `New-StaticHTMLSiteProject`.
 
.DESCRIPTION
Converts the specification for a single item in the navigation bar by
* finding an HTML template to represent the item in the navigation bar.
* replacing placeholders in the template by data taken from the
  specification.
* adjusting the navigation link based on the location in the directory tree.
 
.PARAMETER RelativePath
The path to a Markdown (`*.md`) or HTML (`*.html`) file relative to its
corresponding root configured in `Build.json`. For Markdown files the
root is configured by parameter `"markdown_dir"` for html files it is
`"site_dir"`. This parameter will be used to compute relative resource
and navigation bar links for the HTML page currently
being assembled.
 
The given path should use forward slash '/' path separators.
 
.PARAMETER NavSpec
An object or dictionary with exactly one `NoteProperty` or key representing
a single key-value pair. This parameter provides the data for one item in
the navigation bar.
 
Following key-value pairs are recognized:
 
+ :----: + :---: + :--------: + ---------------------------------------------- +
| Key | Value | Template | Description
| | | Name |
+ ====== + ===== + ========== + ============================================== +
|"_text_"|"_url_"| navitem | A clickable hyperlink where _text_ is the link
| | | | text and _url_ a link to a web page, a relative
| | | | path to a local Markdown file, or a local
| | | | directory containing Markdown files. Links
| | | | must be relative to the project root, unless
| | | | the navigation specification is defined
| | | | in a `Build.json` which is **not** at the
| | | | project root, but somewhere below `markdown_dir`.
| | | | In that case the link must be relative to that
| | | | `Build.json` file.
+ ------ + ----- + ---------- + ---------------------------------------------- +
|"_text_"| "" | navlabel | A label without hyperlink.
+ ------ + ----- + ---------- + ---------------------------------------------- +
| "---" | "" |navseparator| A speparator line.
+ ------ + ----- + ---------- + ---------------------------------------------- +
 
The structure of the data determines the type of navigation bar item to build.
The table above maps supported key-value combinations to the associated named
HTML templates.
 
.PARAMETER NavTemplate
An optional dictionary of named HTML templates.
 
+ :--------: + :----------: + ----------------------------------------
| Type | Key | HTML Template
+ ========== + ============ + ========================================
| clickable | "navitem" | ~~~ html
| link | | <button class='navitem'>
| | | <a href='{{navurl}}'>{{navtext}}</a>
| | | </button>
| | | ~~~
+ ---------- + ------------ + ----------------------------------------
| label (no | "navlabel" | ~~~ html
| link) | | <div class='navlabel'>{{navtext}}</div>
| | | ~~~
+ ---------- + ------------ + ----------------------------------------
| separator |"navseparator"| ~~~ html
| | | <hr/>
| | | ~~~
+ ---------- + ------------ + ----------------------------------------
 
The HTML templates listed represent are the values configured in `Build.json`.
These values are used for keys not provided in the given dictionary or if no
dictionary is given at all. Additional mappings contained in dictionary are
ignored.
 
For more customization options see
[Static Site Project Customization](about_MarkdownToHTML.md#static-site-project-customization).
 
The css styles used in the templates are defined in `md-styles.css`.
 
During the build process the placeholders contained in the HTML template
are replaced with content extracted from the `NavSpec` parameter .
 
| Placeholder | Description
| :-----------: | -----------
| `{{navurl}}` | hyperlink to web-page or local file.
| `{{navtext}}` | link or label text
 
.INPUTS
Objects or hashtables with one NoteProperty or key.
 
.OUTPUTS
HTML element representing one navigation item for use in a vertical navigation
bar.
 
.EXAMPLE
ConvertTo-NavigationItem @{'Project HOME'='https://github.com/WetHat/MarkdownToHtml'} -RelativePath 'intro/about.md'
 
Generates a web navigation link to be put on the page `intro/about.md`.
Note:
the parameter `RelativePath` is specified but not used because the link is
non-local.
 
Output:
 
~~~ html
<button class='navitem'>
    <a href="https://github.com/WetHat/MarkdownToHtml">Project HOME</a>
</button><br/>
~~~
 
.EXAMPLE
ConvertTo-NavigationItem @{'Index'='index.md'} -RelativePath 'intro/about.md'
 
Generates a navigation link relative to `intro/about.md`. The
link target `index.md` is a page at the root of the same site, hence the link is
adjusted accordingly.
 
Output:
 
~~~ html
<button class="navitem">
    <a href="../index.html">Index</a>
</button>
~~~
 
.EXAMPLE
ConvertTo-NavigationItem @{'Index'='index.md'} -RelativePath 'intro/about.md' -NavTemplate $custom
 
Generates a navigation link relative to `intro/about.md`.
A custom template definition `$custom` is used:
 
~~~ PowerShell
    $custom = @{ navitem = '<li class="li-item"><a href="{{navurl}}">{{navtext}}</a></li>'}
~~~
 
The link target `index.md` is a page at the root of the same site, hence the link is
adjusted accordingly.
 
Output:
 
~~~ html
<li class="li-item">
    <a href="../index.html">Index</a>
</li>
~~~
 
.EXAMPLE
ConvertTo-NavigationItem @{'---'=''} -RelativePath 'intro/about.md'
 
Generates a separator line in the navigation bar. The _RelativePath_ parameter
is not used (the specification does not contain a link).
 
Output:
 
~~~ html
<hr class="navitem" />
~~~
 
.EXAMPLE
ConvertTo-NavigationItem @{'---'=''} -NavTemplate $custom
 
Generates a separator line in the navigation bar using a custom template `$custom`:
 
~~~ PowerShell
    $custom = @{ navseparator = '<hr class="li-item" />'}
~~~
 
Output:
 
~~~ html
<hr class="li-item" />
~~~
 
.EXAMPLE
ConvertTo-NavigationItem @{'Introduction'=''}
 
Generates a label in the navigation bar.
 
Output:
 
~~~ html
<div class='navitem'>Introduction</div>
~~~
 
.EXAMPLE
ConvertTo-NavigationItem @{'Introduction'=''} -NavTemplate $custom
 
Generates a label in the navigation bar using the custom template `$custom`:
 
~~~ PowerShell
    $custom = @{ navlabel = '<div class='li-item'>Introduction</div>'}
~~~
 
Output:
 
~~~ html
<div class='li-item'>Introduction</div>
~~~
 
.NOTES
This function is typically used in the build script `Build.ps1` to define
the contents of the navigation bar (placeholder `{{nav}}`).
 
.LINK
https://wethat.github.io/MarkdownToHtml/2.7/ConvertTo-NavigationItem.html
.LINK
`New-StaticHTMLSiteProject`
.LINK
`New-PageHeadingNavigation`
#>

function ConvertTo-NavigationItem {
    [OutputType([string])]
    [CmdletBinding()]
    param(
        [parameter(Mandatory=$false,ValueFromPipeline=$true)]
        [ValidateNotNull()]
        [object]$NavSpec,
        [parameter(Mandatory=$false,ValueFromPipeline=$false)]
        [string]$RelativePath = '',
        [parameter(Mandatory=$false,ValueFromPipeline=$false)]
        [object]$NavTemplate = $defaultNavTemplate
    )
    PROCESS {
        # create page specific navigation links by making the path relative to
        # the current location specified by `RelativePath`
        $name = Get-NavSpecKey -NavSpec $NavSpec
        $link = $NavSpec.$name

        if ([string]::IsNullOrWhiteSpace($link)) {
            if ($name.StartsWith('---')) { # separator line
                [string]$navseparator = $NavTemplate.navseparator
                if (!$navseparator) {
                    $navseparator = $SCRIPT:defaultNavTemplate.navseparator
                }
                Write-Output $navseparator # separator
            } else { # label
                [string]$navlabel = $NavTemplate.navlabel
                if (!$navlabel) {
                    $navlabel = $SCRIPT:defaultNavTemplate.navlabel
                }
                Expand-HtmlTemplate -InputObject $navlabel -ContentMap @{'{{navtext}}' = $name}
            }
        } else {
            # we have an actual link
            if (!$link.StartsWith('http')) {
                # handle fragment on page

                $hash = $link.IndexOf('#')

                switch ($hash) {
                    {$_ -eq 0 } { # '#target'
                        $file = ''
                        $fragment = $link
                        Break
                    }
                    {$_ -ge 0} {
                        $file = $link.Substring(0,$hash)
                        $fragment = $link.Substring($hash)
                        Break
                    }
                    default {
                        $file = $link
                        $fragment = ''
                    }
                }

                # fix file type
                if ($file.EndsWith('.md') -or $file.EndsWith('.markdown')) {
                    $file = [System.IO.Path]::ChangeExtension($file,'html')
                 }

                # Re-assemble the link
                $link = $file + $fragment
            }

            [string]$navitem = $NavTemplate.navitem
            if (!$navitem) {
                $navitem = $SCRIPT:defaultNavTemplate.navitem
            }

            # optimize the path if the nav spec has a NavRoot property
            if ($NavSpec.NavRoot) {
                $fromPathSegments = $RelativePath.Split("\")
                $toPathSegments = $NavSpec.NavRoot.Split("\")
                [int]$n = [Math]::min($fromPathSegments.Length, $toPathSegments.Length)
                [int]$unique=0
                while ($fromPathSegments[$unique] -eq $toPathSegments[$unique] -and $unique -lt $n) {
                    $unique++
                }
                $relpath = $fromPathSegments[$unique..$($fromPathSegments.Count-1)] -join '/'
            } else {
                $relpath = $RelativePath.Replace('\','/')
            }

            Expand-HtmlTemplate -InputObject $navitem -ContentMap @{
                '{{navurl}}'  = $link
                '{{navtext}}' = $name
            } |  Update-ResourceLinks -RelativePath $relpath
        }
    }
}

<#
.SYNOPSIS
Expand directory link targets one level to links to contained markdown files.
 
.DESCRIPTION
Scans a collection of navigation links for link targets pointing to directories
and inserts links to all directly contained markdown files.
 
.PARAMETER LiteralPath
Absolute path to the directory the navigation itens are relative to. Usually
this is the path to the directory containing the `Build.json` file the navigation
items are from or the directory configured by `markdown_dir` if the navigation
items are from the top-level `Build.json` file.
 
.PARAMETER NavSpec
The specification of a single navigation item as specified in
`ConvertTo-NavigationItem`.
 
.INPUTS
Specifications of navigation items as specified in
`ConvertTo-NavigationItem`.
 
.OUTPUTS
Specification of navigation items as specified in
`ConvertTo-NavigationItem` where all links to directories
are replaced by links to the directory contents (one level).
 
.EXAMPLE
@{'My Directory' = 'foo'} | Expand-DirectoryNavigation -LiteralPath e:\stuff
 
Expands the directory `foo` which is a subdirectory of `e:\stuff`.
Assuming the following directory structure and content:
 
~~~bob
e:\stuff
    |
    '- foo
        |
        +- bar.md
        |
        '- baz.md
~~~
 
where `bar.md` and `baz.md` are two markdown files.
 
This expands to:
 
~~~PowerShell
@{'My Directory' = ""} # <- label
@{'bar' = "foo/bar.md"} # <- link
@{'baz' = "foo/baz.md"} # <- link
~~~
 
.LINK
https://wethat.github.io/MarkdownToHtml/2.7/Expand-DirectoryNavigation.html
.LINK
`ConvertTo-NavigationItem`
#>

function Expand-DirectoryNavigation {
    [OutputType([System.IO.FileInfo])]
    [CmdletBinding()]
    param (
        [parameter(Mandatory=$true,ValueFromPipeline=$false)]
        [ValidateNotNullorEmpty()]
        [string]$LiteralPath,
        [parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [ValidateNotNullorEmpty()]
        [object]$NavSpec
    )

    PROCESS {
        # get the name of the navigation link
        $name = Get-NavSpecKey -NavSpec $NavSpec
        $link = $NavSpec.$name
        if ($link) {
            $target = Join-Path $LiteralPath $link
            if ([System.IO.Directory]::Exists($target)) {
                # target is indeed a director - expand
                # first emit a label
                @{ $name = ''}
                # followed by the file contents
                Get-ChildItem $target -File -Filter '*.m*' `
                | Where-Object { $_.Extension -in '.md','.markdown'} `
                | ForEach-Object {
                    $relpath = Join-Path $link $_.Name
                    @{ $_.BaseName = $relpath}
                }
            } else {
                # Pass through
                $NavSpec
            }
        } else {
            # Pass through
            $NavSpec
        }
    }
}


<#
.SYNOPSIS
Build navigation bar content for a HTML page from a specification and
redering templates
 
.DESCRIPTION
A navigation bar section for a HTML page is built by:
* processing a list of item specification which define the navigation bar
  content
* picking the appropriate HTML template for each navigation bar item
  and expanding the template using data from the item specification
 
.PARAMETER NavitemSpecs
An array where each item is an object or dictionary with exactly one NoteProperty`
or key representing a single key-value pair. This parameter provides the data
for **one** item in the navigation bar.
 
Following key-value pairs are recognized:
 
+ :----: + :---: + :--------: + ---------------------------------------------- +
| Key | Value | Template | Description
| | | Name |
+ ====== + ===== + ========== + ============================================== +
|"_text_"|"_url_"| navitem | A clickable hyperlink where _text_ is the link
| | | | text and _url_ a link to a web page or a local
| | | | Markdown file. Local file links must be
| | | | relative to the project root.
+ ------ + ----- + ---------- + ---------------------------------------------- +
|"_text_"| "" | navlabel | A label without hyperlink.
+ ------ + ----- + ---------- + ---------------------------------------------- +
| "---" | "" |navseparator| A speparator line.
+ ------ + ----- + ---------- + ---------------------------------------------- +
 
The structure of the data determines the type of navigation bar item to build.
The table above maps supported key-value combinations to the associated named
HTML templates.
 
.PARAMETER RelativePath
The path to a Markdown (`*.md`) or HTML (`*.html`) file relative to its
corresponding root configured in `Build.json`. For Markdown files the
root is configured by parameter `"markdown_dir"` for HTML files it is
`"site_dir"`. This parameter will be used to compute relative resource
and navigation bar links for the HTML page currently
being assembled.
 
The given path should use forward slash '/' path separators.
 
.PARAMETER NavTemplate
An optional dictionary of named HTML templates.
 
+ :--------: + :----------: + ----------------------------------------
| Type | Key | HTML Template
+ ========== + ============ + ========================================
| clickable | "navitem" | ~~~ html
| link | | <button class='navitem'>
| | | <a href='{{navurl}}'>{{navtext}}</a>
| | | </button>
| | | ~~~
+ ---------- + ------------ + ----------------------------------------
| label (no | "navlabel" | ~~~ html
| link) | | <div class='navlabel'>{{navtext}}</div>
| | | ~~~
+ ---------- + ------------ + ----------------------------------------
| separator |"navseparator"| ~~~ html
| | | <hr/>
| | | ~~~
+ ---------- + ------------ + ----------------------------------------
 
The HTML templates listed represent are the values configured in `Build.json`.
These values are used for keys not provided in the given dictionary or if no
dictionary is given at all. Additional mappings contained in dictionary are
ignored.
 
For more customization options see
[Static Site Project Customization](about_MarkdownToHTML.md#static-site-project-customization).
 
The css styles used in the templates are defined in `md-styles.css`.
 
During the build process the placeholders contained in the HTML template
are replaced with content extracted from the `NavSpec` parameter .
 
| Placeholder | Description
| :-----------: | -----------
| `{{navurl}}` | hyperlink to web-page or local file.
| `{{navtext}}` | link or label text
 
.INPUTS
None
 
.OUTPUTS
HTML fragment for a navigation bar.
 
.EXAMPLE
New-SiteNavigation -NavitemSpecs $specs -RelativePath 'a/b/c/bob.md'
 
Create navigation content for a file with relative path `a/b/c/bob.md` and
the specification `$specs` and default templates:
 
`$specs` is defined like so:
 
~~~PowerShell
$specs = @(
    @{ "<img width='90%' src='logo.png' />" = "README.md" },
    @{ "README" = "" },
    @{ "Home" = "README.md" },
    @{ "---" = "" }
)
~~~
 
Output:
 
~~~ html
<button class="navitem">
   <a href="../../../README.html">
      <img width='90%' src='../../../logo.png' />
   </a>
</button>
<div class="navlabel">README</div>
<button class="navitem">
   <a href="../../../README.html">Home</a>
</button>
<hr/>
~~~
 
Note how the relative path parameter was used to update the links.
 
.LINK
https://wethat.github.io/MarkdownToHtml/2.7/New-SiteNavigation.html
#>

function New-SiteNavigation {
    [OutputType([string])]
    [CmdletBinding()]

    param(
        [parameter(Mandatory=$true,ValueFromPipeline=$false)]
        [ValidateNotNull()]
        [object[]]$NavitemSpecs,

        [parameter(Mandatory=$false,ValueFromPipeline=$false)]
        [string]$RelativePath = '',

        [parameter(Mandatory=$false,ValueFromPipeline=$false)]
        [object]$NavTemplate = $defaultNavTemplate
    )

    $NavitemSpecs | ConvertTo-NavigationItem -RelativePath $RelativePath `
                                             -NavTemplate $NavTemplate
}

# SIG # Begin signature block
# MIIFYAYJKoZIhvcNAQcCoIIFUTCCBU0CAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQ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