-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathprocess-markdown.test.ts
More file actions
296 lines (222 loc) · 7.75 KB
/
process-markdown.test.ts
File metadata and controls
296 lines (222 loc) · 7.75 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
import dedent from 'dedent';
import assert from 'node:assert';
import { describe, test } from 'node:test';
import { processMarkdown } from '../plugins/process-markdown.ts';
describe('processMarkdown', () => {
test('strips MDX import statements', async () => {
const input = dedent`
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
Some content here.
`;
const { content: result } = await processMarkdown(input);
assert.ok(!result.includes('import'));
assert.ok(result.includes('Some content here.'));
});
test('transforms Tabs with two TabItems into labeled sections', async () => {
const input = dedent`
Before tabs.
<Tabs groupId="config" queryString="config">
<TabItem value="static" label="Static" default>
Static content here.
</TabItem>
<TabItem value="dynamic" label="Dynamic">
Dynamic content here.
</TabItem>
</Tabs>
After tabs.
`;
const { content: result } = await processMarkdown(input);
assert.ok(!result.includes('<Tabs'));
assert.ok(!result.includes('<TabItem'));
assert.ok(!result.includes('</Tabs>'));
assert.ok(!result.includes('</TabItem>'));
assert.ok(result.includes('**Static:**'));
assert.ok(result.includes('Static content here.'));
assert.ok(result.includes('**Dynamic:**'));
assert.ok(result.includes('Dynamic content here.'));
assert.ok(result.includes('Before tabs.'));
assert.ok(result.includes('After tabs.'));
});
test('transforms nested Tabs', async () => {
const input = dedent`
<Tabs groupId='framework' queryString="framework">
<TabItem value='expo' label='Expo' default>
Expo instructions.
</TabItem>
<TabItem value='community-cli' label='Community CLI'>
CLI instructions.
<Tabs>
<TabItem value='kotlin' label='Kotlin' default>
Kotlin code.
</TabItem>
<TabItem value='java' label='Java'>
Java code.
</TabItem>
</Tabs>
More CLI content.
</TabItem>
</Tabs>
`;
const { content: result } = await processMarkdown(input);
assert.ok(!result.includes('<Tabs'));
assert.ok(!result.includes('<TabItem'));
assert.ok(result.includes('**Expo:**'));
assert.ok(result.includes('Expo instructions.'));
assert.ok(result.includes('**Community CLI:**'));
assert.ok(result.includes('CLI instructions.'));
assert.ok(result.includes('**Kotlin:**'));
assert.ok(result.includes('Kotlin code.'));
assert.ok(result.includes('**Java:**'));
assert.ok(result.includes('Java code.'));
});
test('handles single TabItem without label', async () => {
const input = dedent`
<Tabs>
<TabItem value="only" label="Only Option">
Only content.
</TabItem>
</Tabs>
`;
const { content: result } = await processMarkdown(input);
assert.ok(!result.includes('<Tabs'));
// Single tab should not have a label
assert.ok(!result.includes('**Only Option:**'));
assert.ok(result.includes('Only content.'));
});
test('transforms static2dynamic code fences', async () => {
const input = dedent`
Before code.
\`\`\`js name="Example" snack static2dynamic
import * as React from 'react';
import { createStaticNavigation } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
function HomeScreen() {
return null;
}
const MyStack = createNativeStackNavigator({
screens: {
Home: HomeScreen,
},
});
const Navigation = createStaticNavigation(MyStack);
export default function App() {
return <Navigation />;
}
\`\`\`
After code.
`;
const { content: result } = await processMarkdown(input);
assert.ok(result.includes('**Static:**'));
assert.ok(result.includes('**Dynamic:**'));
// The dynamic version should have NavigationContainer
assert.ok(result.includes('NavigationContainer'));
// Meta attributes should be stripped from fence lines
assert.ok(!result.includes('static2dynamic'));
// Check that 'snack' doesn't appear in fence meta (it may appear in code content)
assert.ok(!result.match(/^```\w*.*snack/m));
assert.ok(!result.match(/^```\w*.*name=/m));
assert.ok(result.includes('Before code.'));
assert.ok(result.includes('After code.'));
});
test('strips code fence meta attributes', async () => {
const input = dedent`
\`\`\`js name="Test" snack
const x = 1;
\`\`\`
\`\`\`bash npm2yarn
npm install something
\`\`\`
`;
const { content: result } = await processMarkdown(input);
assert.ok(!result.includes('name='));
assert.ok(!result.includes('snack'));
assert.ok(!result.includes('npm2yarn'));
assert.ok(result.includes('```js'));
assert.ok(result.includes('```bash'));
assert.ok(result.includes('const x = 1;'));
assert.ok(result.includes('npm install something'));
});
test('converts admonitions to blockquotes', async () => {
const input = dedent`
Some text.
:::warning
This is a warning.
::::
More text.
`;
const { content: result } = await processMarkdown(input);
assert.ok(!result.includes(':::'));
assert.ok(result.includes('> **Warning:**'));
assert.ok(result.includes('> This is a warning.'));
assert.ok(result.includes('Some text.'));
assert.ok(result.includes('More text.'));
});
test('converts info admonitions', async () => {
const input = dedent`
:::info
Some info here.
::::
`;
const { content: result } = await processMarkdown(input);
assert.ok(result.includes('> **Info:**'));
assert.ok(result.includes('> Some info here.'));
});
test('cleans up extra blank lines', async () => {
const input = 'Line 1.\n\n\n\n\nLine 2.';
const { content: result } = await processMarkdown(input);
assert.ok(!result.includes('\n\n\n'));
assert.ok(result.includes('Line 1.\n\nLine 2.'));
});
test('handles Tabs with code blocks inside', async () => {
const input = dedent`
<Tabs groupId="config" queryString="config">
<TabItem value="static" label="Static" default>
\`\`\`js
const x = createStackNavigator({});
\`\`\`
</TabItem>
<TabItem value="dynamic" label="Dynamic">
\`\`\`js
function MyStack() {
return <Stack.Navigator />;
}
\`\`\`
</TabItem>
</Tabs>
`;
const { content: result } = await processMarkdown(input);
assert.ok(result.includes('**Static:**'));
assert.ok(result.includes('createStackNavigator'));
assert.ok(result.includes('**Dynamic:**'));
assert.ok(result.includes('Stack.Navigator'));
assert.ok(!result.includes('<Tabs'));
});
test('preserves content outside of MDX constructs', async () => {
const input = dedent`
# Title
Regular paragraph with **bold** and [links](https://example.com).
- List item 1
- List item 2
> A blockquote.
`;
const { content: result } = await processMarkdown(input);
assert.strictEqual(result, input);
});
test('strips frontmatter and returns parsed data', async () => {
const input = dedent`
---
id: getting-started
title: Getting started
description: Learn how to get started
---
Some content here.
`;
const { frontmatter, content } = await processMarkdown(input);
assert.strictEqual(frontmatter.title, 'Getting started');
assert.strictEqual(frontmatter.description, 'Learn how to get started');
assert.strictEqual(frontmatter.id, 'getting-started');
assert.ok(!content.includes('---'));
assert.ok(content.includes('Some content here.'));
});
});