Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jahilldev/preact-parser
Avoid use of dangerouslySetInnerHTML with this lightweight (2KB) function. Can parse HTML strings into VDom trees, ready to render in your Preact components
https://github.com/jahilldev/preact-parser
browser dangerouslysetinnerhtml html isomorphic jsx parser preact render server string
Last synced: 2 months ago
JSON representation
Avoid use of dangerouslySetInnerHTML with this lightweight (2KB) function. Can parse HTML strings into VDom trees, ready to render in your Preact components
- Host: GitHub
- URL: https://github.com/jahilldev/preact-parser
- Owner: jahilldev
- License: mit
- Created: 2022-01-19T12:44:55.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-10-30T09:31:45.000Z (about 1 year ago)
- Last Synced: 2024-10-06T11:59:02.921Z (3 months ago)
- Topics: browser, dangerouslysetinnerhtml, html, isomorphic, jsx, parser, preact, render, server, string
- Language: TypeScript
- Homepage:
- Size: 117 KB
- Stars: 9
- Watchers: 3
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# preact-parser
When dealing with HTML strings in Preact, our only real option is to use `dangerouslySetInnerHTML`. This is fine(-ish) if you 100% trust the contents of that HTML, but regardless, it opens up potential vectors for attack, problems and bugs. Ideally, we'd be able to sanitise, and convert this HTML into VDom nodes that can be natively rendered in the same manner as defined JSX or `h()` calls.
This lightweight package (**2KB** GZipped) accepts an HTML string (doesn't have to contain HTML, can be text), parses it, and returns a tree of VDom nodes ready to render by Preact. It can work both on the client (Dom Parser) and the server, so is ideal for isomorphic applications.
It automatically strips `` tags, so you no longer have to worry about someone "accidentally" adding an `alert('Hello')` in your CMS / API of choice.
# Getting Started
Install with Yarn:
```bash
$ yarn add preact-parser
```Install with NPM:
```bash
$ npm i preact-parser
```# Using parse()
`preact-parser` exports a single function, `parse()`, that accepts a string of HTML or text, and can be used directly within your component trees. As mentioned above, in the browser it makes use of the native DOM parser, and on the server uses a tiny, efficient string parser.
For example:
```jsx
import { parse } from 'preact-parser';/*[...]*/
const htmlApiResponse = `
Contrary to popular belief, Lorem Ipsum is not simply random text.
There are many variations of passages of Lorem Ipsum available
alert('Gotcha!');
`;/*[...]*/
function BlogContent() {
return {parse(htmlApiResponse)};
}
```When rendered, the above will output:
```html
Contrary to popular belief, Lorem Ipsum is not simply random text.
There are many variations of passages of Lorem Ipsum available
```# Acknowledgement
The server side HTML string parser in this package takes inspiration from the fantastically fast `node-html-parser`. That package provides a full DOM representation, including methods, which was overkill for this. In order to keep the size of `preact-parser` to a minimum, we've made use of the excellent parsing function found there.