In the world of web development, Markdown has become a ubiquitous language for creating content. Its simplicity and readability make it a favorite for everything from documentation to blog posts. However, sometimes static Markdown isn't enough. You might want to embed dynamic content like videos, social media posts, or interactive elements directly within your Markdown.
Enter remark-react-liquid-tag
, a clever little plugin that bridges the gap between static Markdown and dynamic React components. Inspired by the popular dev.to
platform, this plugin allows you to leverage the power of "liquid tags" within your Markdown, bringing a new level of interactivity to your content.
The Inspiration: Liquid Tags on dev.to
If you've ever written or read an article on dev.to
, you might have noticed the neat way they embed content from various platforms. They achieve this using liquid tags – special elements enclosed in {% %}
syntax. These tags act as placeholders that are later replaced with dynamic content.
remark-react-liquid-tag
brings this same concept to your React-based Markdown rendering pipeline. It allows you to define custom tags that, when processed, will render specific React components, effectively embedding dynamic functionality within your static Markdown.
How it Works: Bridging Markdown and React
This plugin is built as an extension for remark, a powerful Markdown processor. When used in conjunction with react-remark (a library for rendering Markdown as React components), remark-react-liquid-tag
parses your Markdown for these special liquid tags.
Upon encountering a tag, the plugin extracts the tag's type, any associated URL, options, and configuration. This information is then passed as props to a designated React component, which is responsible for rendering the dynamic content.
Getting Started: Installation and Basic Usage
Integrating remark-react-liquid-tag
into your project is straightforward. If you're already using react-remark
, the setup is particularly smooth.
First, install the plugin using npm:
npm install remark-react-liquid-tag
Next, in your React component where you're using react-remark
, you'll need to include the plugin in your remarkPlugins
array. You'll also need to provide a React component that will handle the rendering of your liquid tags.
Here's a basic example based on the README:
import React, { Fragment } from 'react';
import { useRemark } from 'react-remark';
import remarkGemoji from 'remark-gemoji';
import rehypeSlug from 'rehype-slug';
import rehypeAutoLinkHeadings from 'rehype-autolink-headings';
import reactLiquidTag, { RemarkReactLiquidTagProps } from 'remark-react-liquid-tag';
const LiquidTag: React.FC<RemarkReactLiquidTagProps> = (props) => {
switch (props.type) {
case 'youtube':
return (
<iframe
width="560"
height="315"
src={`https://www.youtube.com/embed/${props.url}`}
frameBorder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowFullScreen
/>
);
default:
return <Fragment />;
}
}
// ...
<Remark
remarkPlugins={[remarkGemoji, [reactLiquidTag, { component: LiquidTag }]]}
remarkToRehypeOptions={{ allowDangerousHtml: true }}
rehypePlugins={[rehypeSlug, rehypeAutoLinkHeadings]}
>
{`
This is youtube video:
{% youtube dQw4w9WgXcQ %}
This is a unsupported tag:
{% unsupported_tag %}
`}
</Remark>;
// ...
Customizing Your Liquid Tags
The beauty of remark-react-liquid-tag
lies in its flexibility. You can define your own custom liquid tags and the corresponding React components that will render them.
The plugin offers an options
object where you can configure its behavior:
-
component
: This option lets you specify the React component that will handle the rendering of all your liquid tags. This component receives props liketype
,url
,options
(for additional parameters within the tag), andconfig
(for type-specific configurations). -
config
: This option allows you to provide specific configurations for different types of liquid tags. This can be useful for passing in API keys or other settings.
For instance, you could define a liquid tag for embedding tweets:
Check out this tweet: {% tweet 1234567890 %}
And then, within your custom component, you would handle the tweet
type by fetching and rendering the tweet using a Twitter embedding library.
Benefits and Use Cases
remark-react-liquid-tag
offers several advantages:
- Enhanced Markdown Functionality: It extends the capabilities of Markdown, allowing you to embed dynamic content without resorting to raw HTML.
- Clean Separation of Concerns: Your Markdown content remains focused on the text, while the rendering logic for dynamic elements resides in your React components.
- Reusability: The React components you create for rendering liquid tags can be reused across your application.
-
Inspired by a Proven Solution: The concept is based on the successful implementation of liquid tags on
dev.to
, suggesting its effectiveness and user-friendliness.
This plugin is particularly useful for:
- Blog platforms: Embedding videos, social media posts, code sandboxes, and other interactive elements.
- Documentation sites: Integrating live demos, interactive tutorials, and API explorers.
- Content management systems: Providing content creators with an easy way to embed rich media without needing to write complex code.
Conclusion
remark-react-liquid-tag
is a powerful and elegant solution for bringing dynamic content to your Markdown-driven React applications. By embracing the concept of liquid tags, it offers a clean, intuitive, and extensible way to embed interactive elements and enhance the richness of your content. If you're looking to level up your Markdown and provide a more engaging experience for your users, this plugin is definitely worth exploring.
alfredosalzillo
/
remark-react-liquid-tag
A remark plugin that allows the usage of liquid tags.
remark-react-liquid-tag
This is a remark plugin that allows the usage of liquid tags.
This idea came from the usage of liquid tags in dev.to (DEV COMMUNITY) platform for embedding services in markdowns This documented page shows their idea behind liquid tags and the tags available.
Liquid tags
Liquid tags are special elements to use in markdown.
They are custom embeds that are added via a {% %}
syntax.
Liquid is a templating language developed by Shopify.
Installation
npm install remark-react-liquid-tag
Usage
Usage with react-remark
import React, { Fragment } from 'react';
import { useRemark } from 'react-remark';
import remarkGemoji from 'remark-gemoji';
import rehypeSlug from 'rehype-slug';
import rehypeAutoLinkHeadings from 'rehype-autolink-headings';
import reactLiquidTag, { RemarkReactLiquidTagProps } from 'remark-react-liquid-tag';
const LiquidTag: React.FC<RemarkReactLiquidTagProps> = (props) => {
switch (props.type) {
case 'youtube':
return
…
Top comments (0)