-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert-mdx-to-md.ps1
More file actions
166 lines (146 loc) · 5.86 KB
/
Copy pathconvert-mdx-to-md.ps1
File metadata and controls
166 lines (146 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
$total = 0
$errors = @()
$bunDir = "C:\Users\Antonio\Documents\Obsidian\Recursos\003-Documentación\bun"
if (-not (Test-Path $bunDir)) {
$parent = Get-ChildItem -Path "C:\Users\Antonio\Documents\Obsidian\Recursos" -Filter "003-Doc*" | Select-Object -First 1
if ($parent) { $bunDir = Join-Path $parent.FullName "bun" }
else { Write-Host "Cannot find bun directory!" -ForegroundColor Red; exit 1 }
}
$files = Get-ChildItem -Path $bunDir -Recurse -Filter "*.mdx"
Write-Host "Found $($files.Count) MDX files" -ForegroundColor Yellow
function Strip-MdxComments($text) {
$text = [regex]::Replace($text, '\{/\*.*?\*/\}', '', 'Singleline')
$text = [regex]::Replace($text, '<!--.*?-->', '', 'Singleline')
return $text
}
function Strip-Frontmatter($text) {
return [regex]::Replace($text, '(?ms)^---\s*\n.*?\n---\s*\n', '')
}
function Strip-Imports($text) {
return [regex]::Replace($text, '(?m)^import\s+.+?;\s*$', '')
}
function Normalize-JsxTags($text) {
# Join multi-line JSX opening tags: <Tag\n attr="val"\n> -> <Tag attr="val">
for ($i = 0; $i -lt 15; $i++) {
$text = [regex]::Replace($text, '(?s)(<\w[\w-]*)\s*\r?\n\s*([^>]*\>)', '$1 $2')
}
return $text
}
function Fix-CodeFences($text) {
# Clean code fence headers (including indented ones inside Tab/CodeGroup)
$text = [regex]::Replace($text, '(?m)^[ \t]*(`{3,}\w*)\s+.*$', '$1')
$text = [regex]::Replace($text, '(?m)^[ \t]*(`{3,})\s+\w+.*$', '$1')
return $text
}
function Strip-CodeAnnotations($text) {
return [regex]::Replace($text, '(?m)\s*//\s*\[!code\s+.*?\]\s*$', '')
}
function Convert-Callouts($text) {
$replacements = @{
'<Tip>' = '> [!TIP]'
'</Tip>' = ''
'<Warning>' = '> [!WARNING]'
'</Warning>' = ''
'<Note>' = '> [!NOTE]'
'</Note>' = ''
'<Info>' = '> [!INFO]'
'</Info>' = ''
'<Note type="warning">' = '> [!WARNING]'
'<Note type="tip">' = '> [!TIP]'
}
foreach ($key in $replacements.Keys) {
$text = $text -replace [regex]::Escape($key), $replacements[$key]
}
return $text
}
function Strip-Wrappers($text) {
$wrappers = @('CardGroup', 'Tabs', 'CodeGroup', 'Steps')
foreach ($w in $wrappers) {
$text = [regex]::Replace($text, "<$w[^>]*>", '')
$text = [regex]::Replace($text, "</$w>", '')
}
return $text
}
function Convert-Components($text) {
# Card with title -> **title**: content
$text = [regex]::Replace($text, '<Card\s+title="([^"]*)"[^>]*>(.*?)</Card>', '**$1**: $2', 'Singleline')
# Tab -> ### title
$text = [regex]::Replace($text, '<Tab\s+title="([^"]*)"[^>]*>', '### $1')
$text = [regex]::Replace($text, '</Tab>', '')
# Step -> #### title
$text = [regex]::Replace($text, '<Step\s+title="([^"]*)"[^>]*>', '#### $1')
$text = [regex]::Replace($text, '</Step>', '')
# Accordion -> ### title
$text = [regex]::Replace($text, '<Accordion\s+title="([^"]*)"[^>]*>', '### $1')
$text = [regex]::Replace($text, '</Accordion>', '')
# Frame
$text = [regex]::Replace($text, '<Frame[^>]*>', '')
$text = [regex]::Replace($text, '</Frame>', '')
# Badge -> inline code
$text = [regex]::Replace($text, '<Badge[^>]*>(.*?)</Badge>', '`$1`')
# img with caption
$text = [regex]::Replace($text, '<img\s+[^>]*src="([^"]*)"[^>]*caption="([^"]*)"[^>]*>', '')
$text = [regex]::Replace($text, '<img\s+[^>]*caption="([^"]*)"[^>]*src="([^"]*)"[^>]*>', '')
$text = [regex]::Replace($text, '<img\s+[^>]*src="([^"]*)"[^>]*>', '')
return $text
}
function Strip-RemainingTags($text) {
# Remove self-closing <TagName ... />
$text = [regex]::Replace($text, '<\w[\w-]*(\s+[^>]*?)?\s*/>', '')
# Remove any remaining HTML-like tags
$text = [regex]::Replace($text, '</?\w[\w-]*(\s+[^>]*?)?\s*/?>', '')
return $text
}
function Cleanup-Indent($text) {
# Strip 2+ leading spaces from non-code lines (remnants of Tab nesting)
$inCode = $false
$result = @()
foreach ($line in $text -split "`n") {
if ($line -match '^[ \t]*```') { $inCode = -not $inCode }
if (-not $inCode -and $line.Length -gt 0) {
$stripped = $line -replace '^ +', ''
$result += $stripped
} else {
$result += $line
}
}
return ($result -join "`n")
}
function Cleanup-Whitespace($text) {
# Remove horizontal rules (---) left after tag removal
$text = [regex]::Replace($text, '(?m)^\s*[-]{3,}\s*$', '')
# Remove whitespace-only lines
$text = [regex]::Replace($text, '(?m)^[ \t]+$', '')
# Collapse 3+ newlines to 2
$text = [regex]::Replace($text, "`n{3,}", "`n`n")
return $text.Trim()
}
$files | ForEach-Object {
$file = $_.FullName
$total++
try {
$content = Get-Content -Path $file -Raw -ErrorAction Stop
$content = Strip-MdxComments $content
$content = Strip-Frontmatter $content
$content = Strip-Imports $content
$content = Strip-CodeAnnotations $content
$content = Normalize-JsxTags $content
$content = Fix-CodeFences $content
$content = Convert-Callouts $content
$content = Strip-Wrappers $content
$content = Convert-Components $content
$content = Strip-RemainingTags $content
$content = Cleanup-Indent $content
$content = Cleanup-Whitespace $content
$mdFile = $file -replace '\.mdx$', '.md'
[System.IO.File]::WriteAllText($mdFile, $content, [System.Text.Encoding]::UTF8)
Remove-Item -Path $file -Force
Write-Host "OK $($_.BaseName).md" -ForegroundColor Green
}
catch {
Write-Host "ERR $($_.Name): $_" -ForegroundColor Red
$errors += "$($_.Name): $_"
}
}
Write-Host "`n=== Done: $total files processed, $($errors.Count) errors ===" -ForegroundColor Yellow
if ($errors.Count -gt 0) { $errors | ForEach-Object { Write-Host " - $_" -ForegroundColor Red } }