Skip to content

Commit 1438539

Browse files
Add initial script to export docs in format for docs repo
1 parent 058718f commit 1438539

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

Scripts/ExportDocs.ps1

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
Param (
2+
[Parameter(HelpMessage = "Where to output docs")]
3+
[string]$OutputDir,
4+
5+
[Parameter(HelpMessage = "What to name folder for components")]
6+
[string]$folderName
7+
)
8+
9+
$preWorkingDir = $pwd;
10+
Set-Location $PSScriptRoot;
11+
12+
$OutputDir = Join-Path $preWorkingDir $OutputDir
13+
14+
# Find all Markdown documents
15+
foreach ($markdownFile in Get-ChildItem -Recurse -Path '../../components/*/samples/**/*.md' |
16+
Where-Object {$_.FullName -notlike "*\bin\*" -and $_FullName -notlike "*\obj\*"}) {
17+
$contents = Get-Content $markdownFile -Raw
18+
19+
$filePath = $markdownFile.FullName.Substring($preWorkingDir.Path.Length).Replace('\', '/').Trim('/')
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
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 = $csfile.FullName.Substring($preWorkingDir.Path.Length).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 + '":::'
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+
$outputFile = (Join-Path $OutputDir $filePath.Replace('components','').Replace('samples','').Replace('\\', '\'))
65+
[System.IO.Directory]::CreateDirectory((Split-Path $outputFile)) | Out-Null
66+
67+
# Write file contents
68+
Write-Host 'Writing File:', $outputFile
69+
$contents | Set-Content $outputFile
70+
}
71+
72+
Set-Location $preWorkingDir;

0 commit comments

Comments
 (0)