|
| 1 | +function Compare-OpenSpecToLiveHtml { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Compares converted markdown structure to the live HTML spec page on learn.microsoft.com. |
| 5 | + .DESCRIPTION |
| 6 | + For each converted spec, fetches the live spec page HTML and extracts its structure |
| 7 | + (headings, section IDs). Compares with the converted markdown structure and reports |
| 8 | + missing sections, heading/ID mismatches, and suggested manual review items. |
| 9 | + #> |
| 10 | + [CmdletBinding()] |
| 11 | + param( |
| 12 | + [string]$OutputPath = (Join-Path -Path (Get-Location) -ChildPath 'converted-specs'), |
| 13 | + |
| 14 | + [string[]]$ProtocolId |
| 15 | + ) |
| 16 | + |
| 17 | + if (-not (Test-Path -LiteralPath $OutputPath)) { |
| 18 | + throw "Output path '$OutputPath' was not found." |
| 19 | + } |
| 20 | + |
| 21 | + $catalog = @{} |
| 22 | + try { |
| 23 | + foreach ($e in (Get-OpenSpecCatalog)) { |
| 24 | + $catalog[$e.ProtocolId] = $e |
| 25 | + $norm = $e.ProtocolId -replace '-', '_' |
| 26 | + if (-not $catalog[$norm]) { $catalog[$norm] = $e } |
| 27 | + } |
| 28 | + } |
| 29 | + catch { |
| 30 | + Write-Warning "Could not fetch catalog: $($_.Exception.Message)" |
| 31 | + } |
| 32 | + |
| 33 | + $reports = Get-OpenSpecConversionReport -OutputPath $OutputPath -ProtocolId $ProtocolId |
| 34 | + $results = New-Object System.Collections.Generic.List[object] |
| 35 | + |
| 36 | + foreach ($report in $reports) { |
| 37 | + try { |
| 38 | + $protocolId = $report.ProtocolId |
| 39 | + if ([string]::IsNullOrWhiteSpace($protocolId)) { continue } |
| 40 | + $mdPath = $report.MarkdownPath |
| 41 | + if ([string]::IsNullOrWhiteSpace($mdPath)) { |
| 42 | + $mdPath = Join-Path (Join-Path $OutputPath $protocolId) "$protocolId.md" |
| 43 | + } |
| 44 | + $markdown = '' |
| 45 | + if ($mdPath -and (Test-Path -LiteralPath $mdPath -PathType Leaf -ErrorAction SilentlyContinue)) { |
| 46 | + $markdown = Get-Content -LiteralPath $mdPath -Raw -ErrorAction SilentlyContinue |
| 47 | + } |
| 48 | + |
| 49 | + $mdHeadings = New-Object System.Collections.Generic.List[object] |
| 50 | + $mdAnchors = New-Object System.Collections.Generic.HashSet[string]([System.StringComparer]::OrdinalIgnoreCase) |
| 51 | + $mdHeadingRegex = [regex]::new('(?m)^(#{1,6})\s+(.+)$') |
| 52 | + foreach ($m in $mdHeadingRegex.Matches($markdown)) { |
| 53 | + $level = $m.Groups[1].Value.Length |
| 54 | + $text = $m.Groups[2].Value.Trim() |
| 55 | + [void]$mdHeadings.Add([pscustomobject]@{ Level = $level; Text = $text }) |
| 56 | + } |
| 57 | + foreach ($m in [regex]::Matches($markdown, '<a\s+id="([^"]+)"\s*>\s*</a>')) { |
| 58 | + [void]$mdAnchors.Add($m.Groups[1].Value) |
| 59 | + } |
| 60 | + |
| 61 | + $liveHeadings = New-Object System.Collections.Generic.List[object] |
| 62 | + $liveAnchors = New-Object System.Collections.Generic.HashSet[string]([System.StringComparer]::OrdinalIgnoreCase) |
| 63 | + $liveUrl = $null |
| 64 | + $fetchError = $null |
| 65 | + |
| 66 | + $entry = $catalog[$protocolId] |
| 67 | + if ($entry) { |
| 68 | + $liveUrl = $entry.SpecPageUrl |
| 69 | + try { |
| 70 | + $response = Invoke-OpenSpecRequest -Uri $liveUrl |
| 71 | + $html = $response.Content |
| 72 | + $hRegex = [regex]::new('(?is)<h([1-6])(?:\s[^>]*)?\s+id="([^"]+)"[^>]*>([^<]*)</h\1>') |
| 73 | + foreach ($m in $hRegex.Matches($html)) { |
| 74 | + $level = [int]$m.Groups[1].Value |
| 75 | + $id = $m.Groups[2].Value |
| 76 | + $text = (ConvertFrom-OpenSpecHtml -Html $m.Groups[3].Value).Trim() |
| 77 | + [void]$liveHeadings.Add([pscustomobject]@{ Level = $level; Id = $id; Text = $text }) |
| 78 | + [void]$liveAnchors.Add($id) |
| 79 | + } |
| 80 | + $altHRegex = [regex]::new('(?is)<h([1-6])[^>]*>([^<]*)</h\1>') |
| 81 | + foreach ($m in $altHRegex.Matches($html)) { |
| 82 | + $level = [int]$m.Groups[1].Value |
| 83 | + $text = (ConvertFrom-OpenSpecHtml -Html $m.Groups[2].Value).Trim() |
| 84 | + $lastText = if ($liveHeadings.Count -gt 0) { $liveHeadings[$liveHeadings.Count - 1].Text } else { $null } |
| 85 | + if ($text.Length -gt 0 -and $text -ne $lastText) { |
| 86 | + [void]$liveHeadings.Add([pscustomobject]@{ Level = $level; Id = $null; Text = $text }) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + catch { |
| 91 | + $fetchError = $_.Exception.Message |
| 92 | + } |
| 93 | + } |
| 94 | + else { |
| 95 | + $fetchError = "Protocol not found in catalog" |
| 96 | + } |
| 97 | + |
| 98 | + $missingInMd = New-Object System.Collections.Generic.List[string] |
| 99 | + $missingInLive = New-Object System.Collections.Generic.List[string] |
| 100 | + $mdAnchorList = @($mdAnchors) |
| 101 | + foreach ($aid in $mdAnchorList) { |
| 102 | + if ($aid -notmatch '^_Toc\d+$' -and $liveAnchors.Count -gt 0 -and -not $liveAnchors.Contains($aid)) { |
| 103 | + [void]$missingInLive.Add($aid) |
| 104 | + } |
| 105 | + } |
| 106 | + foreach ($aid in @($liveAnchors)) { |
| 107 | + if (-not $mdAnchors.Contains($aid)) { |
| 108 | + [void]$missingInMd.Add($aid) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + $suggestReview = $false |
| 113 | + if ($fetchError) { $suggestReview = $true } |
| 114 | + if ($missingInMd.Count -gt 5 -or $missingInLive.Count -gt 5) { $suggestReview = $true } |
| 115 | + if ($liveHeadings.Count -eq 0 -and $mdHeadings.Count -gt 0) { $suggestReview = $true } |
| 116 | + |
| 117 | + [void]$results.Add([pscustomobject]@{ |
| 118 | + PSTypeName = 'AwakeCoding.OpenSpecs.LiveHtmlCompareResult' |
| 119 | + ProtocolId = $protocolId |
| 120 | + MarkdownPath = $mdPath |
| 121 | + LiveUrl = $liveUrl |
| 122 | + FetchError = $fetchError |
| 123 | + MarkdownHeadingCount = $mdHeadings.Count |
| 124 | + MarkdownAnchorCount = $mdAnchors.Count |
| 125 | + LiveHeadingCount = $liveHeadings.Count |
| 126 | + LiveAnchorCount = $liveAnchors.Count |
| 127 | + MissingInMarkdown = @($missingInMd) |
| 128 | + MissingInLive = @($missingInLive) |
| 129 | + SuggestManualReview = $suggestReview |
| 130 | + IssueCount = $report.IssueCount |
| 131 | + }) |
| 132 | + } catch { |
| 133 | + Write-Warning "Compare failed for $($report.ProtocolId): $_" |
| 134 | + [void]$results.Add([pscustomobject]@{ |
| 135 | + PSTypeName = 'AwakeCoding.OpenSpecs.LiveHtmlCompareResult' |
| 136 | + ProtocolId = $report.ProtocolId |
| 137 | + FetchError = $_.Exception.Message |
| 138 | + SuggestManualReview = $true |
| 139 | + }) |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + $results |
| 144 | +} |
0 commit comments