Git Product home page Git Product logo

wp-block-converter's Introduction

WP Block Converter

Coding Standards Testing Suite

Convert HTML into Gutenberg Blocks with PHP

Installation

You can install the package via composer:

composer require alleyinteractive/wp-block-converter

Usage

Use this package like so to convert HTML into Gutenberg Blocks:

use Alley\WP\Block_Converter\Block_Converter;

$converter = new Block_Converter( '<p>Some HTML</p>' );

$blocks = $converter->convert();

Filtering the Blocks

The blocks can be filtered on a block-by-block basis or for an entire HTML body.

wp_block_converter_block

Filter the generated block for a specific node.

use Alley\WP\Block_Converter\Block;

add_filter( 'wp_block_converter_block', function ( Block $block, \DOMElement $node ): ?Block {
	// Modify the block before it is serialized.
	$block->content = '...';
	$block->blockName = '...';
	$block->attributes = [ ... ];

	return $block;
}, 10, 2 );

wp_block_converter_document_html

Filter the generated blocks for an entire HTML body.

add_filter( 'wp_block_converter_document_html', function( string $blocks, \DOMNodeList $content ): string {
	// ...
	return $blocks;
}, 10, 2 );

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

This project is actively maintained by Alley Interactive. Like what you see? Come work with us.

License

The GNU General Public License (GPL) license. Please see License File for more information.

wp-block-converter's People

Contributors

dependabot[bot] avatar github-actions[bot] avatar guzmandrade-wds avatar kevinfodness avatar srtfisher avatar

Stargazers

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

Watchers

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

Forkers

guzmandrade-wds

wp-block-converter's Issues

Some block conversion issues identified

Description

First, thank you for this library, it's an awesome idea.

Working with it, I identified some minor issues dealing with images:

  • Function upload_image should return an URL, but instead is returning an attachment ID
  • remove_image_args has issues when the image URLs have formats such as //domain.../image, http://localhost:8888, which can be an issue when working with some CDNs or local development environments, for example.
  • The converter method img might fail, and the return would be WP_Error, and maybe a try-catch would be useful.
  • the Block constructor requires block_name to be of type string but the WordPress Core function get_comment_delimited_block_content can receive null as block_name, where the block content would be returned.

Use Case

As mentioned, the source images might have URLs with format //domain.../image, http://localhost:8888/.../image but remove_image_args would fail.

If the Blocks constructor accepted block_name as null, it would be possible to return a group of blocks directly, for example to return the blocks from a registered pattern.

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.