Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for innerBlocks #51

Merged
merged 2 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Previewing unpublished posts or updates to published posts works out of the box.

## Gutenberg / block support

When you query for content (posts, pages, and custom post types), you'll receive the post content as blocks. If the content was written with WordPress's [block editor][gutenberg] (Gutenberg), these blocks will correspond directly with the blocks you see in the editor.
When you query for content (posts, pages, and custom post types), you'll receive the post content as blocks. If the content was written with WordPress's [block editor][gutenberg] (Gutenberg), these blocks will correspond directly with the blocks you see in the editor. The block data you will receive roughly matches the output of WordPress’s [`parse_blocks` function][parse-blocks], with some enhancements. To learn more, you can follow how block data is parsed and resolved in [our extension of WPGraphQL][content-blocks].

Receiving the content as blocks allows you to easily map each block type to a React component, and the [block attributes][block-attributes] to component props. This boilerplate provides a few mappings for basic components like headings, paragraphs, and lists. Here is a simple example of this mapping:

Expand Down Expand Up @@ -104,7 +104,7 @@ When running the development server, in order to help you identify blocks that h

When writing code that links to another page in your Next.js application, you should use Next.js's [`Link` component][next-link] so that the request is routed client-side without a full-round trip to the server.

However, when your blocks contain links, the `innerHTML` is handled by React and you don't have an opportunity to use the `Link` component. To address this, our boilerplate [listens for link clicks][link-listener] and will route them client-side if the link destination is determined to be internal. You can configure which hostnames are considered internal in [`lib/config`][lib-config].
However, when user-authored blocks contain links, the `innerHTML` is handled by React and you don't have an opportunity to use the `Link` component. To address this, our boilerplate [listens for link clicks][link-listener] and will route them client-side if the link destination is determined to be internal. You can configure which hostnames are considered internal in [`lib/config`][lib-config].

## Data fetching

Expand Down Expand Up @@ -238,6 +238,7 @@ For the API images, the `srcSet` property is automatically defined by the `devic
[cache-config]: https://github.com/Automattic/vip-go-nextjs-skeleton/blob/725c0695ad603d2ecc8b56ff1c9f1cad95f5fe98/next.config.js#L34-L51
[classic-editor]: https://wordpress.com/support/classic-editor-guide/
[code-generation]: https://www.graphql-code-generator.com
[content-blocks]: https://github.com/Automattic/vip-decoupled-bundle/blob/trunk/blocks/blocks.php
[eslint-config]: https://github.com/Automattic/vip-go-nextjs-skeleton/blob/725c0695ad603d2ecc8b56ff1c9f1cad95f5fe98/.eslintrc
[express]: https://expressjs.com
[feed-redirect]: https://github.com/Automattic/vip-go-nextjs-skeleton/blob/725c0695ad603d2ecc8b56ff1c9f1cad95f5fe98/next.config.js#L95-L100
Expand All @@ -259,6 +260,7 @@ For the API images, the `srcSet` property is automatically defined by the `devic
[nextjs-ts]: https://nextjs.org/docs/basic-features/typescript
[output-file-tracing]: https://nextjs.org/docs/advanced-features/output-file-tracing
[page-cache]: https://docs.wpvip.com/technical-references/caching/page-cache/
[parse-blocks]: https://github.com/WordPress/wordpress-develop/blob/5.8.1/src/wp-includes/blocks.php#L879-L891
[post]: https://github.com/Automattic/vip-go-nextjs-skeleton/blob/725c0695ad603d2ecc8b56ff1c9f1cad95f5fe98/pages/%5B...slug%5D.tsx
[post-content]: https://github.com/Automattic/vip-go-nextjs-skeleton/blob/725c0695ad603d2ecc8b56ff1c9f1cad95f5fe98/components/PostContent/PostContent.tsx
[server-entrypoint]: https://github.com/Automattic/vip-go-nextjs-skeleton/blob/725c0695ad603d2ecc8b56ff1c9f1cad95f5fe98/server/index.js
Expand Down
26 changes: 22 additions & 4 deletions components/UnsupportedBlock/UnsupportedBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,33 @@ export default function UnsupportedBlock ( props: Props ) {
return (
<div className={styles.container}>
<h4 className={styles.title}><strong>Unsupported block</strong>: <code>{props.block.name}</code></h4>
<blockquote>
{props.block.innerHTML}
</blockquote>
{
props.block.tagName &&
<h5>{props.block.tagName}</h5>
}
{
props.block.innerHTML &&
<blockquote>
{props.block.innerHTML}
</blockquote>
}
{
props.block.attributes.length > 0 && (
<ul>
{
props.block.attributes.map( ( attr, i ) => (
<li key={i}><strong>{attr.name}</strong>: {attr.value}</li>
<li key={i}><strong>{attr.name}</strong>: {attr.value || 'null'}</li>
) )
}
</ul>
)
}
{
props.block.innerBlocks.length > 0 && (
<ul>
{
props.block.innerBlocks.map( ( block, i ) => (
<li key={i}><strong>{block.name}</strong></li>
) )
}
</ul>
Expand Down
9 changes: 9 additions & 0 deletions graphql/fragments/ContentBlock.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fragment ContentBlockFields on ContentBlock {
attributes {
name
value
}
innerHTML(removeWrappingTag: true)
name
tagName
}
9 changes: 4 additions & 5 deletions graphql/fragments/ContentNode.graphql
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
#import "./ContentType.graphql"
#import "./ContentBlock.graphql"

fragment ContentNodeFields on ContentNode {
id
... on NodeWithContentEditor {
contentBlocks {
isGutenberg
blocks {
attributes {
name
value
...ContentBlockFields
innerBlocks {
...ContentBlockFields
}
innerHTML
name
}
}
}
Expand Down