You can add extra syntax support by integrating markdown-it compatible plugins. This is a two-step process:
1. Integrate the Plugin
Create a MarkdownIt instance using the MarkdownIt function provided by the library. Use .use(plugin, options) to attach your plugin. Pass this instance to the markdownit prop of the Markdown component.
Tip: Use the debugPrintTree prop on the Markdown component to see the rendered tree in your console. This helps identify the name of the new rule (e.g., video) that the plugin introduced.
2. Implement Render Rules and Styles
Once you know the rule name (e.g., video), provide a corresponding function in the rules prop of the Markdown component. This function receives (node, children, parent, styles) and should return a React component. You can also define custom styles for this new rule in the style prop.
Note: The node object contains all necessary metadata (like sourceInfo, type, attributes) required to render the custom component correctly.
import React from 'react';
import { SafeAreaView, ScrollView, Text } from 'react-native';
import Markdown, { MarkdownIt } from 'react-native-markdown-display';
import blockEmbedPlugin from 'markdown-it-block-embed';
// 1. Setup the markdown-it instance with the plugin
const markdownItInstance =
MarkdownIt({typographer: true})
.use(blockEmbedPlugin, {
containerClassName: "video-embed"
});
const copy = `
# Some header
@[youtube](lJIrF4YjHfQ)
`;
const App = () => {
return (
<SafeAreaView>
<ScrollView>
<Markdown
markdownit={markdownItInstance}
style={{
video: {
color: 'red',
}
}}
rules={{
// 2. Define the render rule for the new 'video' type
video: (node, children, parent, styles) => {
// node contains metadata like sourceInfo.videoID
return (
<Text key={node.key} style={styles.video}>
Return a video component instead of this text component!
</Text>
);
}
}}
>
{copy}
</Markdown>
</ScrollView>
</SafeAreaView>
);
};
export default App;