ProseMirror

Integrate ProseMirror with Stapi

ProseMirror

Why Use ProseMirror?

ProseMirror is a modern, highly customizable rich text editor framework that transforms how developers build content management systems. Unlike traditional best WYSIWYG editors, ProseMirror represents documents as structured, editable JSON objects based on customizable schemas.

What sets ProseMirror apart:

  • Modular Architecture: Load only the components you need, keeping your editor lightweight and performant.
  • Extensible Plugin System: Add custom functionality like tables, math notation, or image handling with ease, boosting your productivity with Strapi plugins.
  • Collaborative Editing: Build real-time collaboration through plugins like prosemirror-collab.
  • Schema Validation: Define exact document structures to prevent invalid content, aiding in content modeling with Strapi.
  • Performance: Handle complex documents without sacrificing speed.

The official ProseMirror documentation offers comprehensive resources and examples to get you started.

Why Use Strapi?

Strapi is the leading open-source headless CMS offering features, like customizable APIs, role-based permissions, multilingual support, etc. It simplifies content management and integrates effortlessly with modern frontend frameworks.

Explore the Strapi documentation for more details.

Strapi 5 Highlights

The out-of-the-box Strapi features allow you to get up and running in no time:

  1. Single types: Create one-off pages that have a unique content structure.
  2. Draft and Publish: Reduce the risk of publishing errors and streamline collaboration.
  3. 100% TypeScript Support: Enjoy type safety & easy maintainability
  4. Customizable API: With Strapi, you can just hop in your code editor and edit the code to fit your API to your needs.
  5. Integrations: Strapi supports integrations with Cloudinary, SendGrid, Algolia, and others.
  6. Editor interface: The editor allows you to pull in dynamic blocks of content.
  7. Authentication: Secure and authorize access to your API with JWT or providers.
  8. RBAC: Help maximize operational efficiency, reduce dev team support work, and safeguard against unauthorized access or configuration modifications.
  9. i18n: Manage content in multiple languages. Easily query the different locales through the API.
  10. Plugins: Customize and extend Strapi using plugins.

Learn more about Strapi 5 feature.

Setup Strapi 5 Headless CMS

We are going to start by setting up our Strapi 5 project with the following command:

šŸ–ļø Note: make sure that you have created a new directory for your project.

You can find the full documentation for Strapi 5 here.

Install Strapi

npx create-strapi-app@latest server

You will be asked to choose if you would like to use Strapi Cloud we will choose to skip for now.

 Strapi   v5.6.0 šŸš€ Let's create your new project

 
We can't find any auth credentials in your Strapi config.

Create a free account on Strapi Cloud and benefit from:

- ✦ Blazing-fast ✦ deployment for your projects
- ✦ Exclusive ✦ access to resources to make your project successful
- An ✦ Awesome ✦ community and full enjoyment of Strapi's ecosystem

Start your 14-day free trial now!


? Please log in or sign up. 
  Login/Sign up 
āÆ Skip 

After that, you will be asked how you would like to set up your project. We will choose the following options:

? Do you want to use the default database (sqlite) ? Yes
? Start with an example structure & data? Yes <-- make sure you say yes 
? Start with Typescript? Yes
? Install dependencies with npm? Yes
? Initialize a git repository? Yes

Once everything is set up and all the dependencies are installed, you can start your Strapi server with the following command:

cd server
npm run develop

You will be greeted with the Admin Create Account screen.

003-strapi-5.png

Go ahead and create your first Strapi user. All of this is local so you can use whatever you want.

Once you have created your user, you will be redirected to the Strapi Dashboard screen.

004-strapi-5.png

Publish Article Entries

Since we created our app with the example data, you should be able to navigate to your Article collection and see the data that was created for us.

005-strapi-5.png

Now, let's make sure that all of the data is published. If not, you can select all items via the checkbox and then click the Publish button.

Strapi Articles Published

Enable API Access

Once all your articles are published, we will expose our Strapi API for the Articles Collection. This can be done in Settings -> Users & Permissions plugin -> Roles -> Public -> Article.

You should have find and findOne selected. If not, go ahead and select them.

007-strapi-5.png

Test API

Now, if we make a GET request to http://localhost:1337/api/articles, we should see the following data for our articles.

008-strapi-5.png

šŸ–ļø Note: The article covers (images) are not returned. This is because the REST API by default does not populate any relations, media fields, components, or dynamic zones.. Learn more about REST API: Population & Field Selection.

So, let's get the article covers by using the populate=* parameter: http://localhost:1337/api/articles?populate=*

vuejs strapi integration - api request.png

Getting Started with ProseMirror

Integrating ProseMirror with Strapi requires proper environment setup and a structured approach. This creates the foundation for a powerful rich-text editing experience in your application.

Installing ProseMirror and Adjusting Settings

Grab these core ProseMirror packages:

npm install prosemirror-state prosemirror-view prosemirror-model

Each package has a specific role:

  • prosemirror-state: Manages editor state
  • prosemirror-view: Handles rendering and user interactions
  • prosemirror-model: Defines document schemas and transformations

Set up your environment variables in an .env file:

STRAPI_HOST=localhost
STRAPI_PORT=1337
NODE_ENV=development

When coding, you might wonder whether to use TypeScript vs JavaScript for Strapi. Both have their merits, so choose the one that best fits your project needs.

Code Implementation: Building Your ProseMirror Editor Within Strapi

With everything installed, let's build a basic ProseMirror editor integrated with Strapi:

import { EditorState } from "prosemirror-state";
import { EditorView } from "prosemirror-view";
import { Schema } from "prosemirror-model";
import { schema } from "prosemirror-schema-basic";

// Set up editor state with basic schema
const state = EditorState.create({ schema });

// Set up editor view in the DOM
const view = new EditorView(document.querySelector("#editor"), { state });

Connecting ProseMirror to Your Strapi Backend

Integrate ProseMirror with Strapi by fetching and saving content, utilizing the evolution of APIs:

// Fetch content from Strapi
const fetchContent = async () => {
  const response = await fetch("http://localhost:1337/api/blog-posts/1");
  const data = await response.json();
  
  // Convert Strapi JSON to ProseMirror document
  const content = JSON.parse(data.attributes.content);
  const newState = EditorState.create({
    schema,
    doc: schema.nodeFromJSON(content)
  });
  
  view.updateState(newState);
};

// Save content to Strapi
const saveContent = async () => {
  const content = JSON.stringify(view.state.doc.toJSON());
  
  await fetch("http://localhost:1337/api/blog-posts/1", {
    method: "PUT",
    headers: { 
      "Content-Type": "application/json",
      "Authorization": "Bearer YOUR_API_TOKEN" 
    },
    body: JSON.stringify({ 
      data: { content } 
    }),
  });
};

Whether you're using REST and GraphQL with Strapi, you have flexibility in how you connect your frontend and backend.

This basic implementation lays the groundwork for integrating ProseMirror with Strapi. As you grow, you can add plugins, customize schemas, and build more sophisticated data mapping between ProseMirror documents and Strapi content types. Whether you're building a simple blog or a complex platform, combining ProseMirror with Strapi provides the flexibility needed for diverse applications, including those requiring a headless CMS for media.

Strapi Open Office Hours

If you have any questions about Strapi 5 or just would like to stop by and say hi, you can join us at Strapi's Discord Open Office Hours Monday through Friday at 12:30 pm - 1:30 pm CST: Strapi Discord Open Office Hours

For more details, visit the Strapi documentation and ProseMirror documentation.

Frequently Asked Questions

ProseMirror is a toolkit for building rich text editors with precise document models. It offers schema-based documents, collaborative editing support, and extensive customization for demanding editing requirements.

Create a custom field plugin wrapping ProseMirror or a ProseMirror-based editor (like TipTap or Milkdown). Configure the schema and plugins for your content requirements.

ProseMirror uses JSON documents conforming to a defined schema. Store this JSON in Strapi, then render it using ProseMirror's DOM serialization or custom renderers.

Yes, ProseMirror has robust collaboration support using operational transformation. Implement a collaboration backend alongside Strapi for real-time multi-user editing.

Yes, ProseMirror's schema system lets you define exactly which elements and attributes are allowed. Create schemas that match your content requirements with custom validation rules.