Giter VIP home page Giter VIP logo

wp-graphql-content-blocks's Introduction

WPGraphQL Content Blocks (Structured Content)

This WPGraphQL plugin returns a WordPress post’s content as a shallow tree of blocks and allows for some limited validation and cleanup. This is helpful for rendering post content within component-based front-end ecosystems like React.

What this plugin does

This plugin adds a GraphQL field called blocks to Post in WPGraphQL (and any other post types configured to appear in WPGraphQL).

The blocks field contains a list of the root-level blocks that comprise the post's content. Each block is a distinct HTML element, embeddable URL, shortcode, or Gutenberg block (beta!).

For example, if a post’s content field in GraphQL contained:

<p>Hello world</p>
<ul>
	<li>Here is a list</li>
</ul>

Then the blocks field would contain:

[
	{
		"type": "P",
		"innerHtml": "Hello world"
	},
	{
		"type": "UL",
		"innerHTML": "<li>Here is a list</li>"
	}
]

When consuming this field, you can now easily iterate over the blocks and map them to components in your component library. No more dangerouslySetInnerHTMLing your entire post_content!

GraphQL fields and types

An exhaustive GraphQL of a post’s blocks field would look like this:

blocks {
	type
	tagName
	innerHtml
	attributes {
		name
		value
	}
	connections {
		... on Post {
			...PostParts
		}
		... on MediaItem {
			...MediaItemParts
		}
	}
}

This will return a list of BlockTypes, defined in src/types/Blocktype.php. Let’s break down each of these fields:

type

Type: BlockNameEnumType (defined in src/types/enums/BlockNameEnumType.php)

The name of the block. For HTML blocks, this is the uppercase version of the HTML tag name, e.g. P, UL, BLOCKQUOTE, TABLE etc. Shortcode and embed types are name-spaced with SHORTCODE_ and EMBED_ respectively., e.g. SHORTCODE_CAPTION, EMBED_INSTAGRAM, etc.

HTML block types are hardcoded, as are Gutenberg block types (for now). Embed types are determined by the handlers that have been registered in global $wp_embed->handlers and shortcodes are determined by the handlers registered with global $shortcode_tags. A complete list of permissible block names can seen by browsing the WPGraphQL schema.

You can filter the type definitions with graphql_blocks_definitions.

tagName

Type: String

The suggested HTML tag name for the block. For HTML blocks, this is simply a lowercased version of the type field. For embeds and shortcodes, it will likely be null. This field is most useful for Gutenberg blocks, as a hint from the server for which tag to use when wrapping the innerHtml (see below).

innerHtml

Type: String

The stringified inner content of the block. Can be passed into a React component using dangerouslySetInnerHTML, for example.

Note that the value of innerHtml is the stringified version of all the block’s descendants after they have been parsed. This means that any invalid tags, attributes, etc. will have been stripped out.

attributes

Type: List of BlockAttributeTypes

Each item in the list is a name/value pair describing an attribute of the block. For HTML blocks, these are taken from the HTML attributes, e.g.

{
	"name": "id",
	"value":  "section1"
}

Note that this field will only contain valid attributes for the given Block, as defined in BlockDefinitions. Invalid attributes are stripped out during the parsing process (see How Parsing Works).

connections (beta)

Type: List of MenuItemObjectUnions

The connections field returns an array of objects that are connected to the block. For example, if you wanted to upload an image and associate it with a block, that image could be queried as a GraphQL connection here. The connections field will always be empty by default. Presumably there is some way to derive these connections from the block's attributes, but we have no way of knowing what that correspondence is. If you'd like to use this field, it's up to you to filter graphql_blocks_output and populate the connections array as you see fit.

Blocks

What is a block?

A block is an atomic piece of content that comprises an article body. We define a block as being an HTML tag (like a paragraph, list, table, etc), a Gutenberg block, a text node (the textual content of a non-empty HTML element or shortcode), a shortcode (like a caption, gallery, or video), or an embed (an embeddable URL on its own line in the post content).

HTML Blocks

An HTML block is an HTML element (typically a block-level element) represented by its tag name. If an HTML tag’s name is not included in BlockNameEnumType, it will be stripped from the tree. Additionally, at runtime it must meet the requirements specified in the block definitions in order to be considered valid.

An example HTML block in a GraphQL response looks like this:

{
	"type": "P",
	"innerHtml": "This isn’t the first time Facebook has unveiled new privacy settings in response to user concerns. It debuted a redesign that promised to give users more control over their data <a href=\"https://www.theguardian.com/technology/2010/may/26/facebook-new-privacy-controls-data\">back in 2010</a>. “People think that we don’t care about privacy, but that’s not true,” Zuckerberg said at the time. Yet some observers, including Quartz reporter Mike Murphy, remain skeptical.",
	"attributes": []
}

