-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_escape_mdx_braces.py
More file actions
389 lines (313 loc) · 14 KB
/
test_escape_mdx_braces.py
File metadata and controls
389 lines (313 loc) · 14 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#!/usr/bin/env python3
"""
Thorough tests for _escape_mdx_braces().
Tests cover: basic escaping, code block preservation, inline code preservation,
already-escaped braces, template literals, double braces, JSX style attributes,
real-world metadata content, and edge cases.
"""
import re
import sys
# --- Copy of the function under test (must match ingest.py exactly) ---
def _escape_mdx_braces(body):
"""
Escape bare { outside of fenced code blocks and inline code for MDX 3.
MDX interprets {word} as a JSX expression, which breaks when the content
is plain text from metadata (e.g. metric names like zabbix.{context}).
This function:
- Preserves fenced code blocks (```...```) and inline code (`...`)
- Escapes every bare { that isn't already escaped
- Restores style={{ which is valid JSX
"""
preserved = []
def _save(match):
preserved.append(match.group(0))
return f"\x00MDXBRACE{len(preserved) - 1}\x00"
# Preserve fenced code blocks — must come before inline code
body = re.sub(r"```.*?```", _save, body, flags=re.DOTALL)
# Preserve inline code
body = re.sub(r"`[^`\n]+`", _save, body)
# Preserve MDX import/export statements (ESM syntax uses { for destructuring)
body = re.sub(r"^import\s+.*$", _save, body, flags=re.MULTILINE)
body = re.sub(r"^export\s+(?:default|function|const|let|var|\{).*$", _save, body, flags=re.MULTILINE)
# Escape every bare { not already preceded by a backslash
body = re.sub(r"(?<!\\)\{", r"\\{", body)
# Restore style={{ which is valid JSX (the above turns it into style=\{\{)
body = body.replace("style=\\{\\{", "style={{")
# Restore preserved code sections
for i, original in enumerate(preserved):
body = body.replace(f"\x00MDXBRACE{i}\x00", original)
return body
# --- End of function copy ---
passed = 0
failed = 0
def check(name, input_text, expected):
global passed, failed
result = _escape_mdx_braces(input_text)
if result == expected:
passed += 1
print(f" PASS: {name}")
else:
failed += 1
print(f" FAIL: {name}")
print(f" input: {input_text!r}")
print(f" expected: {expected!r}")
print(f" got: {result!r}")
# ---------------------------------------------------------------------------
# 1. Basic single-brace escaping
# ---------------------------------------------------------------------------
print("=== Basic escaping ===")
check("simple {word}", "hello {world}", r"hello \{world}")
check("metric name {context}", "zabbix.{context}", r"zabbix.\{context}")
check("metric name {script}.{label}", "nagios.{script}.{label}", r"nagios.\{script}.\{label}")
check("{attribute_name} (was hardcoded)", "{attribute_name}", r"\{attribute_name}")
check("{attribute_unit} (was hardcoded)", "{attribute_unit}", r"\{attribute_unit}")
check("{HOST.IP}", "{HOST.IP}", r"\{HOST.IP}")
check("{HOST.NAME}", "{HOST.NAME}", r"\{HOST.NAME}")
check("{#MACRO}", "{#MACRO}", r"\{#MACRO}")
check("{dimension}", '"{dimension}"', r'"\{dimension}"')
check("multiple on one line", "| {a} | {b} |", r"| \{a} | \{b} |")
check("brace at end", "text{", r"text\{")
check("brace at start", "{start} of line", r"\{start} of line")
check("empty braces", "{}", r"\{}")
check("no braces", "plain text here", "plain text here")
# ---------------------------------------------------------------------------
# 2. Template literal ${...}
# ---------------------------------------------------------------------------
print("\n=== Template literals ===")
check("${expr}", "${foo}", r"$\{foo}")
check("${expr} in text", "value is ${count}", r"value is $\{count}")
# ---------------------------------------------------------------------------
# 3. Double braces {{...}}
# ---------------------------------------------------------------------------
print("\n=== Double braces ===")
check("{{double}}", "{{word}}", r"\{\{word}}")
check("{{double}} in text", "use {{var}} here", r"use \{\{var}} here")
# ---------------------------------------------------------------------------
# 4. JSX style={{ }} preservation
# ---------------------------------------------------------------------------
print("\n=== style={{}} JSX ===")
check("style={{...}}", "style={{width: '90%'}}", "style={{width: '90%'}}")
check("style={{...}} in tag",
'<img style={{width: "90%", maxHeight: "100%"}} />',
'<img style={{width: "90%", maxHeight: "100%"}} />')
# ---------------------------------------------------------------------------
# 5. Already escaped braces
# ---------------------------------------------------------------------------
print("\n=== Already escaped ===")
check("\\{already}", r"\{already}", r"\{already}")
check("\\{\\{double}}", r"\{\{double}}", r"\{\{double}}")
check("mixed escaped and bare", r"\{ok} and {bare}", r"\{ok} and \{bare}")
# ---------------------------------------------------------------------------
# 6. Fenced code block preservation
# ---------------------------------------------------------------------------
print("\n=== Fenced code blocks ===")
check("code block untouched",
"text {a}\n```\n{inside_code}\n```\ntext {b}",
"text \\{a}\n```\n{inside_code}\n```\ntext \\{b}")
check("code block with language",
"```yaml\ntarget: \"{HOST.IP}\"\n```",
"```yaml\ntarget: \"{HOST.IP}\"\n```")
check("code block with braces around it",
"{before}\n```\n{inside}\n```\n{after}",
"\\{before}\n```\n{inside}\n```\n\\{after}")
check("multiple code blocks",
"{a}\n```\n{x}\n```\n{b}\n```\n{y}\n```\n{c}",
"\\{a}\n```\n{x}\n```\n\\{b}\n```\n{y}\n```\n\\{c}")
# ---------------------------------------------------------------------------
# 7. Inline code preservation
# ---------------------------------------------------------------------------
print("\n=== Inline code ===")
check("inline code untouched",
"use `{HOST.IP}` macro",
"use `{HOST.IP}` macro")
check("inline code with surrounding braces",
"{bare} and `{safe}` and {bare2}",
"\\{bare} and `{safe}` and \\{bare2}")
check("multiple inline codes",
"`{a}` then {b} then `{c}`",
"`{a}` then \\{b} then `{c}`")
# ---------------------------------------------------------------------------
# 8. Table rows (real integration docs pattern)
# ---------------------------------------------------------------------------
print("\n=== Table rows ===")
check("metric table row",
"| smartctl.device_smart_attr_{attribute_name} | {attribute_name} | {attribute_unit} |",
r"| smartctl.device_smart_attr_\{attribute_name} | \{attribute_name} | \{attribute_unit} |")
check("metric table with backtick col",
"| `{HOST.IP}` | {HOST.CONN} | address |",
"| `{HOST.IP}` | \\{HOST.CONN} | address |")
check("zabbix metric row",
"| zabbix.{context} | User-defined metric |",
r"| zabbix.\{context} | User-defined metric |")
check("nagios metric row",
"| nagios.{script}.{label} | Performance data |",
r"| nagios.\{script}.\{label} | Performance data |")
# ---------------------------------------------------------------------------
# 9. Real-world zabbix metadata content
# ---------------------------------------------------------------------------
print("\n=== Real-world zabbix content ===")
zabbix_desc = """### Virtual Node Label Conventions
| Label key | Zabbix macro | Description |
|-----------|-------------|-------------|
| `_address` | `{HOST.IP}`, `{HOST.CONN}` | IP address or DNS name |
The `{HOST.NAME}`, `{HOST.HOST}`, and `{HOST.DNS}` macros are derived from the vnode hostname."""
zabbix_expected = """### Virtual Node Label Conventions
| Label key | Zabbix macro | Description |
|-----------|-------------|-------------|
| `_address` | `{HOST.IP}`, `{HOST.CONN}` | IP address or DNS name |
The `{HOST.NAME}`, `{HOST.HOST}`, and `{HOST.DNS}` macros are derived from the vnode hostname."""
check("zabbix description (all braces in backticks)", zabbix_desc, zabbix_expected)
zabbix_metrics = """| zabbix.{context} | {dimension} | varies |
| zabbix.{job}.state | ok, collect_failure | state |"""
zabbix_metrics_expected = r"""| zabbix.\{context} | \{dimension} | varies |
| zabbix.\{job}.state | ok, collect_failure | state |"""
check("zabbix metric rows", zabbix_metrics, zabbix_metrics_expected)
# ---------------------------------------------------------------------------
# 10. Real-world nagios metadata content
# ---------------------------------------------------------------------------
print("\n=== Real-world nagios content ===")
nagios_metrics = "| nagios.{script}.{label} | Performance data metric |"
nagios_expected = r"| nagios.\{script}.\{label} | Performance data metric |"
check("nagios metric row", nagios_metrics, nagios_expected)
# ---------------------------------------------------------------------------
# 11. Config example in code block (should NOT be escaped)
# ---------------------------------------------------------------------------
print("\n=== Config in code block ===")
config_block = '''Before config {bare}
```yaml
jobs:
- name: disk_usage
collection:
snmp:
target: "{HOST.IP}"
dependent_pipelines:
- context: zabbix.disk.used
```
After config {bare2}'''
config_expected = '''Before config \\{bare}
```yaml
jobs:
- name: disk_usage
collection:
snmp:
target: "{HOST.IP}"
dependent_pipelines:
- context: zabbix.disk.used
```
After config \\{bare2}'''
check("config in code block preserved", config_block, config_expected)
# ---------------------------------------------------------------------------
# 12. MDX import/export preservation
# ---------------------------------------------------------------------------
print("\n=== MDX import/export ===")
check("import with destructuring",
"import { OneLineInstall } from '@site/src/components/OneLineInstall/'",
"import { OneLineInstall } from '@site/src/components/OneLineInstall/'")
check("import multiple destructured",
"import { Install, InstallBox } from '@site/src/components/Install/'",
"import { Install, InstallBox } from '@site/src/components/Install/'")
check("import Grid Box",
"import { Grid, Box } from '@site/src/components/Grid_integrations';",
"import { Grid, Box } from '@site/src/components/Grid_integrations';")
check("import default (no braces)",
"import Tabs from '@theme/Tabs';",
"import Tabs from '@theme/Tabs';")
check("import in document with braces",
"import { X } from 'Y'\n\nSome text with {bare} braces.",
"import { X } from 'Y'\n\nSome text with \\{bare} braces.")
check("export default function",
"export default function MyComponent() {\n return <div />\n}",
"export default function MyComponent() {\n return <div />\n}")
check("export const",
"export const foo = {bar: 1}",
"export const foo = {bar: 1}")
check("bash export NOT preserved",
"export NETDATA_ALARM_NOTIFY_DEBUG=1",
"export NETDATA_ALARM_NOTIFY_DEBUG=1")
# ---------------------------------------------------------------------------
# 13a. Edge cases
# ---------------------------------------------------------------------------
print("\n=== Edge cases ===")
check("empty string", "", "")
check("only braces", "{}", r"\{}")
check("nested braces", "{outer{inner}}", r"\{outer\{inner}}")
check("brace with no close", "open { text", r"open \{ text")
check("close brace only", "text } here", "text } here")
check("newline after brace", "{\nfoo}", "\\{\nfoo}")
# ---------------------------------------------------------------------------
# 13. Regression: verify the old hardcoded patterns still work
# ---------------------------------------------------------------------------
print("\n=== Regression: old hardcoded patterns ===")
check("smartctl full line",
"| smartctl.device_smart_attr_{attribute_name} | {attribute_name} | {attribute_unit} |\n"
"| smartctl.device_smart_attr_{attribute_name}_normalized | {attribute_name} | value |",
"| smartctl.device_smart_attr_\\{attribute_name} | \\{attribute_name} | \\{attribute_unit} |\n"
"| smartctl.device_smart_attr_\\{attribute_name}_normalized | \\{attribute_name} | value |")
# ---------------------------------------------------------------------------
# 14. Full document simulation
# ---------------------------------------------------------------------------
print("\n=== Full document simulation ===")
full_doc = """---
sidebar_label: "Zabbix Preprocessing"
---
# Zabbix Preprocessing
## Overview
Zabbix macros (`{HOST.NAME}`, `{HOST.IP}`) are expanded before execution.
## Metrics
| Metric | Description | Unit |
|--------|-------------|------|
| zabbix.{context} | User-defined metric | varies |
| zabbix.{job}.state | Job state | state |
## Setup
```yaml
jobs:
- name: api_latency
collection:
type: command
command: /usr/local/bin/get_api_stats.sh
dependent_pipelines:
- context: myapp.api.latency
dimension: p99
steps:
- type: jsonpath
params: "$.latency.p99"
```
The `{HOST.IP}` macro resolves to the host address."""
full_doc_expected = """---
sidebar_label: "Zabbix Preprocessing"
---
# Zabbix Preprocessing
## Overview
Zabbix macros (`{HOST.NAME}`, `{HOST.IP}`) are expanded before execution.
## Metrics
| Metric | Description | Unit |
|--------|-------------|------|
| zabbix.\\{context} | User-defined metric | varies |
| zabbix.\\{job}.state | Job state | state |
## Setup
```yaml
jobs:
- name: api_latency
collection:
type: command
command: /usr/local/bin/get_api_stats.sh
dependent_pipelines:
- context: myapp.api.latency
dimension: p99
steps:
- type: jsonpath
params: "$.latency.p99"
```
The `{HOST.IP}` macro resolves to the host address."""
check("full zabbix document", full_doc, full_doc_expected)
# ---------------------------------------------------------------------------
# Summary
# ---------------------------------------------------------------------------
print(f"\n{'='*60}")
print(f"Results: {passed} passed, {failed} failed, {passed + failed} total")
if failed:
print("SOME TESTS FAILED!")
sys.exit(1)
else:
print("ALL TESTS PASSED!")
sys.exit(0)