nuxt-strapi-blocks-renderer 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/README.md +25 -0
  2. package/dist/module.cjs +5 -0
  3. package/dist/module.d.mts +9 -0
  4. package/dist/module.d.ts +9 -0
  5. package/dist/module.json +5 -0
  6. package/dist/module.mjs +45 -0
  7. package/dist/runtime/components/StrapiBlocksText.vue +19 -0
  8. package/dist/runtime/components/blocks/CodeNode.vue +5 -0
  9. package/dist/runtime/components/blocks/ImageNode.vue +14 -0
  10. package/dist/runtime/components/blocks/ParagraphNode.vue +5 -0
  11. package/dist/runtime/components/blocks/QuoteNode.vue +5 -0
  12. package/dist/runtime/components/blocks/heading/Heading1Node.vue +5 -0
  13. package/dist/runtime/components/blocks/heading/Heading2Node.vue +5 -0
  14. package/dist/runtime/components/blocks/heading/Heading3Node.vue +5 -0
  15. package/dist/runtime/components/blocks/heading/Heading4Node.vue +5 -0
  16. package/dist/runtime/components/blocks/heading/Heading5Node.vue +5 -0
  17. package/dist/runtime/components/blocks/heading/Heading6Node.vue +5 -0
  18. package/dist/runtime/components/blocks/inline/BoldInlineNode.vue +5 -0
  19. package/dist/runtime/components/blocks/inline/CodeInlineNode.vue +5 -0
  20. package/dist/runtime/components/blocks/inline/ItalicInlineNode.vue +5 -0
  21. package/dist/runtime/components/blocks/inline/LinkInlineNode.vue +11 -0
  22. package/dist/runtime/components/blocks/inline/ListItemInlineNode.vue +5 -0
  23. package/dist/runtime/components/blocks/inline/StrikethroughInlineNode.vue +5 -0
  24. package/dist/runtime/components/blocks/inline/UnderlineInlineNode.vue +5 -0
  25. package/dist/runtime/components/blocks/list/OrderedListNode.vue +5 -0
  26. package/dist/runtime/components/blocks/list/UnorderedListNode.vue +5 -0
  27. package/dist/runtime/composables/useBlocksText.d.ts +6 -0
  28. package/dist/runtime/composables/useBlocksText.mjs +7 -0
  29. package/dist/runtime/types/index.d.ts +67 -0
  30. package/dist/runtime/utils/index.d.ts +12 -0
  31. package/dist/runtime/utils/index.mjs +113 -0
  32. package/dist/types.d.mts +16 -0
  33. package/dist/types.d.ts +16 -0
  34. package/package.json +47 -0
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # Nuxt Strapi Blocks Renderer
2
+
3
+ Renderer module for the Strapi CMS blocks text element.
4
+
5
+ ## Development
6
+
7
+ ```bash
8
+ # Install dependencies
9
+ npm install
10
+
11
+ # Generate type stubs
12
+ npm run dev:prepare
13
+
14
+ # Develop with the playground
15
+ npm run dev
16
+
17
+ # Build the playground
18
+ npm run dev:build
19
+
20
+ # Run ESLint
21
+ npm run lint
22
+
23
+ # Release new version
24
+ npm run release
25
+ ```
@@ -0,0 +1,5 @@
1
+ module.exports = function(...args) {
2
+ return import('./module.mjs').then(m => m.default.call(this, ...args))
3
+ }
4
+ const _meta = module.exports.meta = require('./module.json')
5
+ module.exports.getMeta = () => Promise.resolve(_meta)
@@ -0,0 +1,9 @@
1
+ import * as _nuxt_schema from '@nuxt/schema';
2
+
3
+ type ModuleOptions = {
4
+ prefix: string;
5
+ blocksPrefix: string;
6
+ };
7
+ declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
8
+
9
+ export { type ModuleOptions, _default as default };
@@ -0,0 +1,9 @@
1
+ import * as _nuxt_schema from '@nuxt/schema';
2
+
3
+ type ModuleOptions = {
4
+ prefix: string;
5
+ blocksPrefix: string;
6
+ };
7
+ declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
8
+
9
+ export { type ModuleOptions, _default as default };
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "strapi-blocks-renderer",
3
+ "configKey": "strapiBlocksRenderer",
4
+ "version": "0.0.1"
5
+ }
@@ -0,0 +1,45 @@
1
+ import { defineNuxtModule, createResolver, addImports, addComponentsDir } from '@nuxt/kit';
2
+ import { defu } from 'defu';
3
+
4
+ const module = defineNuxtModule({
5
+ meta: {
6
+ name: "strapi-blocks-renderer",
7
+ configKey: "strapiBlocksRenderer"
8
+ },
9
+ defaults: {
10
+ prefix: "",
11
+ blocksPrefix: "StrapiBlocksText"
12
+ },
13
+ setup(options, nuxt) {
14
+ const { resolve } = createResolver(import.meta.url);
15
+ const runtimeDirectory = resolve("./runtime");
16
+ nuxt.options.alias["#strapi-blocks-renderer"] = resolve(runtimeDirectory);
17
+ const runtimeConfig = nuxt.options.runtimeConfig;
18
+ runtimeConfig.public.strapiBlocksRenderer = defu(runtimeConfig.public.strapiBlocksRenderer, options);
19
+ addImports([
20
+ {
21
+ name: "useBlocksText",
22
+ as: "useBlocksText",
23
+ from: resolve(runtimeDirectory, "./composables/useBlocksText")
24
+ }
25
+ ]);
26
+ addComponentsDir({
27
+ path: resolve(runtimeDirectory, "./components"),
28
+ pathPrefix: false,
29
+ prefix: options.prefix,
30
+ global: true,
31
+ priority: 0
32
+ });
33
+ nuxt.hook("components:dirs", (componentsDir) => {
34
+ componentsDir.push({
35
+ path: resolve(runtimeDirectory, "./components/blocks"),
36
+ pathPrefix: false,
37
+ prefix: options.blocksPrefix,
38
+ global: true,
39
+ priority: -10
40
+ });
41
+ });
42
+ }
43
+ });
44
+
45
+ export { module as default };
@@ -0,0 +1,19 @@
1
+ <script setup lang="ts">
2
+ import { useBlocksText } from '#strapi-blocks-renderer/composables/useBlocksText';
3
+
4
+ import type { BlockNode } from '#strapi-blocks-renderer/types';
5
+
6
+ const props = defineProps<{
7
+ nodes: BlockNode[];
8
+ }>();
9
+
10
+ const { text } = useBlocksText(props.nodes);
11
+
12
+ const RenderText = (): VNode[] => {
13
+ return text;
14
+ };
15
+ </script>
16
+
17
+ <template>
18
+ <RenderText />
19
+ </template>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <pre>
3
+ <slot />
4
+ </pre>
5
+ </template>
@@ -0,0 +1,14 @@
1
+ <script setup lang="ts">
2
+ const props = defineProps<{
3
+ image: any;
4
+ }>();
5
+ </script>
6
+
7
+ <template>
8
+ <img
9
+ :src="props.image.url"
10
+ :alt="props.image.alternativeText"
11
+ :width="props.image.width"
12
+ :height="props.image.height"
13
+ >
14
+ </template>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <p>
3
+ <slot />
4
+ </p>
5
+ </template>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <blockquote>
3
+ <slot />
4
+ </blockquote>
5
+ </template>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <h1>
3
+ <slot />
4
+ </h1>
5
+ </template>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <h2>
3
+ <slot />
4
+ </h2>
5
+ </template>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <h3>
3
+ <slot />
4
+ </h3>
5
+ </template>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <h4>
3
+ <slot />
4
+ </h4>
5
+ </template>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <h5>
3
+ <slot />
4
+ </h5>
5
+ </template>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <h6>
3
+ <slot />
4
+ </h6>
5
+ </template>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <strong>
3
+ <slot />
4
+ </strong>
5
+ </template>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <code>
3
+ <slot />
4
+ </code>
5
+ </template>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <em>
3
+ <slot />
4
+ </em>
5
+ </template>
@@ -0,0 +1,11 @@
1
+ <script setup lang="ts">
2
+ const props = defineProps<{
3
+ url: string;
4
+ }>();
5
+ </script>
6
+
7
+ <template>
8
+ <a :href="props.url">
9
+ <slot />
10
+ </a>
11
+ </template>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <li>
3
+ <slot />
4
+ </li>
5
+ </template>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <del>
3
+ <slot />
4
+ </del>
5
+ </template>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <u>
3
+ <slot />
4
+ </u>
5
+ </template>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <ol>
3
+ <slot />
4
+ </ol>
5
+ </template>
@@ -0,0 +1,5 @@
1
+ <template>
2
+ <ul>
3
+ <slot />
4
+ </ul>
5
+ </template>
@@ -0,0 +1,6 @@
1
+ import type { VNode } from 'vue';
2
+ import type { BlockNode } from '#strapi-blocks-renderer/types';
3
+ export type UseBlocksTextReturn = {
4
+ text: VNode[];
5
+ };
6
+ export declare const useBlocksText: (blockNodes: BlockNode[]) => UseBlocksTextReturn;
@@ -0,0 +1,7 @@
1
+ import { renderBlocks } from "#strapi-blocks-renderer/utils";
2
+ export const useBlocksText = (blockNodes) => {
3
+ const textNodes = renderBlocks(blockNodes);
4
+ return {
5
+ text: textNodes
6
+ };
7
+ };
@@ -0,0 +1,67 @@
1
+ // Inline nodes
2
+
3
+ export type TextInlineNode = {
4
+ type: 'text';
5
+ text: string;
6
+ bold?: boolean;
7
+ italic?: boolean;
8
+ underline?: boolean;
9
+ strikethrough?: boolean;
10
+ code?: boolean;
11
+ };
12
+
13
+ export type LinkInlineNode = {
14
+ type: 'link';
15
+ url: string;
16
+ children: TextInlineNode[];
17
+ };
18
+
19
+ export type ListItemInlineNode = {
20
+ type: 'list-item';
21
+ children: DefaultInlineNode[];
22
+ };
23
+
24
+ export type DefaultInlineNode = TextInlineNode | LinkInlineNode;
25
+
26
+ // Typed nodes
27
+
28
+ export type ParagraphBlockNode = {
29
+ type: 'paragraph';
30
+ children: DefaultInlineNode[];
31
+ };
32
+
33
+ export type HeadingBlockNode = {
34
+ type: 'heading';
35
+ level: 1 | 2 | 3 | 4 | 5 | 6;
36
+ children: DefaultInlineNode[];
37
+ };
38
+
39
+ export type ListBlockNode = {
40
+ type: 'list';
41
+ format: 'ordered' | 'unordered';
42
+ children: (ListItemInlineNode | ListBlockNode)[];
43
+ };
44
+
45
+ export type QuoteBlockNode = {
46
+ type: 'quote';
47
+ children: DefaultInlineNode[];
48
+ };
49
+
50
+ export type CodeBlockNode = {
51
+ type: 'code';
52
+ children: TextInlineNode[];
53
+ };
54
+
55
+ export type ImageBlockNode = {
56
+ type: 'image';
57
+ image: any;
58
+ children: [{ type: 'text'; text: '' }];
59
+ };
60
+
61
+ export type BlockNode =
62
+ ParagraphBlockNode |
63
+ QuoteBlockNode |
64
+ CodeBlockNode |
65
+ HeadingBlockNode |
66
+ ListBlockNode |
67
+ ImageBlockNode;
@@ -0,0 +1,12 @@
1
+ import type { BlockNode } from '#strapi-blocks-renderer/types';
2
+ export declare const textInlineNode: (node: TextInlineNode) => VNode | string;
3
+ export declare const linkInlineNode: (node: LinkInlineNode) => VNode;
4
+ export declare const defaultInlineNode: (node: DefaultInlineNode) => VNode | string | undefined;
5
+ export declare const listItemInlineNode: (node: ListItemInlineNode) => VNode;
6
+ export declare const headingBlockNode: (node: BlockNode) => VNode;
7
+ export declare const paragraphBlockNode: (node: BlockNode) => VNode;
8
+ export declare const codeBlockNode: (node: BlockNode) => VNode;
9
+ export declare const quoteBlockNode: (node: BlockNode) => VNode;
10
+ export declare const listBlockNode: (node: BlockNode) => VNode;
11
+ export declare const imageBlockNode: (node: BlockNode) => VNode;
12
+ export declare const renderBlocks: (blockNodes: BlockNode[]) => VNode[];
@@ -0,0 +1,113 @@
1
+ const prefix = () => {
2
+ const { public: { strapiBlocksRenderer } } = useRuntimeConfig();
3
+ return strapiBlocksRenderer.blocksPrefix;
4
+ };
5
+ export const textInlineNode = (node) => {
6
+ if (node.bold)
7
+ return h(resolveComponent(prefix() + "BoldInlineNode"), () => node.text);
8
+ if (node.italic)
9
+ return h(resolveComponent(prefix() + "ItalicInlineNode"), () => node.text);
10
+ if (node.underline)
11
+ return h(resolveComponent(prefix() + "UnderlineInlineNode"), () => node.text);
12
+ if (node.strikethrough)
13
+ return h(resolveComponent(prefix() + "StrikethroughInlineNode"), () => node.text);
14
+ if (node.code)
15
+ return h(resolveComponent(prefix() + "CodeInlineNode"), () => node.text);
16
+ return node.text;
17
+ };
18
+ export const linkInlineNode = (node) => {
19
+ const linkComponent = resolveComponent(prefix() + "LinkInlineNode");
20
+ return h(linkComponent, { url: node.url }, () => node.children.map((childNode) => {
21
+ return textInlineNode(childNode);
22
+ }));
23
+ };
24
+ export const defaultInlineNode = (node) => {
25
+ if (node.type === "link") {
26
+ return linkInlineNode(node);
27
+ } else if (node.type === "text") {
28
+ return textInlineNode(node);
29
+ }
30
+ };
31
+ export const listItemInlineNode = (node) => {
32
+ const listItemComponent = resolveComponent(prefix() + "ListItemInlineNode");
33
+ return h(
34
+ listItemComponent,
35
+ () => node.children.map(
36
+ (childNode) => defaultInlineNode(childNode)
37
+ )
38
+ );
39
+ };
40
+ export const headingBlockNode = (node) => {
41
+ const level = node.level;
42
+ const headingComponent = resolveComponent(prefix() + "Heading" + level + "Node");
43
+ return h(
44
+ headingComponent,
45
+ () => node.children.map(
46
+ (childNode) => defaultInlineNode(childNode)
47
+ )
48
+ );
49
+ };
50
+ export const paragraphBlockNode = (node) => {
51
+ const paragraphComponent = resolveComponent(prefix() + "ParagraphNode");
52
+ return h(
53
+ paragraphComponent,
54
+ () => node.children.map(
55
+ (childNode) => defaultInlineNode(childNode)
56
+ )
57
+ );
58
+ };
59
+ export const codeBlockNode = (node) => {
60
+ const codeComponent = resolveComponent(prefix() + "CodeNode");
61
+ return h(
62
+ codeComponent,
63
+ () => node.children.map(
64
+ (childNode) => textInlineNode(childNode)
65
+ )
66
+ );
67
+ };
68
+ export const quoteBlockNode = (node) => {
69
+ const quoteComponent = resolveComponent(prefix() + "QuoteNode");
70
+ return h(
71
+ quoteComponent,
72
+ () => node.children.map(
73
+ (childNode) => defaultInlineNode(childNode)
74
+ )
75
+ );
76
+ };
77
+ export const listBlockNode = (node) => {
78
+ const listType = node.format === "ordered" ? "OrderedListNode" : "UnorderedListNode";
79
+ const listComponent = resolveComponent(prefix() + listType);
80
+ return h(listComponent, () => node.children.map(
81
+ (childNode) => {
82
+ if (childNode.type === "list") {
83
+ return listBlockNode(childNode);
84
+ } else if (childNode.type === "list-item") {
85
+ return listItemInlineNode(childNode);
86
+ }
87
+ }
88
+ ));
89
+ };
90
+ export const imageBlockNode = (node) => {
91
+ const imageComponent = resolveComponent(prefix() + "ImageNode");
92
+ return h(imageComponent, {
93
+ image: node.image
94
+ });
95
+ };
96
+ export const renderBlocks = (blockNodes) => {
97
+ return blockNodes.map((blockNode) => {
98
+ switch (blockNode.type) {
99
+ case "heading":
100
+ return headingBlockNode(blockNode);
101
+ case "paragraph":
102
+ return paragraphBlockNode(blockNode);
103
+ case "code":
104
+ return codeBlockNode(blockNode);
105
+ case "list":
106
+ return listBlockNode(blockNode);
107
+ case "quote":
108
+ return quoteBlockNode(blockNode);
109
+ case "image":
110
+ return imageBlockNode(blockNode);
111
+ }
112
+ });
113
+ };
@@ -0,0 +1,16 @@
1
+
2
+ import type { ModuleOptions } from './module'
3
+
4
+
5
+ declare module '@nuxt/schema' {
6
+ interface NuxtConfig { ['strapiBlocksRenderer']?: Partial<ModuleOptions> }
7
+ interface NuxtOptions { ['strapiBlocksRenderer']?: ModuleOptions }
8
+ }
9
+
10
+ declare module 'nuxt/schema' {
11
+ interface NuxtConfig { ['strapiBlocksRenderer']?: Partial<ModuleOptions> }
12
+ interface NuxtOptions { ['strapiBlocksRenderer']?: ModuleOptions }
13
+ }
14
+
15
+
16
+ export type { ModuleOptions, default } from './module'
@@ -0,0 +1,16 @@
1
+
2
+ import type { ModuleOptions } from './module'
3
+
4
+
5
+ declare module '@nuxt/schema' {
6
+ interface NuxtConfig { ['strapiBlocksRenderer']?: Partial<ModuleOptions> }
7
+ interface NuxtOptions { ['strapiBlocksRenderer']?: ModuleOptions }
8
+ }
9
+
10
+ declare module 'nuxt/schema' {
11
+ interface NuxtConfig { ['strapiBlocksRenderer']?: Partial<ModuleOptions> }
12
+ interface NuxtOptions { ['strapiBlocksRenderer']?: ModuleOptions }
13
+ }
14
+
15
+
16
+ export type { ModuleOptions, default } from './module'
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "nuxt-strapi-blocks-renderer",
3
+ "version": "0.0.1",
4
+ "description": "Renderer for the strapi blocks text element.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/types.d.ts",
10
+ "import": "./dist/module.mjs",
11
+ "require": "./dist/module.cjs"
12
+ }
13
+ },
14
+ "main": "./dist/module.cjs",
15
+ "types": "./dist/types.d.ts",
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "scripts": {
20
+ "prepack": "nuxt-module-build build",
21
+ "dev": "npm run dev:basic",
22
+ "dev:basic": "nuxi dev playground/basic",
23
+ "dev:custom": "nuxi dev playground/custom",
24
+ "release": "npm run lint && npm run test && npm run prepack && npm publish && git push --follow-tags",
25
+ "lint": "eslint './**/*.{js,ts,vue}' --max-warnings=0",
26
+ "test": "vitest run",
27
+ "test:watch": "vitest watch"
28
+ },
29
+ "dependencies": {
30
+ "@nuxt/kit": "^3.8.2",
31
+ "defu": "^6.1.3"
32
+ },
33
+ "devDependencies": {
34
+ "@types/node": "^20.10.3",
35
+ "@nuxt/devtools": "^1.0.6",
36
+ "@nuxt/eslint-config": "^0.2.0",
37
+ "@nuxt/module-builder": "^0.5.4",
38
+ "@nuxt/schema": "^3.8.2",
39
+ "@nuxt/test-utils": "^3.8.1",
40
+ "@stylistic/eslint-plugin": "^1.5.1",
41
+ "eslint": "^8.55.0",
42
+ "nuxt": "^3.8.2",
43
+ "vitest": "^1.1.0",
44
+ "vue": "^3.3.13",
45
+ "vue-eslint-parser": "^9.3.2"
46
+ }
47
+ }