Gutenberg blocks (beta)

Gutenberg blocks map very well to blocks, but do not have a server-side registration system. Like HTML blocks, we are forced to hardcode a list of core Gutenberg blocks. This list can be extended with graphql_blocks_definitions to add your own custom Gutenberg blocks.

Another issue with Gutenberg is that the markup of a (non-dynamic) block is defined only in JavaScript and then rendered directly into post_content. While we are most interested in the attributes of a block, the innerHtml is also important since the rendered tag name could be important information to, say, a React component tasked with its implementation.

For this reason, we descend into the innerHtml of a Gutenberg block to extract the tagName of the surrounding tag, then discard it, leaving just the true "inner" HTML of the block.

{
	"type": "CORE_HEADING",
	"tagName": "h2",
	"attributes": [],
	"innerHtml": "My Heading"
}

In the example above, this allows the innerHtml to be My Heading instead of <h2>My Heading</h2>. This is a much better situation for the components that implement this data.

Gutenberg blocks present a number of challenges and the spec is still evolving. Take care when using this plugin with Gutenberg blocks since there will likely be breaking changes ahead.

Shortcode and Embed Blocks

A shortcode block is a WordPress shortcode. Shortcode/embed blocks are returned untransformed: the parsing of shortcodes is the responsibility of the front-end consuming the GraphQL endpoint. Only the name of the shortcode, its attributes and any nested content of the shortcode are returned in the GraphQL response.

Shortcode block type names are prefixed with the SHORTCODE_ namespace by default.

{
	"type": "SHORTCODE_PULLQUOTE",
	"innerHtml": "Here is some <abbr title=\"HyperText Markup Language\">HTML</abbr> within a shortcode",
	"attributes": []
}

An embed is a distinct block-type that represents WordPress’ URL-to-markup embedding functionality. If WordPress recognizes a URL as an embed, this plugin will output it as an embed block.

Embed block type names are prefixed with the EMBED_ namespace by default.

{
	"type": "EMBED_TWITTER",
	"innerHtml": "",
	"attributes": [
		{
			"name": "url",
			"value": "https://twitter.com/mcwm/status/978975850455556097"
		}
	]
}

Because neither shortcode or embed blocks are parsed, the markup for embedding the URL is not provided by the plugin.

Block definitions

We can specify validation requirements for individual blocks. This allows us to enforce certain rules about blocks that determine where they end up in the tree, what attributes they may have, and whether or not they should end up in the GraphQL response at all.

Block definitions (and the default definition from which all blocks extend) for blocks can be found in src/types/shared/BlockDefinitions. If you are interested in filtering the block definitions (via graphql_blocks_definitions) to override behavior or add your own block definitions, you should look over that file.

Our default block definition suits us well for most block HTML elements; we always want them to exist at the root and we will hoist them to the root if we find them nested deeper in the post content HTML. We therefore don’t provide any overrides for most block elements:

'blockquote' => [],
'figure' => [],
'h1' => [],
'h2' => [],
'h3' => [],
'h4' => [],
'h5' => [],
'h6' => [],
'hr' => [],
// etc

We permit <p> tags to live at the root, but we do not enforce it (i.e. we don’t want to hoist a <p> tag out of a parent element) so we use this definition.

'p' => [
	'root_only' => false,
],

We don’t want inline HTML elements like <a> to exist by themselves at the root, and we don’t want to permit the target attribute, so we use the following definition:

'a' => [
	'attributes' => array(
		'deny' => array( 'target' ),
	),
	'root_only' => false,
	'allow_root' => false,
],

Now any <a> tag found at the root-level of the post HTML will be wrapped in a <p> tag before being added to the tree. Additionally, if there is a target attribute, it will not appear in the attribute field.

How parsing works

Here’s a rough breakdown of the process of parsing post content into blocks. (If the block is a Gutenberg block, we use gutenberg_parse_blocks and skip these steps.)

  1. The post content string is prepared for parsing (see Fields::prepare_html in src/data/Fields.php). This includes running the wpautop, wptexturize and convert_chars filters.
  2. The prepared content string is loaded into a PHP DOMDocument) object. This allows us to recurse the HTML as a tree.
  3. The DOMDocument object is passed into an HTMLBlock (src/parser/class-htmlblock.php) object. This begins the process of recursing the tree. Each child block is assigned a class depending on its type: HTMLBlock, TextBlock, EmbedBlock or ShortcodeBlock. Each block is responsible for validating itself against the Block Definitions (src/types/shared/BlockDefinitions.php) to determine whether it belongs in the tree or not.
  4. Although the tree is recursed and validated to an infinite depth, the GraphQL type BlockType (src/types/BlockType.php) will stringify the tree below a depth of 1 for consumption in the GraphQL endpoint.

Examples

Given a query for the content of a post returns the following:

Query:

{
	post(id: "cG9zdDoxMjM5NzIx") {
		content
	}
}

Response:

{
	"data": {
		"post": {
			"content": "<p>Now this is a story all about how<br />\nMy life got flipped turned upside down<br />\nAnd I&#8217;d like to take a minute, just sit right there<br />\nI&#8217;ll tell you how I became the prince of a town called Bel-Air</p>\n<p>https://www.youtube.com/watch?v=AVbQo3IOC_A</p>\n<p>In West Philadelphia, born and raised<br />\nOn the playground is where I spent most of my days<br />\nChillin&#8217; out, maxin&#8217;, relaxin&#8217; all cool<br />\nAnd all shootin&#8217; some b-ball outside of the school<br />\nWhen a couple of guys who were up to no good<br />\nStarted makin&#8217; trouble in my neighborhood<br />\nI got in one little fight and my mom got scared<br />\nAnd said &#8220;You&#8217;re movin&#8217; with your auntie and uncle in Bel-Air</p>\n[pullquote]You&#8217;re movin&#8217; with your auntie and uncle in Bel-Air[/pullquote]\n<p>I begged and pleaded with her day after day<br />\nBut she packed my suitcase and sent me on my way<br />\nShe gave me a kiss and then she gave me my ticket<br />\nI put my Walkman on and said &#8220;I might as well kick it&#8221;<br />\nFirst class, yo, this is bad<br />\nDrinkin&#8217; orange juice out of a champagne glass<br />\nIs this what the people of Bel-Air livin&#8217; like?<br />\nHmmm, this might be all right<br />\nBut wait, I hear they&#8217;re prissy, bourgeois, and all that<br />\nIs this the type of place that they just sent this cool cat?<br />\nI don&#8217;t think so, I&#8217;ll see when I get there<br />\nI hope they&#8217;re prepared for the Prince of Bel-Air</p>\n<p><img src=\"https://example.com/fresh-prince.jpeg\" alt=\"The Fresh Prince of Bel Air\" /></p>\n"
		}
	}
}

Then we would expect a query for the blocks that comprise the post to return the following.

Query:

{
	post(id: "cG9zdDoxMjM5NzIx") {
		blocks {
			type
			innerHtml
		}
	}
}

Response:

{
	"data": {
		"post": {
			"blocks": [
				{
					"type": "P",
					"innerHtml": "Now this is a story all about how<br>My life got flipped turned upside down<br>And I’d like to take a minute, just sit right there<br>I’ll tell you how I became the prince of a town called Bel-Air"
				},
				{
					"type": "EMBED_YOUTUBE",
					"innerHtml": ""
				},
				{
					"type": "P",
					"innerHtml": "In West Philadelphia, born and raised<br>On the playground is where I spent most of my days<br>Chillin’ out, maxin’, relaxin’ all cool<br>And all shootin’ some b-ball outside of the school<br>When a couple of guys who were up to no good<br>Started makin’ trouble in my neighborhood<br>I got in one little fight and my mom got scared<br>And said “You’re movin’ with your auntie and uncle in Bel-Air"
				},
				{
					"type": "SHORTCODE_PULLQUOTE",
					"innerHtml": "You’re movin’ with your auntie and uncle in Bel-Air"
				},
				{
					"type": "P",
					"innerHtml": "I begged and pleaded with her day after day<br>But she packed my suitcase and sent me on my way<br>She gave me a kiss and then she gave me my ticket<br>I put my Walkman on and said “I might as well kick it”<br>First class, yo, this is bad<br>Drinkin’ orange juice out of a champagne glass<br>Is this what the people of Bel-Air livin’ like?<br>Hmmm, this might be all right<br>But wait, I hear they’re prissy, bourgeois, and all that<br>Is this the type of place that they just sent this cool cat?<br>I don’t think so, I’ll see when I get there<br>I hope they’re prepared for the Prince of Bel-Air"
				},
				{
					"type": "IMG",
					"innerHtml": ""
				}
			]
		}
	}
}

We can also see the attributes for the shortcode and embed blocks by requesting the attributes field.

Query:

{
	post(id: "cG9zdDoxMjM5NzIx") {
		blocks {
			type
			attributes {
				name
				value
			}
		}
	}
}

