41
loading...
This website collects cookies to deliver better user experience
gatsby-transformer-remark
plugin (which is responsible for converting markdown to HTML) can render it? Bonus: How do I make sure that the iframe of the embedded videos is responsive and looks good on mobile devices?gatsby-remark-embed-video
plugin. This allows me to represent a YouTube video in markdown as a simple code block:`youtube: https://www.youtube.com/watch?v=dQw4w9WgXcQ`
gatsby-config.js
:yarn add gatsby-remark-embed-video
gatsby-remark-responsive-iframe
plugin and place it after the gatsby-remark-embed-video
one, as recommended by the plugin’s author:module.exports = {
plugins: [
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [
`gatsby-remark-embed-video`,
`gatsby-remark-images`,
`gatsby-remark-responsive-iframe`,
],
},
},
],
}
--------
title: "Here is a blog post with a YouTube video"
date: "2020-06-03"
--------
## This is my fav song of all times
Don't cha think?
`youtube: https://www.youtube.com/watch?v=dQw4w9WgXcQ`
registerEditorComponent
method. And hey, what luck, there is an actual example of registering an editor component for adding YouTube videos! Today is a good day.gatsby-plugin-netlify-cms
plugin in my gatsby-config.js
like so:module.exports = {
plugins: [`gatsby-plugin-netlify-cms`],
}
modulePath
option which lets me specify a path to a file where I can import the CMS
object. Sweet!module.exports = {
plugins: [
{
resolve: `gatsby-plugin-netlify-cms`,
options: {
modulePath: `${__dirname}/src/cms.js`,
},
},
],
}
cms.js
file in the /src
directory (feel free to place it wherever you want as long as you link it properly in the gatsby-config.js
file):import CMS from "netlify-cms-app"
CMS.registerEditorComponent({
id: "youtube",
label: "YouTube",
fields: [
{
name: "url",
label: "Youtube video URL",
widget: "string",
},
],
pattern: /^`youtube:\s(.*)`$/,
fromBlock: function (match) {
return {
url: match[1],
}
},
toBlock: function (obj) {
return "`youtube: " + obj.url + "`"
},
toPreview: function (obj) {
return obj.url
},
})
pattern
attribute defines the regular expression that will look for the line in the markdown content that starts with the string "`youtube: " and catch anything that comes after it, which in our case is the link to the video.fromBlock
attribute defines a function that is responsible to initialize the value of the rendered widget. In other words, it takes the link of the video from the pattern
and adds it in the input field of the YouTube widget.toBlock
function defines the representation of the YouTube video in the markdown content. To comply with the gatsby-remark-embed-video
Gatsby plugin, this needs to be a line that starts with a backtick, followed by the word "youtube: ", followed by the video link, and ends with another backtick.gatsby-remark-embed-video
plugin supports multiple video hosting services like Vimeo and VideoPress, not just YouTube (it also says it supports Twitch and Twitch live, but I couldn’t manage to make it work). Simply replace the instances of youtube
with video
in the cms.js
file to support them!41