Skip to content

Commit 636d839

Browse files
committed
Improve markdown conversion
1 parent f72216e commit 636d839

File tree

2 files changed

+112
-120
lines changed

2 files changed

+112
-120
lines changed

src/__tests__/process-markdown.test.ts

Lines changed: 89 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ describe('processMarkdown', () => {
115115
assert.ok(result.includes('Only content.'));
116116
});
117117

118-
test('transforms static2dynamic code fences', async () => {
118+
test('transforms static2dynamic code fences and removes only that tag', async () => {
119119
const input = dedent`
120120
Before code.
121121
@@ -148,18 +148,14 @@ describe('processMarkdown', () => {
148148

149149
assert.ok(result.includes('**Static:**'));
150150
assert.ok(result.includes('**Dynamic:**'));
151-
// The dynamic version should have NavigationContainer
152151
assert.ok(result.includes('NavigationContainer'));
153-
// Meta attributes should be stripped from fence lines
154152
assert.ok(!result.includes('static2dynamic'));
155-
// Check that 'snack' doesn't appear in fence meta (it may appear in code content)
156-
assert.ok(!result.match(/^```\w*.*snack/m));
157-
assert.ok(!result.match(/^```\w*.*name=/m));
153+
assert.ok(result.match(/^```js name="Example" snack$/m));
158154
assert.ok(result.includes('Before code.'));
159155
assert.ok(result.includes('After code.'));
160156
});
161157

162-
test('strips code fence meta attributes', async () => {
158+
test('preserves code fence meta attributes', async () => {
163159
const input = dedent`
164160
\`\`\`js name="Test" snack
165161
const x = 1;
@@ -172,16 +168,10 @@ describe('processMarkdown', () => {
172168

173169
const { content: result } = await processMarkdown(input);
174170

175-
assert.ok(!result.includes('name='));
176-
assert.ok(!result.includes('snack'));
177-
assert.ok(!result.includes('npm2yarn'));
178-
assert.ok(result.includes('```js'));
179-
assert.ok(result.includes('```bash'));
180-
assert.ok(result.includes('const x = 1;'));
181-
assert.ok(result.includes('npm install something'));
171+
assert.strictEqual(result, input);
182172
});
183173

184-
test('converts admonitions to blockquotes', async () => {
174+
test('preserves admonitions', async () => {
185175
const input = dedent`
186176
Some text.
187177
@@ -196,14 +186,10 @@ describe('processMarkdown', () => {
196186

197187
const { content: result } = await processMarkdown(input);
198188

199-
assert.ok(!result.includes(':::'));
200-
assert.ok(result.includes('> **Warning:**'));
201-
assert.ok(result.includes('> This is a warning.'));
202-
assert.ok(result.includes('Some text.'));
203-
assert.ok(result.includes('More text.'));
189+
assert.strictEqual(result, input);
204190
});
205191

206-
test('converts info admonitions', async () => {
192+
test('preserves info admonitions', async () => {
207193
const input = dedent`
208194
:::info
209195
@@ -214,16 +200,15 @@ describe('processMarkdown', () => {
214200

215201
const { content: result } = await processMarkdown(input);
216202

217-
assert.ok(result.includes('> **Info:**'));
218-
assert.ok(result.includes('> Some info here.'));
203+
assert.strictEqual(result, input);
219204
});
220205

221-
test('cleans up extra blank lines', async () => {
206+
test('cleans up extra blank lines in plain markdown', async () => {
222207
const input = 'Line 1.\n\n\n\n\nLine 2.';
223208
const { content: result } = await processMarkdown(input);
224209

225210
assert.ok(!result.includes('\n\n\n'));
226-
assert.ok(result.includes('Line 1.\n\nLine 2.'));
211+
assert.strictEqual(result, 'Line 1.\n\nLine 2.');
227212
});
228213

229214
test('handles Tabs with code blocks inside', async () => {
@@ -382,7 +367,7 @@ describe('processMarkdown', () => {
382367
assert.ok(result.includes('/assets/fundamentals/navigate.mp4'));
383368
});
384369

385-
test('strips nested decorative divs', async () => {
370+
test('strips nested divs', async () => {
386371
const input = dedent`
387372
<div className="outer">
388373
<div className="inner">
@@ -451,6 +436,84 @@ describe('processMarkdown', () => {
451436
assert.ok(result.includes('</div>'));
452437
});
453438

439+
test('preserves import statements inside code fences', async () => {
440+
const input = dedent`
441+
\`\`\`js
442+
import { AppRegistry } from 'react-native-web';
443+
import ReactDOMServer from 'react-dom/server';
444+
import App from './src/App';
445+
446+
const html = ReactDOMServer.renderToString(App);
447+
\`\`\`
448+
`;
449+
450+
const { content: result } = await processMarkdown(input);
451+
452+
assert.strictEqual(result, input);
453+
});
454+
455+
test('preserves admonitions inside code fences', async () => {
456+
const input = dedent`
457+
\`\`\`md
458+
:::warning
459+
Keep this as markdown source.
460+
:::
461+
\`\`\`
462+
`;
463+
464+
const { content: result } = await processMarkdown(input);
465+
466+
assert.strictEqual(result, input);
467+
});
468+
469+
test('preserves Tabs markup inside code fences', async () => {
470+
const input = dedent`
471+
\`\`\`mdx
472+
<Tabs>
473+
<TabItem value="static" label="Static">
474+
475+
Static content.
476+
477+
</TabItem>
478+
</Tabs>
479+
\`\`\`
480+
`;
481+
482+
const { content: result } = await processMarkdown(input);
483+
484+
assert.strictEqual(result, input);
485+
});
486+
487+
test('preserves tilde-fenced markdown source', async () => {
488+
const input = dedent`
489+
~~~md
490+
:::warning
491+
Keep this as markdown source.
492+
:::
493+
~~~
494+
`;
495+
496+
const { content: result } = await processMarkdown(input);
497+
498+
assert.strictEqual(result, input);
499+
});
500+
501+
test('strips generic divs and keeps their content', async () => {
502+
const input = dedent`
503+
<div id="callout">
504+
505+
**Important** raw HTML wrapper.
506+
507+
</div>
508+
`;
509+
510+
const { content: result } = await processMarkdown(input);
511+
512+
assert.ok(!result.includes('<div'));
513+
assert.ok(!result.includes('</div>'));
514+
assert.ok(result.includes('**Important** raw HTML wrapper.'));
515+
});
516+
454517
test('strips frontmatter and returns parsed data', async () => {
455518
const input = dedent`
456519
---

0 commit comments

Comments
 (0)