Response:

{
	"data": {
		"post": {
			"blocks": [
				{
					"type": "P",
					"attributes": []
				},
				{
					"type": "EMBED_YOUTUBE",
					"attributes": [
						{
							"name": "url",
							"value": "https://www.youtube.com/watch?v=AVbQo3IOC_A"
						}
					]
				},
				{
					"type": "P",
					"attributes": []
				},
				{
					"type": "SHORTCODE_PULLQUOTE",
					"attributes": []
				},
				{
					"type": "P",
					"attributes": []
				},
				{
					"type": "IMG",
					"attributes": [
						{
							"name": "src",
							"value": "https://example.com/fresh-prince.jpeg"
						},
						{
							"name": "alt",
							"value": "The Fresh Prince of Bel Air"
						}
					]
				}
			]
		}
	}
}

wp-graphql-content-blocks's People

Contributors

bhammy avatar chriszarate avatar dalecreativ avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

wp-graphql-content-blocks's Issues

Getting Gutenberg Shortcode content in query

I am working on a headless WP project using Gutenberg, and trying to return the HTML content of a shortcode in my query, so I can parse it on the front end. The end goal would be to access what is between the comments here.

<!-- wp:shortcode -->
<div>[before-after overlay_opacity="0" label_one="Original" label_two="BeFunky Vibrance Tool" label_color="#1c1d1e"]<img class="alignnone size-full wp-image-27095" src="https://editors.befunky.com/wp-content/uploads/2018/05/Vibrance-Tool-3.jpg" alt="how to enhance dull colors in an image" width="1080" height="720"><img class="alignnone size-full wp-image-27097" src="https://editors.befunky.com/wp-content/uploads/2018/05/Vibrance-Tool-2.jpg" alt="how to correct color with BeFunky Vibrance tool" width="1080" height="720">[/before-after]</div>
<!-- /wp:shortcode -->

I have worked with filtering the connections array in my functions.php, registering a new GraphQL type and field

function add_post_connections( $blocks, $post ) {
	return array_map( function ( $block ) {
		if ( 'core/shortcode' === $block['type'] ) {
			$test = $block['tagName'];
		}
		return $block;
	}, $blocks );

}

function register_block_shortcode_type() {
	register_graphql_object_type('GutenShortCode', [
		'description' => 'Shortcode from GutenBerg Block',
		'fields' => [
			'html' => [ 'type' => 'String' ],
		]
	]);
}

function register_shortcode_field() {
	register_graphql_field('ShortCode', 'getShortCode', [
		'description' => __('Get a Shortcode', 'shortcode'),
		'type' => 'GutenShortCode',
		'resolve' => function($post) {
			return [
				'html' => 'shortcode content',
			];
		}
	]);
}

Digging through the plugin code and adding lots of breakpoints I can see it returns the content I want, then on this line 338 in html-block.php it not longer returns the innerHTML of my shortcode.

if ( $doc->loadHTML( '<?xml encoding="utf-8" ?>' . $html ) ) {
		$root = new HTMLBlock( null, $doc, 'root' );
		return $root;
	}

Here is what is returned from the shortcode in my query.

{
  "type": "CORE_SHORTCODE",
  "innerHtml": "[before-after overlay_opacity=”0″ label_one=”Original” label_two=”BeFunky Vibrance Tool” label_color=”#1c1d1e”]",
  "tagName": "p",
  "attributes": [],
  "connections": []
},

I realize this all may be kind of vague, so please let me know where I can be clearer. Also happy to dig into the code if I can be pointed in the right direction.

Getting internal server error using this.

In GraphiQL:

query GET_POST($id: ID!) {
  post(id: $id) {
    blocks {
      attributes {
        name
        value
      }
      innerHtml
      tagName
      type
    }
  }
}

(with valid id)

returns:

{
  "errors": [
    {
      "message": "Internal server error",
      "category": "internal",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "path": [
        "post",
        "blocks"
      ]
    }
  ],
  "data": {
    "post": {
      "blocks": null
    }
  }
}

It's not liking something in my install.

The site is loading and I'm able to get other GraphQL responses from the site so this is isolated to the plugin.

Shortcodes from Visual Composer

Hello! My posts are made with WpBakery (Visual Composer), so when I get them in WpGraphQL via wp-graphql-content-blocks I get a lot of not rendered shortcodes in output.

I solved this for content field, I registered a new field called "vsCodeShortcodesToHTML" and get there all decoded content. Then in my React component I just use this field instead of "content". To do so I added this code in my function,js:

