@@ -5,84 +5,118 @@ Param (
5
5
6
6
$tocContents = " items:`n "
7
7
8
- $componentsRoot = Resolve-Path $PSScriptRoot / ../ components/
9
-
10
- # For each component
11
- foreach ($componentFolder in Get-ChildItem - Path $componentsRoot - Directory) {
12
- $componentName = $componentFolder.Name
13
-
14
- # Add component to TOC
15
- $tocContents = $tocContents + " - name: $componentName `n items:`n "
16
-
17
- # Find each markdown file in component's samples folder
18
- foreach ($markdownFile in Get-ChildItem - Recurse - Path " $componentFolder /samples/**/*.md" | Where-Object { $_.FullName -notlike " *\bin\*" -and $_FullName -notlike " *\obj\*" }) {
19
- $contents = Get-Content $markdownFile - Raw
20
-
21
- # Find title
22
- $contents -match ' title:\s*(?<title>.*)' | Out-Null
8
+ function AppendTocItem ([string ] $name , [int ] $level , [hashtable ] $properties ) {
9
+ $indent = " " * $level
10
+ $firstLineIndent = " " * ($level - 1 )
23
11
24
- $header = $Matches.title
25
-
26
- # Find end of YAML
27
- $endIndex = $contents | Select-String - Pattern " ---" - AllMatches | ForEach-Object { $_.Matches [1 ].Index }
28
-
29
- # Insert Header
30
- $contents = $contents.Substring (0 , $endIndex + 5 ) + " `n # $header `n " + $contents.Substring ($endIndex + 5 )
12
+ $lines = " $firstLineIndent - name: $name `n "
31
13
32
- # Find Sample Placeholders, replace with code content
33
- foreach ($sample in ($contents | Select-String - Pattern ' >\s*\[!SAMPLE\s*(?<sampleid>.*)\s*\]\s*' - AllMatches).Matches) {
34
- $sampleid = $sample.Groups [1 ].Value
35
- $sampleString = $sample.Groups [0 ].Value
14
+ foreach ($key in $properties.Keys ) {
15
+ $lines += " $indent $ ( $key ) : $ ( $properties [$key ]) `n "
16
+ }
36
17
37
- # Find matching filename for CS
38
- foreach ($csFile in Get-ChildItem - Recurse - Path ($markdownFile.DirectoryName + ' \**\*.xaml.cs' ).Replace(' \' , ' /' ) |
39
- Where-Object { $_.FullName -notlike " *\bin\*" -and $_FullName -notlike " *\obj\*" }) {
40
- $csSample = Get-Content $csFile - Raw
41
-
42
- if ($csSample -match ' \[ToolkitSample\s?\(\s*id:\s*(?:"|nameof\()\s?' + $sampleid + ' \s?(?:"|\))' ) {
43
- # Get Relative Path
44
- $docPath = $ (Join-Path " components" $ ($csfile.FullName.Replace ($componentsRoot.Path , ' ' ))).Replace(' \' , ' /' ).Trim(' /' )
18
+ return $lines
19
+ }
45
20
46
- # See https://learn.microsoft.com/en-us/contribute/content/code-in-docs#out-of-repo-snippet-references
47
- $snippet = ' :::code language="xaml" source="~/../code-windows/' + $docPath.Substring (0 , $docPath.Length - 3 ) + ' ":::' + " `n`n "
21
+ function GetTitleFrontMatterFromMarkdownFile ($markdownFile ) {
22
+ $contents = Get-Content $markdownFile - Raw
23
+ return GetTitleFrontMatterFromMarkdownContents $contents
24
+ }
48
25
49
- $snippet = $snippet + ' :::code language="csharp" source="~/../code-windows/' + $docPath + ' ":::' + " `n`n "
26
+ function GetTitleFrontMatterFromMarkdownContents ($contents ) {
27
+ $contents -match ' title:\s*(?<title>.*)' | Out-Null
28
+ return $Matches.title
29
+ }
50
30
51
- # Replace our Sample Placeholder with references for docs
52
- $contents = $contents.Replace ($sampleString , $snippet )
53
- }
31
+ function ProcessMarkdownFile ($markdownFile ) {
32
+ $contents = Get-Content $markdownFile - Raw
33
+ $header = GetTitleFrontMatterFromMarkdownContents $contents
34
+
35
+ # Find end of YAML
36
+ $endIndex = $contents | Select-String - Pattern " ---" - AllMatches | ForEach-Object { $_.Matches [1 ].Index }
37
+
38
+ # Insert Header
39
+ $contents = $contents.Substring (0 , $endIndex + 5 ) + " `n # $header `n " + $contents.Substring ($endIndex + 5 )
40
+
41
+ # Find Sample Placeholders, replace with code content
42
+ foreach ($sample in ($contents | Select-String - Pattern ' >\s*\[!SAMPLE\s*(?<sampleid>.*)\s*\]\s*' - AllMatches).Matches) {
43
+ $sampleid = $sample.Groups [1 ].Value
44
+ $sampleString = $sample.Groups [0 ].Value
45
+
46
+ # Find matching filename for CS
47
+ foreach ($csFile in Get-ChildItem - Recurse - Path ($markdownFile.DirectoryName + ' \**\*.xaml.cs' ).Replace(' \' , ' /' ) |
48
+ Where-Object { $_.FullName -notlike " *\bin\*" -and $_FullName -notlike " *\obj\*" }) {
49
+ $csSample = Get-Content $csFile - Raw
50
+
51
+ if ($csSample -match ' \[ToolkitSample\s?\(\s*id:\s*(?:"|nameof\()\s?' + $sampleid + ' \s?(?:"|\))' ) {
52
+ # Get Relative Path
53
+ $docPath = $ (Join-Path " components" $ ($csfile.FullName.Replace ($componentsRoot.Path , ' ' ))).Replace(' \' , ' /' ).Trim(' /' )
54
+
55
+ # See https://learn.microsoft.com/en-us/contribute/content/code-in-docs#out-of-repo-snippet-references
56
+ $snippet = ' :::code language="xaml" source="~/../code-windows/' + $docPath.Substring (0 , $docPath.Length - 3 ) + ' ":::' + " `n`n "
57
+
58
+ $snippet = $snippet + ' :::code language="csharp" source="~/../code-windows/' + $docPath + ' ":::' + " `n`n "
59
+
60
+ # Replace our Sample Placeholder with references for docs
61
+ $contents = $contents.Replace ($sampleString , $snippet )
54
62
}
55
63
}
56
-
57
- # Make any learn links relative
58
- $contents = $contents.Replace (' https://learn.microsoft.com' , ' ' )
64
+ }
59
65
60
- $mdOutputPath = Join-Path $OutputDir $componentName
61
-
62
- if (-not (Test-Path $mdOutputPath )) {
63
- New-Item - ItemType Directory - Path $mdOutputPath | Out-Null
64
- }
65
-
66
- $markdownFileName = $markdownFile.Name
66
+ # Make any learn links relative
67
+ $contents = $contents.Replace (' https://learn.microsoft.com' , ' ' )
68
+
69
+ $markdownFileName = $markdownFile.Name
70
+
71
+ # If the file is named the same as the component, rename it to index.md
72
+ # This is so that the URL for the component is /componentName instead of /componentName/componentName
73
+ if ($markdownFileName -eq " $componentName .md" ) {
74
+ $markdownFileName = " index.md"
75
+ }
76
+
77
+ $mdOutputPath = Join-Path $OutputDir $componentName
78
+ $mdOutputFile = Join-Path $mdOutputPath $markdownFileName
79
+
80
+ # Create output directory if it doesn't exist
81
+ if (-not (Test-Path $mdOutputPath )) {
82
+ New-Item - ItemType Directory - Path $mdOutputPath | Out-Null
83
+ }
84
+
85
+ # Write file contents
86
+ Write-Host ' Writing File:' , $mdOutputFile
87
+ $contents | Set-Content $mdOutputFile
88
+
89
+ return $mdOutputFile
90
+ }
67
91
68
- # If the file is named the same as the component, rename it to index.md
69
- # This is so that the URL for the component is /componentName instead of /componentName/componentName
70
- if ($markdownFileName -eq " $componentName .md" ) {
71
- $markdownFileName = " index.md"
72
- }
92
+ $componentsRoot = Resolve-Path $PSScriptRoot / ../ components/
73
93
74
- $mdOutputFile = Join-Path $mdOutputPath $markdownFileName
94
+ # For each component
95
+ foreach ($componentFolder in Get-ChildItem - Path $componentsRoot - Directory) {
96
+ $componentName = $componentFolder.Name
75
97
76
- # Write file contents
77
- Write-Host ' Writing File:' , $mdOutputFile
78
- $contents | Set-Content $mdOutputFile
98
+ # Add component to TOC
99
+ $markdownFiles = Get-ChildItem - Recurse - Path " $componentFolder /samples/**/*.md" | Where-Object { $_.FullName -notlike " *\bin\*" -and $_FullName -notlike " *\obj\*" }
79
100
80
- # Add to TOC
81
- $mdOutputFile = $mdOutputFile.Trim (' /' ) # need to remove initial / from path
82
-
83
- # TOC is placed within output directory, hrefs are relative to TOC
84
- $tocHref = $mdOutputFile.Replace ($OutputDir , ' ' ).Trim(' \' )
85
- $tocContents = $tocContents + " - name: $header `n href: $tocHref `n "
101
+ # If there's only one markdown file, append it to the root of the TOC
102
+ if ($markdownFiles.Count -eq 1 ) {
103
+ $header = GetTitleFrontMatterFromMarkdownFile $markdownFiles [0 ]
104
+ $mdOutputFile = ProcessMarkdownFile $markdownFiles [0 ]
105
+
106
+ $tocHref = $mdOutputFile.Trim (' /' ).Replace($OutputDir , ' ' ).Trim(' \' )
107
+ $tocContents += AppendTocItem $header 1 @ { " href" = $tocHref }
108
+ }
109
+ else {
110
+ $tocContents += AppendTocItem $componentName 1 @ { " items" = " " }
111
+
112
+ # For each markdown file
113
+ foreach ($markdownFile in $markdownFiles ) {
114
+ $header = GetTitleFrontMatterFromMarkdownFile $markdownFile
115
+ $mdOutputFile = ProcessMarkdownFile $markdownFile
116
+
117
+ $tocHref = $mdOutputFile.Trim (' /' ).Replace($OutputDir , ' ' ).Trim(' \' )
118
+ $tocContents += AppendTocItem $header 2 @ { " href" = $tocHref }
119
+ }
86
120
}
87
121
}
88
122
0 commit comments