1
+ $include = $env: RESOLVE_INCLUDE ?? ' *.md'
2
+ $exclude = $env: RESOLVE_EXCLUDE ?? $null
3
+ $recurse = [bool ]::Parse($env: RESOLVE_RECURSE ?? " true" )
4
+ $validate = [bool ]::Parse($env: RESOLVE_VALIDATE ?? " true" )
5
+
6
+ Write-Output " Include: $include "
7
+ Write-Output " Exclude: $exclude "
8
+
9
+ $files = if ($recurse ) {
10
+ Get-ChildItem - Include $include - Exclude $exclude - Recurse
11
+ } else {
12
+ Get-ChildItem - Include $include - Exclude $exclude
13
+ }
14
+
15
+ foreach ($file in $files ) {
16
+ Write-Output " Processing $file "
17
+ $content = ((Get-Content $file - Raw - Encoding UTF8) ?? ' ' ).Trim()
18
+ if ($content.StartsWith (' <!-- exclude -->' ) -or $content.EndsWith (' <!-- exclude -->' )) {
19
+ Write-Output " Excluding $file "
20
+ continue
21
+ }
22
+
23
+ $replacements = @ {}
24
+ foreach ($match in (Select-String - Pattern ' <!--\s?include (.*?)\s?-->' - Path $file )) {
25
+ $includedPath = ($match.Matches [0 ].Value -replace ' <!--\s?include ' , ' ' -replace ' \s?-->' , ' ' ).Trim()
26
+ $fragment = $null
27
+ if ($includedPath.Contains (' #' )) {
28
+ $fragment = ' #' + $includedPath.Split (' #' )[1 ]
29
+ $includedPath = $includedPath.Substring (0 , $includedPath.IndexOf (' #' ))
30
+ }
31
+
32
+ $isuri = [uri ]::IsWellFormedUriString($includedPath , ' Absolute' )
33
+ $includedFullPath = Join-Path (Get-ChildItem - Path $file ).DirectoryName - ChildPath $includedPath
34
+
35
+ if ((Test-Path $includedFullPath ) -Or $isuri ) {
36
+ $include = if ($isuri ) {
37
+ (Invoke-RestMethod $includedPath ) ?? ' '
38
+ } else {
39
+ (Get-Content $includedFullPath - Raw - Encoding UTF8) ?? ' '
40
+ }
41
+
42
+ # Resolve fragment specifier if present
43
+ if ($fragment ) {
44
+ $anchor = " <!-- $fragment -->"
45
+ $start = $include.IndexOf ($anchor )
46
+ if ($start -eq -1 ) {
47
+ if ($validate ) {
48
+ Write-Error " Referenced anchor $fragment not found in $includedPath "
49
+ } else {
50
+ Write-Warning " Referenced anchor $fragment not found in $includedPath "
51
+ }
52
+ continue
53
+ }
54
+ $include = $include.Substring ($start )
55
+ $end = $include.IndexOf ($anchor , $anchor.Length )
56
+ if ($end -ne -1 ) {
57
+ $include = $include.Substring (0 , $end + $anchor.Length )
58
+ }
59
+ }
60
+ # TODO: disable nested includes until we can properly support that scenario, and nested excludes
61
+ $include = $include -replace ' <!--\s?include ' , ' <!-- ' -replace ' <!-- exclude -->' , ' '
62
+ # see if we already have a section we previously replaced
63
+ $existingRegex = " <!--\s?include $includedPath$fragment \s?-->[\s\S]*<!-- $includedPath$fragment -->"
64
+ $replacement = " <!-- include $includedPath$fragment -->`n $include `n <!-- $includedPath$fragment -->"
65
+ if ($content -match $existingRegex ) {
66
+ $replacements [$existingRegex ] = $replacement
67
+ } else {
68
+ $replacements [" <!--\s?include $includedPath$fragment \s?-->" ] = $replacement
69
+ }
70
+ } else {
71
+ if ($validate ) {
72
+ Write-Error " Included file $includedPath in $ ( $file.Name ) not found"
73
+ } else {
74
+ Write-Warning " Included file $includedPath in $ ( $file.Name ) not found"
75
+ }
76
+ }
77
+ }
78
+
79
+ if ($replacements.Count -gt 0 ) {
80
+ foreach ($replacement in $replacements.GetEnumerator ()) {
81
+ # Write-Host "Replacing $($replacement.Key) with $($replacement.Value)"
82
+ $content = $content -replace $replacement.Key , $replacement.Value
83
+ }
84
+
85
+ $content = $content.TrimEnd ()
86
+ $actual = (Get-Content $file - Raw - Encoding UTF8).TrimEnd()
87
+
88
+ if ($content -ne $actual ) {
89
+ Set-Content $file - Value $content.TrimEnd () - Encoding UTF8
90
+ Write-Output " Updated $ ( $file.Name ) "
91
+ }
92
+ }
93
+ }
0 commit comments