Giter Site home page Giter Site logo

shelob9 / block-content Goto Github PK

View Code? Open in Web Editor NEW
18.0 3.0 1.0 275 KB

Renders "raw" post content with WordPress block markup in it using React components, which you optionally provide.

Home Page: https://www.npmjs.com/package/@shelob9/block-content

License: MIT License

JavaScript 3.27% HTML 2.02% TypeScript 94.71%

block-content's Introduction

Block Content Renderer

CI npm

Renders "raw" post content with WordPress block markup in it using React components you optionally provide. Uses @wordpress/block-serialization-default-parser.

This works with the "raw" value returned by WordPress REST API for post title, content, excerpt, etc. You must request with ?context=edit which requires permission to edit the post.

BETA Probably don't use. An experiment by Josh Pollock.

Why / Status

WordPress parses block-based content to HTML before displaying it in a front-end theme. This HTML is also returned by the REST API and WPGraphQL. With a JavaScript front-end, in a headless site or what not, you may want to treat the block content as an object for several reasons.

  • Change the markup -- add classes to paragraphs, change element types, etc.
  • Sanitize content
  • Re-order or reformat content.

WordPress' block parser converts blocks to objects. These objects have block attributes and the inner HTML. This library will provide the ability to:

  • ✔️ Remove script and style tags
  • Set a list of allowed elements and attributes (TODO)
  • ✔️ Use your own component library to render block-based content.
  • ✔️ Use block columns.
    • Needs CSS

Install

yarn add @shelob9/block-content

https://www.npmjs.com/package/@shelob9/block-content

Usage

The <BlockContent> component parses raw blocks to React elements and renders it:

import BlockContent from "@shelob9/block-content";
const Post = ({postTitleRaw,postContentRaw}) => {
  return (
    <article>
      <BlockContent rawContent={postTitleRaw} />
      <BlockContent rawContent={postContentRaw} />
    </article>
  )
}

For saftery reasons, it's best to use <RenderBlockContent />. If given raw content, it works the same as <BlockContent>, but falls back to using rendered HTML.

import {RenderBlockContent} from "@shelob9/block-content";

const PostContent = ({post}) => {
  return (
    <article>
      <RenderBlockContent 
          raw={post.content.raw}
          rendered={post.content.rendered}
       />
    </article>
  )
}

Theme Provider

Customize Components

To provide your own custom components, wrap your app in the <ThemeProvider> and provide an object of render functions for each element you wish to customize:

import {ThemeProvider} from "@shelob9/block-content";

//A map like object of element type and a render function for each
const components = {
  p => ({children,className}) => (
    <p className={`${className} custom-class`}>{children}</p>
  ), 
}
//Wrap everything in the theme provider
const App = () => {
  return(
    <ThemeProvider components={}>
      <div>Put the rest of your app here</div>
    </ThemeProvider>
  )
}

By default, each allowed element has a basic render function. You can use this to replace a elements with a <Link> element from Gatsby or NextJS.

Restricting Element Types

By default, script and style tags are removed. The rest of this feature is not yet implimented.

Development

yarn start

This builds to /dist and runs the project in watch mode so any edits you save inside src causes a rebuild to /dist.

Then run either Storybook or the example playground:

Publish

yarn release

Storybook

Run inside another terminal:

yarn storybook

This loads the stories from ./stories.

Contribute

Copyright Josh Pollock. License: GPL 2.0 or later. Please share with your neighbor and feal free to contribute.

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.