diff --git a/components/about-section-bucket.js b/components/about-section-bucket.js index 8744fb2..1aecff8 100644 --- a/components/about-section-bucket.js +++ b/components/about-section-bucket.js @@ -12,7 +12,7 @@ export default function AboutSectionBucket(props) {
{bucket.title_h3 &&

{bucket.title_h3}

} - {bucket.description && parse(bucket.description)} + {typeof bucket.description === "string" && parse(bucket.description)}
); diff --git a/components/archive-relative.js b/components/archive-relative.js index 979fd72..94bc072 100644 --- a/components/archive-relative.js +++ b/components/archive-relative.js @@ -11,7 +11,7 @@ export default function ArchiveRelative(props) {

{blog.title}

- {parse(blog.body.slice(0, 80))} + {typeof blog.body === "string" && parse(blog.body.slice(0, 80))}
diff --git a/components/blog-section.js b/components/blog-section.js index d6ac4a0..622a738 100644 --- a/components/blog-section.js +++ b/components/blog-section.js @@ -29,7 +29,7 @@ class BlogSection extends React.Component { )}
{blog.title &&

{blog.title}

} - {blog.body && parse(blog.body.slice(0, 300))} + {typeof blog.body === "string" && parse(blog.body.slice(0, 300))} {blog.url && ( {"Read More -->"} diff --git a/components/footer.js b/components/footer.js index fe6bef9..b0113bc 100644 --- a/components/footer.js +++ b/components/footer.js @@ -43,7 +43,7 @@ export default function Footer(props) {
- {footer.copyright && parse(footer.copyright)} + {typeof footer.copyright === "string" && parse(footer.copyright)}
); diff --git a/components/header.js b/components/header.js index 3c973d3..3a009f5 100644 --- a/components/header.js +++ b/components/header.js @@ -9,9 +9,10 @@ export default function Header(props) { return (
- {header.notification_bar.show_announcement ? ( - parse(header.notification_bar.announcement_text) - ) : ( + {header.notification_bar.show_announcement ? typeof header.notification_bar.announcement_text === "string" + && ( + parse(header.notification_bar.announcement_text) + ) : (
Devtools section
)} } {bucket.title_h3 ?

{bucket.title_h3}

: ""} - {bucket.description && parse(bucket.description)} + {typeof bucket.description === "string" && parse(bucket.description)} {bucket.call_to_action.title ? (
{embedCode.title &&

{embedCode.title}

} - {embedCode.description && parse(embedCode.description)} + {typeof embedCode.description === "string" && parse(embedCode.description)}
- {embedCode.html_code + {typeof embedCode.html_code === "string" && parse(embedCode.html_code)}
@@ -19,7 +19,7 @@ export default function SectionWithHtmlCode(props) { return (
- {parse(embedCode.html_code)} + {typeof embedCode.html_code === "string" && parse(embedCode.html_code)}
{embedCode.title ? ( @@ -27,7 +27,7 @@ export default function SectionWithHtmlCode(props) { ) : ( "" )} - {embedCode.description && parse(embedCode.description)} + {typeof embedCode.description === "string" && parse(embedCode.description)}
); diff --git a/package-lock.json b/package-lock.json index 0b713b7..9fdc103 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "contentstack-nextjs-starter-app", - "version": "1.1.0", + "version": "1.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 6d1ff03..c5a8d45 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "contentstack-nextjs-starter-app", "description": "A starter app for Contentstack and Nextjs", - "version": "1.1.1", + "version": "1.2.0", "private": true, "author": "Contentstack", "scripts": { diff --git a/pages/about-us.jsx b/pages/about-us.jsx index 165f734..b2aabb2 100644 --- a/pages/about-us.jsx +++ b/pages/about-us.jsx @@ -22,12 +22,20 @@ export default function About(props) { export async function getServerSideProps(context) { try { - const result = await Stack.getEntryByUrl("page", context.resolvedUrl); - const header = await Stack.getEntry( - "header", - "navigation_menu.page_reference", - ); - const footer = await Stack.getEntry("footer"); + const result = await Stack.getEntryByUrl({ + contentTypeUid: "page", + entryUrl: context.resolvedUrl, + jsonRtePath: ["page_components.section_with_buckets.buckets.description"], + }); + const header = await Stack.getEntry({ + contentTypeUid: "header", + referenceFieldPath: ["navigation_menu.page_reference"], + jsonRtePath: ["notification_bar.announcement_text"], + }); + const footer = await Stack.getEntry({ + contentTypeUid: "footer", + jsonRtePath: ["copyright"], + }); return { props: { header: header[0][0], diff --git a/pages/blog/[...post].jsx b/pages/blog/[...post].jsx index 20daeff..c242238 100644 --- a/pages/blog/[...post].jsx +++ b/pages/blog/[...post].jsx @@ -8,9 +8,7 @@ import RenderComponents from "../../components/render-components"; import ArchiveRelative from "../../components/archive-relative"; export default function BlogPost(props) { - const { - header, banner, footer, result, - } = props; + const { header, banner, footer, result } = props; return ( {banner.page_components && ( @@ -26,12 +24,10 @@ export default function BlogPost(props) {

{result.title ? result.title : ""}

- {moment(result.date).format("ddd, MMM D YYYY")} - , - {" "} + {moment(result.date).format("ddd, MMM D YYYY")},{" "} {result.author[0].title}

- {parse(result.body)} + {typeof result.body === "string" && parse(result.body)}
@@ -49,17 +45,25 @@ export default function BlogPost(props) { } export async function getServerSideProps({ params }) { try { - const banner = await Stack.getEntryByUrl("page", "/blog"); - const header = await Stack.getEntry( - "header", - "navigation_menu.page_reference", - ); - const footer = await Stack.getEntry("footer"); - const blog = await Stack.getEntryByUrl( - "blog_post", - `/blog/${params.post}`, - ["author", "related_post"], - ); + const banner = await Stack.getEntryByUrl({ + contentTypeUid: "page", + entryUrl: "/blog", + }); + const blog = await Stack.getEntryByUrl({ + contentTypeUid: "blog_post", + entryUrl: `/blog/${params.post}`, + referenceFieldPath: ["author", "related_post"], + jsonRtePath: ["body", "related_post.body"], + }); + const header = await Stack.getEntry({ + contentTypeUid: "header", + referenceFieldPath: ["navigation_menu.page_reference"], + jsonRtePath: ["notification_bar.announcement_text"], + }); + const footer = await Stack.getEntry({ + contentTypeUid: "footer", + jsonRtePath: ["copyright"], + }); return { props: { header: header[0][0], diff --git a/pages/blog/index.jsx b/pages/blog/index.jsx index 919dba9..88bfe9a 100644 --- a/pages/blog/index.jsx +++ b/pages/blog/index.jsx @@ -58,7 +58,8 @@ export default function Blog(props) { {" "} {bloglist.author[0].title}

- {bloglist.body && parse(bloglist.body.slice(0, 300))} + {typeof bloglist.body === "string" + && parse(bloglist.body.slice(0, 300))} {bloglist.url ? ( @@ -88,16 +89,24 @@ export default function Blog(props) { export async function getServerSideProps(context) { try { - const blog = await Stack.getEntryByUrl("page", context.resolvedUrl); - const result = await Stack.getEntry("blog_post", [ - "author", - "related_post", - ]); - const header = await Stack.getEntry( - "header", - "navigation_menu.page_reference", - ); - const footer = await Stack.getEntry("footer"); + const blog = await Stack.getEntryByUrl({ + contentTypeUid: "page", + entryUrl: context.resolvedUrl, + }); + const result = await Stack.getEntry({ + contentTypeUid: "blog_post", + referenceFieldPath: ["author", "related_post"], + jsonRtePath: ["body"], + }); + const header = await Stack.getEntry({ + contentTypeUid: "header", + referenceFieldPath: ["navigation_menu.page_reference"], + jsonRtePath: ["notification_bar.announcement_text"], + }); + const footer = await Stack.getEntry({ + contentTypeUid: "footer", + jsonRtePath: ["copyright"], + }); let archived = [], blogList = []; result[0].forEach((blogs) => { diff --git a/pages/contact-us.jsx b/pages/contact-us.jsx index 5a70ea9..331f156 100644 --- a/pages/contact-us.jsx +++ b/pages/contact-us.jsx @@ -21,14 +21,21 @@ export default function Contact(props) { } export async function getServerSideProps(context) { try { - const result = await Stack.getEntryByUrl("page", context.resolvedUrl, [ - "page_components.from_blog.featured_blogs", - ]); - const header = await Stack.getEntry( - "header", - "navigation_menu.page_reference", - ); - const footer = await Stack.getEntry("footer"); + const result = await Stack.getEntryByUrl({ + contentTypeUid: "page", + entryUrl: context.resolvedUrl, + referenceFieldPath: ["page_components.from_blog.featured_blogs"], + jsonRtePath: ["page_components.section_with_html_code.description"], + }); + const header = await Stack.getEntry({ + contentTypeUid: "header", + referenceFieldPath: ["navigation_menu.page_reference"], + jsonRtePath: ["notification_bar.announcement_text"], + }); + const footer = await Stack.getEntry({ + contentTypeUid: "footer", + jsonRtePath: ["copyright"], + }); return { props: { header: header[0][0], diff --git a/pages/index.jsx b/pages/index.jsx index 65f3012..ad0088c 100644 --- a/pages/index.jsx +++ b/pages/index.jsx @@ -21,14 +21,26 @@ export default function Home(props) { export async function getServerSideProps(context) { try { - const result = await Stack.getEntryByUrl("page", context.resolvedUrl, [ - "page_components.from_blog.featured_blogs", - ]); - const header = await Stack.getEntry( - "header", - "navigation_menu.page_reference", - ); - const footer = await Stack.getEntry("footer"); + const result = await Stack.getEntryByUrl({ + contentTypeUid: "page", + entryUrl: context.resolvedUrl, + referenceFieldPath: [ + "page_components.from_blog.featured_blogs", + ], + jsonRtePath: [ + "page_components.from_blog.featured_blogs.body", + "page_components.section_with_buckets.buckets.description", + ], + }); + const header = await Stack.getEntry({ + contentTypeUid: "header", + referenceFieldPath: ["navigation_menu.page_reference"], + jsonRtePath: ["notification_bar.announcement_text"], + }); + const footer = await Stack.getEntry({ + contentTypeUid: "footer", + jsonRtePath: ["copyright"], + }); return { props: { header: header[0][0], diff --git a/sdk-plugin/index.js b/sdk-plugin/index.js index c1f8c64..7df2cfd 100644 --- a/sdk-plugin/index.js +++ b/sdk-plugin/index.js @@ -1,4 +1,5 @@ -const contentstack = require("contentstack"); +import * as contentstack from "contentstack"; +import * as Utils from "@contentstack/utils"; const Stack = contentstack.Stack({ api_key: process.env.CONTENTSTACK_API_KEY, @@ -10,15 +11,23 @@ const Stack = contentstack.Stack({ if (process.env.CONTENTSTACK_CUSTOM_HOST) { Stack.setHost(process.env.CONTENTSTACK_CUSTOM_HOST); } + +const renderOption = { + ["span"]: (node, next) => { + return next(node.children); + }, +}; + export default { /** * * fetches all the entries from specific content-type * @param {* content-type uid} contentTypeUid * @param {* reference field name} referenceFieldPath + * @param {* Json RTE path} jsonRtePath * */ - getEntry(contentTypeUid, referenceFieldPath) { + getEntry({ contentTypeUid, referenceFieldPath, jsonRtePath }) { return new Promise((resolve, reject) => { const query = Stack.ContentType(contentTypeUid).Query(); if (referenceFieldPath) query.includeReference(referenceFieldPath); @@ -28,11 +37,17 @@ export default { .find() .then( (result) => { + jsonRtePath && + Utils.jsonToHTML({ + entry: result, + paths: jsonRtePath, + renderOption, + }); resolve(result); }, (error) => { reject(error); - }, + } ); }); }, @@ -43,9 +58,10 @@ export default { * @param {* content-type uid} contentTypeUid * @param {* url for entry to be fetched} entryUrl * @param {* reference field name} referenceFieldPath + * @param {* Json RTE path} jsonRtePath * @returns */ - getEntryByUrl(contentTypeUid, entryUrl, referenceFieldPath) { + getEntryByUrl({ contentTypeUid, entryUrl, referenceFieldPath, jsonRtePath }) { return new Promise((resolve, reject) => { const blogQuery = Stack.ContentType(contentTypeUid).Query(); if (referenceFieldPath) blogQuery.includeReference(referenceFieldPath); @@ -53,12 +69,18 @@ export default { const data = blogQuery.where("url", `${entryUrl}`).find(); data.then( (result) => { + jsonRtePath && + Utils.jsonToHTML({ + entry: result, + paths: jsonRtePath, + renderOption, + }); resolve(result[0]); }, (error) => { reject(error); - }, + } ); }); }, -}; +}; \ No newline at end of file