Skip to content

Commit 3ef96ed

Browse files
mientjanclaude
andcommitted
feat(example): load markdown from .md file asset
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ec22811 commit 3ef96ed

9 files changed

Lines changed: 75 additions & 53 deletions

File tree

example/App.tsx

Lines changed: 18 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,29 @@
1+
import { useEffect, useState } from 'react';
12
import { StatusBar } from 'expo-status-bar';
2-
import { StyleSheet, ScrollView, SafeAreaView } from 'react-native';
3+
import { StyleSheet, ScrollView, SafeAreaView, Text } from 'react-native';
4+
import { Asset } from 'expo-asset';
35
import Markdown from 'react-native-markdown-renderer';
46

5-
const copy = `# React Native Markdown Renderer
6-
7-
## Features
8-
9-
This library renders **Markdown** in *React Native* using native components.
10-
11-
### Supported elements
12-
13-
- **Bold** and *italic* text
14-
- ~~Strikethrough~~
15-
- [Links](https://github.com)
16-
- Inline \`code\`
17-
18-
### Lists
19-
20-
1. First ordered item
21-
2. Second ordered item
22-
3. Third ordered item
23-
24-
- Unordered item A
25-
- Unordered item B
26-
- Unordered item C
27-
28-
### Code block
29-
30-
\`\`\`
31-
const greeting = "Hello, Markdown!";
32-
console.log(greeting);
33-
\`\`\`
34-
35-
### Blockquote
36-
37-
> Markdown is a lightweight markup language that you can use
38-
> to add formatting elements to plaintext text documents.
39-
40-
### Table
41-
42-
| Feature | Status |
43-
|---------------|--------|
44-
| Headings | Done |
45-
| Bold / Italic | Done |
46-
| Links | Done |
47-
| Images | Done |
48-
| Tables | Done |
49-
| Code blocks | Done |
50-
51-
---
52-
53-
*Rendered with react-native-markdown-renderer v4*
54-
`;
7+
/* eslint-disable @typescript-eslint/no-require-imports */
8+
const mdAsset = require('./example_test.md');
559

5610
export default function App() {
11+
const [copy, setCopy] = useState<string | null>(null);
12+
13+
useEffect(() => {
14+
(async () => {
15+
const asset = Asset.fromModule(mdAsset);
16+
await asset.downloadAsync();
17+
const uri = asset.localUri || asset.uri;
18+
const response = await fetch(uri);
19+
setCopy(await response.text());
20+
})();
21+
}, []);
22+
5723
return (
5824
<SafeAreaView style={styles.safe}>
5925
<ScrollView style={styles.scroll} contentContainerStyle={styles.content}>
60-
<Markdown>{copy}</Markdown>
26+
{copy ? <Markdown>{copy}</Markdown> : <Text>Loading...</Text>}
6127
</ScrollView>
6228
<StatusBar style="auto" />
6329
</SafeAreaView>

example/app.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
},
2626
"web": {
2727
"favicon": "./assets/favicon.png"
28-
}
28+
},
29+
"plugins": [
30+
"expo-asset"
31+
]
2932
}
3033
}

example/assets/adaptive-icon.png

17.1 KB
Loading

example/assets/favicon.png

1.43 KB
Loading

example/assets/icon.png

21.9 KB
Loading

example/assets/splash-icon.png

17.1 KB
Loading

example/example_test.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# React Native Markdown Renderer
2+
3+
## Features
4+
5+
This library renders **Markdown** in *React Native* using native components.
6+
7+
### Supported elements
8+
9+
- **Bold** and *italic* text
10+
- ~~Strikethrough~~
11+
- [Links](https://github.com)
12+
- Inline \`code\`
13+
14+
### Lists
15+
16+
1. First ordered item
17+
2. Second ordered item
18+
3. Third ordered item
19+
20+
- Unordered item A
21+
- Unordered item B
22+
- Unordered item C
23+
24+
### Code block
25+
26+
\`\`\`
27+
const greeting = "Hello, Markdown!";
28+
console.log(greeting);
29+
\`\`\`
30+
31+
### Blockquote
32+
33+
> Markdown is a lightweight markup language that you can use
34+
> to add formatting elements to plaintext text documents.
35+
36+
### Table
37+
38+
| Feature | Status |
39+
|---------------|--------|
40+
| Headings | Done |
41+
| Bold / Italic | Done |
42+
| Links | Done |
43+
| Images | Done |
44+
| Tables | Done |
45+
| Code blocks | Done |
46+
47+
---
48+
49+
*Rendered with react-native-markdown-renderer v4*

example/metro.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ config.resolver.extraNodeModules = {
2121
'markdown-it': path.resolve(libraryRoot, 'node_modules', 'markdown-it'),
2222
};
2323

24+
// Allow importing .md files as assets
25+
config.resolver.assetExts.push('md');
26+
2427
// Block the library's node_modules for packages the example already provides
2528
config.resolver.blockList = [
2629
new RegExp(

example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
},
1111
"dependencies": {
1212
"expo": "~54.0.33",
13+
"expo-asset": "~12.0.12",
1314
"expo-status-bar": "~3.0.9",
1415
"react": "19.1.0",
1516
"react-dom": "19.1.0",

0 commit comments

Comments
 (0)