question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve types for Html2React processors

See original GitHub issue

Description

Right now, using TypeScript when declarating a processor for the Html2React package has some problems (see discussion here: https://github.com/frontity/frontity/commit/f7f2e6eab85889465764ae70d1fe73364db5b0ad#r37684196):

  • node type is not inferred correctly and a type guard is needed to consider it of type Element.
  • parent and children nodes cannot be typed with the current implementation. Also, the default props type of this nodes is unknown which is not a good default type.

Possible solution

To solve this issue, the idea is to pass not only the types of the props but the type of the element as well and its parent and children attributes. Some of this attributes could be optional and the final types would be the passed types merged with a default type.

E.g.

type LazyImgWrapper = {
  type: "element";
  component: "figure";
  children: [LazyImg];
}

type LazyImg = {
  type: "element";
  component: "img";
  props: { "data-src": string };
  parent: LazyImgWrapper;
};

const myProcessor: Processor<LazyImg> = {
  test: ({ node }) => { },
  processor: ({ node }) => {
    node.props["data-src"] // <- this should be of type `string`
    node.parent.component // <- this should be of type `"figure"`
  }
};

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
iamuchejudecommented, Apr 16, 2020

We’d have to refractor the definition of some of the interfaces. The reason is that we have a problem with inferring the types of.

The proposed approach to solving this issue include replacing the default type definitions, exposing the interfaces for Element, Text and Comment so users can define custom interface be made by extending them.

import { Element, ElementProps, Processor } from "@frontity/html2react/types.ts";

// You could create a new interface that extends `Element`:
interface CustomElement extends Element {
	props: ElementProps & {
		isAuthenticated: boolean;
	}
}

const userProcessor: Processor<CustomElement> = {
	...
	processor: ({ node }) {
		node.props.css = css``; // this is available as a result of the union with the ElementProps
		node.props.isAuthenticated = true;
		return node;
	} 
}

// Or you could pass a custom element definition
type CustomElementType = {
	type: "element";
	props: ElementProps<HTMLIFrameElement> & {
		src: string;
	}
}


const iframeProcessor: Processor<CustomElementType> = {
	...
	processor: ({ node }) {
		node.iframe.title = "Hello"; // title is an HTML iframe prop
		node.props.css = css``; // this is also going to be available
		return node;
	} 
}

@DAreRodz is currently working on these features and will keep working on it till it’s done. So, if you have any questions, please, do ask him.

1reaction
luisherranzcommented, Apr 9, 2020

@darerodz, I have assigned this to you as well, as you are going to work on this together.

Could you please keep here a record of what’s going on?

  • The problems you’re facing.
  • The approach that you tried and it’s not working, and why it’s not working.
  • The ideas you have in mind to solve it.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Working with processors - Frontity Docs
The processor property is a function that in some way "processes" the received node (remember that a node is a JavaScript object representing...
Read more >
frontity/html2react - api-reference - GitHub
This package is in charge of converting HTML to React. It works with processors that match HTML portions and replaces them with React...
Read more >
Frontity Talks 2020-05 - Contact Form 7 & Processors - YouTube
In this episode of #FrontityTalks we cover two topics. Michael demonstrates how to use beforeSSR to pre-fetch data, in this case a contact ......
Read more >
Connecting Gutenberg and Frontity - DevPress - CSDN
In the same way that WordPress usage is increasing each year and is the ... import { Element,Processor } from "@frontity/html2react/types"; ...
Read more >
How to build a headless WordPress website with Frontity ...
The current Frontity state is used to know which post type * should be rendered. ... libraries.html2react.processors array. <Content>
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found