// render js composer shortcodes to html in graphql response "addAllMappedShortcodes" field
add_action('graphql_register_types', function() {
	function vsCodeShortcodesToHTML( $post ) {
		if(class_exists('WPBMap') && method_exists('WPBMap', 'addAllMappedShortcodes')) {
			WPBMap::addAllMappedShortcodes(); 
		}
		return do_shortcode(get_post($post->ID)->post_content);
	}
	register_graphql_field('Post', 'vsComposerContentRendered', [
		'type' => 'String',
		'description' => __('the_content() VS Composer shortcodes rendered to HTML.', 'wp-graphql'),
		'resolve' => 'vsCodeShortcodesToHTML'
	]);
	register_graphql_field('Page', 'vsComposerContentRendered', [
		'type' => 'String',
		'description' => __('the_content() VS Composer shortcodes rendered to HTML.', 'wp-graphql'),
		'resolve' => 'vsCodeShortcodesToHTML'
	]);
	
});

It works for me untill I want to brake the content into blocks with your plugin. Shortcodes are still remaining.
Any thoughts?

InnerHtml is empty for UL blocks

I'm working with a standard wordpress instance and having some issues with the block's innerHtml. It works for everything except lists from what I can see. I am expected a list with 2 items.

query Post($id: ID = "") { post(idType: SLUG, id: $id) { blocks { tagName innerHtml attributes { name value } } } }

and the response:

{ "data": { "post": { "blocks": [ { "tagName": "ul", "innerHtml": "\n\n", "attributes": [] }, { "tagName": null, "innerHtml": "", "attributes": [] } ] } }, "extensions": { "debug": [ { "type": "DEBUG_LOGS_INACTIVE", "message": "GraphQL Debug logging is not active. To see debug logs, GRAPHQL_DEBUG must be enabled." } ] } }

Can't install it

Hello guys, I am kinda new to wordpress, can anyone help me figure out how to install this to wpgraphql plugin? I find it hard to deal with wpgraphql already so please forgive my ignorance.

core quote block is missing cite tag

Hello,

Thanks for creating this plugin, it's been super useful for a current project.

I was looking for some help, or to make a request to handle the core/quote block cite tags.

When using the plugin the innerHtml of the quote block seems to remove the cite tag, but it keeps the inner text from the cite tag. When disabling the plugin, or querying against post.content I see the cite tags are there, as expected. When we switch the query to use blocks the cite tags are removed from the innerHtml property again.

Is there anything that can be done to handle this so we can include the correct semantic markup for a blockquote block?

Any help or advice would be much appreciated.

Blocks dont show up in Post or any other GraphQL Query

Using WPGraphql v0.13.3 and wp-graphql-content-blocks v0.7.0 the blocks field doesn't show up in the Query for Posts (or any other Custom Post Type).

The following sample query

{
  postBy(id: "cG9zdDoxOA==") {
    title
    blocks {
      type
    }
  }
}

will throw

{
  "errors": [
    {
      "message": "Cannot query field \"blocks\" on type \"Post\".",
      "extensions": {
        "category": "graphql"
      },
      "locations": [
        {
          "line": 5,
          "column": 5
        }
      ]
    }
  ]
}

Am i missing something or are more recent versions of WPGraphQL breaking this Plugin? Whats the latest tested WPGraphQL Version for this Plugin?

ClassName is missing ??

Here's the query

query post {
  post(id: "1", idType: ID) {
    content(format: RENDERED)
    blocks {
      tagName
      innerHtml
    }
  }
}

The Output of Content String

<pre><code class=\"language-python\">\n\nfrom math import *\nprint(&quot;The Value of PI is &quot;, pi)</code></pre>

With Blocks

        {
          "tagName": "pre",
          "innerHtml": "<code>from math import *print(\"The Value of PI is \", pi)</code>"
        },

className is missing with blocks !! Any Idea ??

Can't query blocks on type post error

I have installed and activated plugin and when I test a below query in GraphiQL then I'm getting an error message:
Cannot query filed "blocks on type "post"

{
  page: postBy(uri: "hello-world") {
     title
     blocks {
	type
        innerHtml
    }
  }
}

Do I need to do anything else to make it work?

Installation instructions (or publish it to packagist or wordpress)

Do you have any plans to publish this package to the wordpress repository or to the composer repository? So I could use wpackagist or packagist to install and version-control it, instead of downloading it and commiting it to my project.

Or maybe I am missing something...

Block groups

This plugin doesn't seem to support the use of block groups of WP.

Please let me know if I'm missing something, otherwise support for block groups could be handy in some use cases..

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.