diff --git a/lib/index.ts b/lib/index.ts index b1c0422..c957115 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -90,6 +90,7 @@ export const storyblokInit = (pluginOptions: SbSDKOptions = {}) => { } // Rich Text resolver + // TODO: replace with @storyblok/richtext package on v4.x richTextResolver = new RichtextResolver(richText.schema); if (richText.resolver) { setComponentResolver(richTextResolver, richText.resolver); @@ -134,12 +135,13 @@ export const renderRichText = ( } if (options) { + // TODO: replace with @storyblok/richtext package on v4.x localResolver = new RichtextResolver(options.schema); if (options.resolver) { setComponentResolver(localResolver, options.resolver); } } - + // NOTE: This will warn the user about deprecation of legacy Richtext when https://github.com/storyblok/storyblok-js-client/pull/845 is merged return localResolver.render(data); }; @@ -157,3 +159,18 @@ export { // Reexport all types so users can have access to them export * from "./types"; + +// New Richtext Resolver +export { + BlockTypes, + MarkTypes, + richTextResolver, + TextTypes, + type StoryblokRichTextOptions, + type StoryblokRichTextDocumentNode, + type StoryblokRichTextNodeTypes, + type StoryblokRichTextNode, + type StoryblokRichTextResolvers, + type StoryblokRichTextNodeResolver, + type StoryblokRichTextImageOptimizationOptions, +} from "@storyblok/richtext" diff --git a/lib/package.json b/lib/package.json index 3b0416c..192a4a3 100644 --- a/lib/package.json +++ b/lib/package.json @@ -28,6 +28,7 @@ "prepublishOnly": "npm run build && cp ../README.md ./" }, "dependencies": { + "@storyblok/richtext": "^2.0.0", "storyblok-js-client": "^6.9.0" }, "devDependencies": { diff --git a/lib/tests/__snapshots__/index.test.js.snap b/lib/tests/__snapshots__/index.test.js.snap index b33d8c4..5af99b8 100644 --- a/lib/tests/__snapshots__/index.test.js.snap +++ b/lib/tests/__snapshots__/index.test.js.snap @@ -1,3 +1,3 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`@storyblok/js > Rich Text Resolver > should return the rendered HTML when passing a valid RichText object 1`] = `"

Holain bold

paragraph after empty line

  1. item in another list

  2. another item

this is a quote


some words after an <hr>

italic text

strikethrough

underlined

superscript

subscript

inline code

"`; +exports[`@storyblok/js > Legacy Rich Text Resolver > should return the rendered HTML when passing a valid RichText object 1`] = `"

Holain bold

paragraph after empty line

  1. item in another list

  2. another item

this is a quote


some words after an <hr>

italic text

strikethrough

underlined

superscript

subscript

inline code

"`; diff --git a/lib/tests/index.test.js b/lib/tests/index.test.js index fd09e33..ffa1d0b 100644 --- a/lib/tests/index.test.js +++ b/lib/tests/index.test.js @@ -83,7 +83,9 @@ describe("@storyblok/js", () => { }) }) - describe("Rich Text Resolver", () => { + // TODO: This might change when legacy rich text resolver is removed and + // the new rich text resolver is implemented instead + describe("Legacy Rich Text Resolver", () => { it("should return the rendered HTML when passing a valid RichText object", () => { storyblokInit({ accessToken: "wANpEQEsMYGOwLxwXQ76Ggtt", bridge: false }); expect(renderRichText(richTextFixture)).toMatchSnapshot(); diff --git a/package-lock.json b/package-lock.json index 715826b..647ec60 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,7 +18,7 @@ "@tsconfig/recommended": "^1.0.7", "eslint": "^8.54.0", "eslint-config-prettier": "^9.1.0", - "husky": "^9.1.5", + "husky": "^9.0.11", "lint-staged": "^15.2.9", "prettier": "^3.3.3" } @@ -27,6 +27,7 @@ "name": "@storyblok/js", "version": "2.0.6", "dependencies": { + "@storyblok/richtext": "^2.0.0", "storyblok-js-client": "^6.9.0" }, "devDependencies": { @@ -1278,6 +1279,14 @@ "resolved": "playground", "link": true }, + "node_modules/@storyblok/richtext": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@storyblok/richtext/-/richtext-2.0.0.tgz", + "integrity": "sha512-yJm5BgWrCRGUdwZtviPgGno3rLXcnEvjK1bL1Ut4GmU1UOEVLC42m543EtrrOBVGDZdRPD9dK8ObgyHp6zNwqw==", + "dependencies": { + "consola": "^3.2.3" + } + }, "node_modules/@storyblok/vue-playground": { "resolved": "playground-vue", "link": true @@ -2389,6 +2398,14 @@ "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==", "dev": true }, + "node_modules/consola": { + "version": "3.2.3", + "resolved": "https://registry.npmjs.org/consola/-/consola-3.2.3.tgz", + "integrity": "sha512-I5qxpzLv+sJhTVEoLYNcTW+bThDCPsit0vLNKShZx6rLtpilNpmmeTPaeqJb9ZE9dV3DGaeby6Vuhrw38WjeyQ==", + "engines": { + "node": "^14.18.0 || >=16.10.0" + } + }, "node_modules/conventional-changelog-angular": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/conventional-changelog-angular/-/conventional-changelog-angular-7.0.0.tgz", diff --git a/package.json b/package.json index 3b15dd1..cbd7c21 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "@tsconfig/recommended": "^1.0.7", "eslint": "^8.54.0", "eslint-config-prettier": "^9.1.0", - "husky": "^9.1.5", + "husky": "^9.0.11", "lint-staged": "^15.2.9", "prettier": "^3.3.3" }, diff --git a/playground/index.html b/playground/index.html index 5b629ce..0ad3f60 100644 --- a/playground/index.html +++ b/playground/index.html @@ -31,6 +31,9 @@ > renderRichTextWithOptions +

Rich Text Renderer

diff --git a/playground/main.ts b/playground/main.ts index d4c855a..69b5dea 100644 --- a/playground/main.ts +++ b/playground/main.ts @@ -4,6 +4,8 @@ import { renderRichText, useStoryblokBridge, apiPlugin, + richTextResolver, + StoryblokRichTextOptions } from "@storyblok/js"; import richTextFixture from "../lib/fixtures/richTextObject.json"; @@ -43,6 +45,7 @@ declare global { initCustomRichText: any; renderRichText: any; renderRichTextWithOptions: any; + newRichTextResolver: any } } @@ -104,3 +107,28 @@ window.renderRichTextWithOptions = () => { window.loadStoryblokBridgeScript = () => { loadStoryblokBridge(); }; + +window.newRichTextResolver = () => { + const options: StoryblokRichTextOptions = { + resolvers: { + custom_link: (node) => { + const attrs = { ...node.attrs }; + + return { + tag: [ + { + tag: "a", + attrs: attrs, + }, + ], + }; + }, + } + } + const html = richTextResolver(options).render(richTextFixture as any); + const richTextContainer = document.getElementById( + "rich-text-container" + ) as any; + + richTextContainer.innerHTML = html; +}