1+ Param (
2+ [Parameter (HelpMessage = " Where to output docs" )]
3+ [string ]$OutputDir = " docs"
4+ )
5+
6+ $tocContents = " items:`n "
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
23+
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 )
31+
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+ {
35+ $sampleid = $sample.Groups [1 ].Value
36+ $sampleString = $sample.Groups [0 ].Value
37+
38+ # Find matching filename for CS
39+ foreach ($csFile in Get-ChildItem - Recurse - Path ($markdownFile.DirectoryName + ' \**\*.xaml.cs' ).Replace(' \' , ' /' ) |
40+ Where-Object {$_.FullName -notlike " *\bin\*" -and $_FullName -notlike " *\obj\*" })
41+ {
42+ $csSample = Get-Content $csFile - Raw
43+
44+ if ($csSample -match ' \[ToolkitSample\s?\(\s*id:\s*(?:"|nameof\()\s?' + $sampleid + ' \s?(?:"|\))' )
45+ {
46+ # Get Relative Path
47+ $docPath = $ (Join-Path " components" $ ($csfile.FullName.Replace ($componentsRoot.Path , ' ' ))).Replace(' \' , ' /' ).Trim(' /' )
48+
49+ # See https://learn.microsoft.com/en-us/contribute/content/code-in-docs#out-of-repo-snippet-references
50+ $snippet = ' :::code language="xaml" source="~/../code-windows/' + $docPath.Substring (0 , $docPath.Length - 3 ) + ' ":::' + " `n`n "
51+
52+ $snippet = $snippet + ' :::code language="csharp" source="~/../code-windows/' + $docPath + ' ":::' + " `n`n "
53+
54+ # Replace our Sample Placeholder with references for docs
55+ $contents = $contents.Replace ($sampleString , $snippet )
56+ }
57+ }
58+ }
59+
60+ # Make any learn links relative
61+ $contents = $contents.Replace (' https://learn.microsoft.com' , ' ' )
62+
63+ # create output directory if it doesn't exist
64+ $mdOutputPath = Join-Path $OutputDir $componentName
65+
66+ if (-not (Test-Path $mdOutputPath )) {
67+ New-Item - ItemType Directory - Path $mdOutputPath | Out-Null
68+ }
69+
70+ $mdOutputFile = Join-Path $mdOutputPath $markdownFile.Name
71+
72+ # Write file contents
73+ Write-Host ' Writing File:' , $mdOutputFile
74+ $contents | Set-Content $mdOutputFile
75+
76+ # Add to TOC
77+ $mdOutputFile = $mdOutputFile.Trim (' /' ) # need to remove initial / from path
78+
79+ # TOC is placed within output directory, hrefs are relative to TOC
80+ $tocHref = $mdOutputFile.Replace ($OutputDir , ' ' ).Trim(' \' )
81+ $tocContents = $tocContents + " - name: $header `n href: $tocHref `n "
82+ }
83+ }
84+
85+ Write-Host ' Writing TOC'
86+ $tocContents | Set-Content (Join-Path $OutputDir " TOC.yml" )
0 commit comments