1
+ Param (
2
+ [Parameter (HelpMessage = " Where to output docs" )]
3
+ [string ]$OutputDir = " docs"
4
+ )
5
+
6
+ $tocContents = " items:`n "
7
+
8
+ function AppendTocItem ([string ] $name , [int ] $level , [hashtable ] $properties ) {
9
+ $indent = " " * $level
10
+ $firstLineIndent = " " * ($level - 1 )
11
+
12
+ $lines = " $firstLineIndent - name: $name `n "
13
+
14
+ foreach ($key in $properties.Keys ) {
15
+ $lines += " $indent $ ( $key ) : $ ( $properties [$key ]) `n "
16
+ }
17
+
18
+ return $lines
19
+ }
20
+
21
+ function GetTitleFrontMatterFromMarkdownFile ($markdownFile ) {
22
+ $contents = Get-Content $markdownFile - Raw
23
+ return GetTitleFrontMatterFromMarkdownContents $contents
24
+ }
25
+
26
+ function GetTitleFrontMatterFromMarkdownContents ($contents ) {
27
+ $contents -match ' title:\s*(?<title>.*)' | Out-Null
28
+ return $Matches.title
29
+ }
30
+
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 )
62
+ }
63
+ }
64
+ }
65
+
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
+ }
91
+
92
+ $componentsRoot = Resolve-Path $PSScriptRoot / ../ components/
93
+
94
+ # For each component
95
+ foreach ($componentFolder in Get-ChildItem - Path $componentsRoot - Directory) {
96
+ $componentName = $componentFolder.Name
97
+
98
+ # Add component to TOC
99
+ $markdownFiles = Get-ChildItem - Recurse - Path " $componentFolder /samples/**/*.md" | Where-Object { $_.FullName -notlike " *\bin\*" -and $_FullName -notlike " *\obj\*" }
100
+
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
+ }
120
+ }
121
+ }
122
+
123
+ Write-Host ' Writing TOC'
124
+ $tocContents | Set-Content (Join-Path $OutputDir " TOC.yml" )
0 commit comments