Skip to content

Commit

Permalink
Merge pull request #558 from storyblok/feature/PRO-423-add-new-richtext
Browse files Browse the repository at this point in the history
feat: pro 423 add new richtext
  • Loading branch information
alvarosabu authored Aug 27, 2024
2 parents 41c0b59 + 49a042d commit a6ba281
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 5 deletions.
19 changes: 18 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
};

Expand All @@ -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"
1 change: 1 addition & 0 deletions lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"prepublishOnly": "npm run build && cp ../README.md ./"
},
"dependencies": {
"@storyblok/richtext": "^2.0.0",
"storyblok-js-client": "^6.9.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion lib/tests/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -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`] = `"<p></p><p>Hola<b>in bold</b></p><p></p><p>paragraph after empty line</p><p></p><ul><li><p>an item in a list</p></li><li><p>another item</p></li></ul><p></p><ol><li><p>item in another list</p></li><li><p>another item</p></li></ol><p></p><blockquote><p>this is a quote</p></blockquote><p></p><hr /><p></p><p>some words after an &lt;hr&gt;</p><p></p><p><i>italic text</i></p><p></p><p><s>strikethrough</s></p><p></p><p><u>underlined</u></p><p></p><p><sup>superscript</sup></p><p></p><p><sub>subscript</sub></p><p></p><p><code>inline code</code> </p>"`;
exports[`@storyblok/js > Legacy Rich Text Resolver > should return the rendered HTML when passing a valid RichText object 1`] = `"<p></p><p>Hola<b>in bold</b></p><p></p><p>paragraph after empty line</p><p></p><ul><li><p>an item in a list</p></li><li><p>another item</p></li></ul><p></p><ol><li><p>item in another list</p></li><li><p>another item</p></li></ol><p></p><blockquote><p>this is a quote</p></blockquote><p></p><hr /><p></p><p>some words after an &lt;hr&gt;</p><p></p><p><i>italic text</i></p><p></p><p><s>strikethrough</s></p><p></p><p><u>underlined</u></p><p></p><p><sup>superscript</sup></p><p></p><p><sub>subscript</sub></p><p></p><p><code>inline code</code> </p>"`;
4 changes: 3 additions & 1 deletion lib/tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
19 changes: 18 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
3 changes: 3 additions & 0 deletions playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
>
renderRichTextWithOptions
</button>
<button class="render-new-rich-text" onclick="newRichTextResolver()">
newRichTextRenderer
</button>
<h3>Rich Text Renderer</h3>
<div id="rich-text-container"></div>
<script type="module" src="/main.ts"></script>
Expand Down
28 changes: 28 additions & 0 deletions playground/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
renderRichText,
useStoryblokBridge,
apiPlugin,
richTextResolver,
StoryblokRichTextOptions
} from "@storyblok/js";
import richTextFixture from "../lib/fixtures/richTextObject.json";

Expand Down Expand Up @@ -43,6 +45,7 @@ declare global {
initCustomRichText: any;
renderRichText: any;
renderRichTextWithOptions: any;
newRichTextResolver: any
}
}

Expand Down Expand Up @@ -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;
}

0 comments on commit a6ba281

Please sign in to comment.