Giter VIP home page Giter VIP logo

xml-renderer's Introduction

xml-renderer

Combines XML and XPath with your components and stylesheets to render whatever in React or elsewhere. Can be used for front- or back-end rendering.

Docs & demo

Docs have been temporarily removed. As an alternative, check out;

Installation

@TODO Use in Deno @TODO Use in Node

Example in NodeJS

import React from 'react';
import { parseXmlDocument } from 'slimdom';

// Take the default export of `xml-renderer`
import { ReactRenderer } from 'xml-renderer';

// Instantiate a new object to contain rendering rules
const experience = new ReactRenderer(React.createElement);

// For text nodes you _probably_ just want to show the text content
experience.add('self::text()', ({ node }) => node.nodeValue);

// For some nodes, including the document node, you _probably_ just want to render the children
experience.add('self::node()', ({ traverse }) => traverse());

// For other nodes you may want to add a template
experience.add('self::paragraph', ({ traverse }) => <p>{traverse()}</p>);

// For some situations you may want to specify a more specific test
experience.add('self::paragraph[not(preceding-sibling::*)]', ({ traverse }) => (
	<p>
		<b>{traverse()}</b>
	</p>
));

// For some situations you may want to traverse into specific children, or add some elements of your own
experience.add('self::webpage', ({ traverse }) => (
	<div>
		<h1>My first page</h1>
		{traverse('./paragraph')}
	</div>
));

// ReactRenderer#render returns React elements, so you can use them in React like any other JS value
export default function MyXmlDocument({ xmlString }) {
	return <div className="doc-or-whatever">{xp.render(React.createElement, parseXmlDocument(xmlString))}</div>;
}

Example in Deno

import { ReactRenderer} from 'https://deno.land/x/xml_renderer/mod.ts';
// …

xml-renderer's People

Contributors

wvbe avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

xml-renderer's Issues

Consider making the Node type used throughout XmlRenderer generic

Hey there,

I've been using xml-renderer for a bit and I noticed the Node type used in a bunch of places in the codebase is of the type globalThis.Node: the browser built-in variant. This usually works out fine.

I've been using xml-renderer in combination with another DOM implementation: slimdom. At that point, if you want to use some more specific slimdom functions, such as calling XMLSerializer#serializeToString with a node that is coming out of xml-renderer, Typescript warns me about the fact that globalThis.Node is not compatible with slimdom.Node. You'll have to typecast the xml-renderer Node (which is the browser Node type) to slimdom.Node (which is what slimdom accepts).

If xml-renderer would be generic on the Node type, I could pass slimdom.Node and have more powerful typing. Other libraries, such as FontoXPath will then also pick those typings up, making it easier for me to work with xml-renderer.

What would be your thoughts?

Regard,

Martin

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.