npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nuxt-strapi-blocks-renderer

v1.0.13

Published

Renderer for the strapi CMS blocks text content element.

Downloads

1,581

Readme

Nuxt Strapi Blocks Renderer

Github Actions NPM version NPM downloads License

A fully customizable Nuxt 3 module for rendering text with the new Blocks rich text editor element from Strapi CMS.

The implementation is based on Strapi's Blocks React Renderer.

Installation

  1. Install the Blocks renderer:

    npm install nuxt-strapi-blocks-renderer
  2. Add the module to nuxt.config.{ts|js}:

    modules: ['nuxt-strapi-blocks-renderer']

Usage

To render text, use the StrapiBlocksText component:

<StrapiBlocksText :nodes="blockNodes" />

In this example, the blockNodes are taken from the JSON response which Strapi provides when using the Blocks rich text editor element:

<script setup lang="ts">
    import type { BlockNode } from '#strapi-blocks-renderer/types';
    import type { Restaurant } from '~/types';

    const route = useRoute();
    const { findOne } = useStrapi();

    // Fetch restaurants data from Strapi
    const response = await findOne<Restaurant>('restaurants', route.params.id);
    
    // Obtain blocks text nodes from description field
    const blockNodes: BlockNode[] = response.data.attributes.description;
</script>

<template>
    <!-- Render blocks text -->
    <StrapiBlocksText :nodes="blockNodes" />
</template>

To use the useStrapi composable, install the Strapi Nuxt module.

Advanced Usage

In situations where your project requires specific styling or behavior for certain HTML tags such as <a>, <p>, and others, you can override the default rendering components used by the Nuxt Strapi Blocks Renderer. This flexibility allows you to tailor the rendering to align with your project's unique design and functional needs.

Global Component Registration

First, ensure that your components are globally registered in your Nuxt app. This step is crucial for your custom components to be recognized and used by the renderer.

In your Nuxt configuration (nuxt.config.{js|ts}), add:

components: {
    dirs: [
        {
            path: '~/components',
        },
    ],
    global: true,
},

Customizing the Paragraph Tag

To customize the rendering of the paragraph (<p>) tag, you need to create a corresponding Vue component. The name of the component follows a predefined pattern: 'StrapiBlocksText' + [NodeName] + 'Node.vue'. To override the default paragraph tag, we create a file called StrapiBlocksTextParagraphNode.vue.

<!-- components/StrapiBlocksTextParagraphNode.vue -->
<template>
    <p class="my-custom-class-for-p">
        <slot />
    </p>
</template>

This component assigns a custom class my-custom-class-for-p to the paragraph tag, which can be styled as needed.

The prefix for the custom components can be adjusted in your nuxt.config.{js|ts}:

modules: ['nuxt-strapi-blocks-renderer'],
strapiBlocksRenderer: {
    prefix: 'MyCustomPrefix'
    blocksPrefix: 'MyCustomBlocksPrefix',
},

With this configuration, the StrapiBlocksText component becomes MyCustomPrefixStrapiBlocksText and the custom paragraph node component would be named MyCustomBlocksPrefixParagraphNode.

Other Custom Tags

You can apply similar customizations to all other HTML tags used by the renderer:

Custom heading tags (<h1>, <h2>, <h3>, etc.):

<!-- components/StrapiBlocksTextHeading1Node.vue -->
<template>
    <h1 class="my-custom-class-for-h1">
        <slot />
    </h1>
</template>

<!-- components/StrapiBlocksTextHeading2Node.vue -->
<template>
    <h2 class="my-custom-class-for-h2">
        <slot />
    </h2>
</template>

This pattern also extends to the h3, h4, h5 and h6 tags.

Custom list tags (<ol>, <ul> and <li>):

<!-- components/StrapiBlocksTextOrderedListNode.vue -->
<template>
    <ol class="my-custom-class-for-ol">
        <slot />
    </ol>
</template>

<!-- components/StrapiBlocksTextUnorderedListNode.vue -->
<template>
    <ul class="my-custom-class-for-ul">
        <slot />
    </ul>
</template>

<!-- components/StrapiBlocksTextListItemInlineNode.vue -->
<template>
    <li class="my-custom-class-for-li">
        <slot />
    </li>
</template>

Custom blockquote and code tags (<blockquote>, <pre>):

<!-- components/StrapiBlocksTextQuoteNode.vue -->
<template>
    <blockquote class="my-custom-class-for-blockquote">
        <slot />
    </blockquote>
</template>

<!-- components/StrapiBlocksTextCodeNode.vue -->
<template>
    <pre class="my-custom-class-for-pre"><slot /></pre>
</template>

Custom inline text nodes (<strong>, <em>, <u>, <del>, <code>):

<!-- components/StrapiBlocksTextBoldInlineNode.vue -->
<template>
    <strong class="my-custom-class-for-strong">
        <slot />
    </strong>
</template>

<!-- components/StrapiBlocksTextItalicInlineNode.vue -->
<template>
    <em class="my-custom-class-for-em">
        <slot />
    </em>
</template>

<!-- components/StrapiBlocksTextUnderlineInlineNode.vue -->
<template>
    <u class="my-custom-class-for-u">
        <slot />
    </u>
</template>

<!-- components/StrapiBlocksTextStrikethroughInlineNode.vue -->
<template>
    <del class="my-custom-class-for-del">
        <slot />
    </del>
</template>

<!-- components/StrapiBlocksTextCodeInlineNode.vue -->
<template>
    <code class="my-custom-class-for-code">
        <slot />
    </code>
</template>

Custom link tag (<a>):

<!-- components/StrapiBlocksTextLinkInlineNode.vue -->
<script setup lang="ts">
    const props = defineProps<{
        url: string;
    }>();
</script>

<template>
    <a :href="props.url" class="my-custom-class-for-a">
        <slot />
    </a>
</template>

When rendering a link tag, the url gets passed as the url component property.

Custom image tag (<img>):

<!-- components/StrapiBlocksTextImageNode.vue -->
<script setup lang="ts">
    const props = defineProps<{
        image: any;
    }>();
</script>

<template>
    <img
        class="my-custom-class-for-img"
        :src="props.image.url"
        :alt="props.image.alternativeText"
        :width="props.image.width"
        :height="props.image.height"
    >
</template>

When rendering an image tag, the image object gets passed as the image component property. You can also use different image components here, i.e. NuxtImg or others.

Development

Dependencies

To install the dependencies, run the install command:

npm install

The project requires Node.js and NPM to run. You can either install these manually on your system or if you have the nix package manager installed, use the provided nix-shell with the following command:

nix-shell

This will automatically install the needed software and start up a shell.

Type stubs

To generate the type stubs for the nuxt module, run the dev:prepare command:

npm run dev:prepare

Development server

To start the development server with the provided text components, run the dev command:

npm run dev

This will boot up the playground with the default text components. To start the development server using custom text components, overriding the provided components, use the dev:custom command:

npm run dev:custom

Quality

Linter

To run ESLint, use the following command:

npm run lint:es

Type checks

To run the TypeScript type checks, use the following command:

npm run lint:types

Unit Tests

To run the Vitest unit tests, run the following command:

npm run test

Build

To build the module, first install all dependencies and generate the type stubs. Then run the build script:

npm run build

The module files will be output to the dist folder.

Release

To release a new version of the strapi blocks renderer nuxt module, take the following steps:

  1. Increment version number in the package.json file

  2. Add changelog entry for the new version number

  3. Run linters and unit tests

  4. Build the nuxt module

    npm run build
  5. Log in to NPM using your access token

  6. Run the release command

    npm run release