Serving Docusaurus images with Cloudinary

This page summarizes the projects mentioned and recommended in the original post on dev.to

Our great sponsors
  • SurveyJS - Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • WorkOS - The modern identity platform for B2B SaaS
  • blog.johnnyreilly.com

    This is the source code for https://johnnyreilly.com

  • //@ts-check const visit = require('unist-util-visit'); /** * Create a remark plugin that will replace image URLs with Cloudinary URLs * @param {*} options cloudName your Cloudinary’s cloud name eg demo, baseUrl the base URL of your website eg https://blog.johnnyreilly.com - should not include a trailing slash, will likely be the same as the config.url in your docusaurus.config.js * @returns remark plugin that will replace image URLs with Cloudinary URLs */ function imageCloudinaryRemarkPluginFactory( /** @type {{ cloudName: string; baseUrl: string }} */ options ) { const { cloudName, baseUrl } = options; const srcRegex = / src={(.*)}/; /** @type {import('unified').Plugin<[], import('hast').Root>} */ return function imageCloudinaryRemarkPlugin() { return (tree) => { visit(tree, ['element', 'jsx'], (node) => { if (node.type === 'element' && node['tagName'] === 'img') { // handles nodes like this: // { // type: 'element', // tagName: 'img', // properties: { // src: 'https://some.website.com/cat.gif', // alt: null // }, // ... // } const url = node['properties'].src; node[ 'properties' ].src = `https://res.cloudinary.com/${cloudName}/image/fetch/${url}`; } else if (node.type === 'jsx' && node['value']?.includes('')) { // handles nodes like this: // { // type: 'jsx', // value: '' // } const match = node['value'].match(srcRegex); if (match) { const urlOrRequire = match[1]; node['value'] = node['value'].replace( srcRegex, ` src={${`\`https://res.cloudinary.com/${cloudName}/image/fetch/${baseUrl}\$\{${urlOrRequire}\}\``}}` ); } } }); }; }; } module.exports = imageCloudinaryRemarkPluginFactory;

  • remark

    markdown processor powered by plugins part of the @unifiedjs collective (by remarkjs)

  • Now we have our Cloudinary account set up, we can use it with Docusaurus. To do so, we need to create a remark plugin. This is a plugin for the remark markdown processor. It's a plugin that will transform the markdown image syntax into a Cloudinary URL.

  • SurveyJS

    Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.

    SurveyJS logo
  • remark-cloudinary-docusaurus

    Discontinued Cloudinary offers an image CDN which can improve performance of your site. This plugin allows Docusaurus to use Cloudinary to serve optimised images. [Moved to: https://github.com/johnnyreilly/rehype-cloudinary-docusaurus]

  • But who wants to make a remark plugin? I don't. I want to use a remark plugin. So I created one. It's called remark-cloudinary-docusaurus and you can find it on npm. It's a drop-in replacement for the plugin we created above. You can add it like this (use whichever package manager CLI tool you prefer):

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts