Nuxt Strapi Blocks 渲染器
一个用于 Nuxt 3 & 4 的完全可定制模块,用于渲染 Strapi CMS 的“块”富文本编辑器元素。
该实现基于 Strapi 的 Blocks React 渲染器。
安装
要安装 Nuxt Strapi Blocks 渲染器模块,请运行以下命令
npx nuxt@latest module add nuxt-strapi-blocks-renderer
或者手动安装
- 安装模块
npm install nuxt-strapi-blocks-renderer
- 将模块添加到
nuxt.config.{ts|js}
export default defineNuxtConfig({ modules: [ 'nuxt-strapi-blocks-renderer' ] })
使用
要渲染文本,请使用 StrapiBlocksText
组件
<StrapiBlocksText :nodes="blockNodes" />
在此示例中,blockNodes
取自 Strapi 在使用 Blocks 富文本编辑器元素时提供的 JSON 响应
<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>
要使用 useStrapi
可组合函数,请安装 Strapi Nuxt 模块。
高级用法
在您的项目需要特定 HTML 标签(如 <a>
、<p>
等)的特定样式或行为时,您可以覆盖 Nuxt Strapi Blocks 渲染器使用的默认渲染组件。这种灵活性允许您根据项目的独特设计和功能需求定制渲染。
全局组件注册
首先,请确保您的组件在 Nuxt 应用程序中已全局注册。此步骤对于您的自定义组件被渲染器识别和使用至关重要。
在您的 Nuxt 配置 (nuxt.config.{js|ts}
) 中,添加
export default defineNuxtConfig({
components: {
dirs: [
{
path: '~/components/blocks',
pathPrefix: false,
global: true,
},
// also include ~/components to ensure other local components are resolved properly
'~/components'
],
},
})
!重要
将~/components
包含在 dirs 数组中很重要。省略它可能会导致局部作用域组件(~/components/blocks
之外)无法正确解析。
自定义段落标签
要自定义段落 (<p>
) 标签的渲染,您需要创建一个相应的 Vue 组件。组件的名称遵循预定义模式:'StrapiBlocksText' + [NodeName] + 'Node.vue'
。要覆盖默认段落标签,我们创建一个名为 StrapiBlocksTextParagraphNode.vue
的文件。
<!-- components/blocks/StrapiBlocksTextParagraphNode.vue -->
<template>
<p class="my-custom-class-for-p">
<slot />
</p>
</template>
此组件将自定义类 my-custom-class-for-p
分配给段落标签,可以根据需要进行样式设置。
自定义组件的前缀可以在您的 nuxt.config.{js|ts}
中调整
export default defineNuxtConfig({
strapiBlocksRenderer: {
prefix: 'MyCustomPrefix',
blocksPrefix: 'MyCustomBlocksPrefix',
},
})
通过此配置,StrapiBlocksText
组件变为 MyCustomPrefixStrapiBlocksText
,自定义段落节点组件将被命名为 MyCustomBlocksPrefixParagraphNode
。
其他自定义标签
您可以对渲染器使用的所有其他 HTML 标签应用类似的自定义
标题
自定义标题标签 (<h1>
, <h2>
, <h3>
, 等)
<!-- components/blocks/StrapiBlocksTextHeading1Node.vue -->
<template>
<h1 class="my-custom-class-for-h1">
<slot />
</h1>
</template>
<!-- components/blocks/StrapiBlocksTextHeading2Node.vue -->
<template>
<h2 class="my-custom-class-for-h2">
<slot />
</h2>
</template>
此模式也适用于 h3
、h4
、h5
和 h6
标签。
列表
自定义列表标签 (<ol>
, <ul>
和 <li>
)
<!-- components/blocks/StrapiBlocksTextOrderedListNode.vue -->
<template>
<ol class="my-custom-class-for-ol">
<slot />
</ol>
</template>
<!-- components/blocks/StrapiBlocksTextUnorderedListNode.vue -->
<template>
<ul class="my-custom-class-for-ul">
<slot />
</ul>
</template>
<!-- components/blocks/StrapiBlocksTextListItemInlineNode.vue -->
<template>
<li class="my-custom-class-for-li">
<slot />
</li>
</template>
引用块和代码块
自定义引用块和代码块标签 (<blockquote>
, <pre>
)
<!-- components/blocks/StrapiBlocksTextQuoteNode.vue -->
<template>
<blockquote class="my-custom-class-for-blockquote">
<slot />
</blockquote>
</template>
<!-- components/blocks/StrapiBlocksTextCodeNode.vue -->
<script setup lang="ts">
const props = defineProps<{
language?: string;
}>();
</script>
<template>
<pre class="my-custom-class-for-pre"><slot /></pre>
</template>
行内文本节点
自定义行内文本节点 (<strong>
, <em>
, <u>
, <del>
, <code>
)
<!-- components/blocks/StrapiBlocksTextBoldInlineNode.vue -->
<template>
<strong class="my-custom-class-for-strong">
<slot />
</strong>
</template>
<!-- components/blocks/StrapiBlocksTextItalicInlineNode -->
<template>
<em class="my-custom-class-for-em">
<slot />
</em>
</template>
<!-- components/blocks/StrapiBlocksTextUnderlineInlineNode -->
<template>
<u class="my-custom-class-for-u">
<slot />
</u>
</template>
<!-- components/blocks/StrapiBlocksTextStrikethroughInlineNode -->
<template>
<del class="my-custom-class-for-del">
<slot />
</del>
</template>
<!-- components/blocks/StrapiBlocksTextCodeInlineNode.vue -->
<template>
<code class="my-custom-class-for-code">
<slot />
</code>
</template>
链接
自定义链接标签 (<a>
)
<!-- components/blocks/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>
渲染链接标签时,URL 会作为 url
组件属性传递。
图片
自定义图像标签 (<img>
)
<!-- components/blocks/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>
渲染图像标签时,图像对象会作为 image
组件属性传递。您也可以在此处使用不同的图像组件,例如 NuxtImg
或其他。
开发
依赖项
要安装依赖项,请运行 install
命令
npm install
该项目需要 Node.js 和 NPM 才能运行。您可以手动在系统上安装它们,或者如果您安装了 nix 包管理器,请使用以下命令使用提供的 nix-shell
nix-shell
这将自动安装所需的软件并启动 shell。
类型存根
要为 Nuxt 模块生成类型存根,请运行 dev:prepare
命令
npm run dev:prepare
开发服务器
要启动带有提供的文本组件的开发服务器,请运行 dev
命令
npm run dev
这将启动带有默认文本组件的演练场。要使用自定义文本组件(覆盖提供的组件)启动开发服务器,请使用 dev:custom
命令
npm run dev:custom
质量
代码检查
要运行 ESLint,请使用以下命令
npm run lint:es
类型检查
要运行 TypeScript 类型检查,请使用以下命令
npm run lint:types
单元测试
要运行 Vitest 单元测试,请运行以下命令
npm run test
构建
要构建模块,首先安装所有依赖项并 生成类型存根。然后运行构建脚本
npm run build
模块文件将输出到 dist
文件夹。
发布
要发布 Strapi blocks 渲染器 Nuxt 模块的新版本,请执行以下步骤
- 增加
package.json
文件中的版本号 - 为新版本号添加更新日志条目
- 运行代码检查和单元测试
- 构建 Nuxt 模块
npm run build
- 使用您的访问令牌登录 NPM
- 运行
release
命令npm run release