Skip to content

jozefini/codesync-kses

Repository files navigation

Equivalent of wp_kses / wp_kses_post in JavaScript/Node.js

Safely render HTML in React or any other JavaScript environment. Works in both Node.js and the browser.

Example JavaScript

import { kses } from '@codesync/kses'

const unsafeHtml = '<strong>Test parser</strong><script>alert("hello")</script>'
const safeHtml = kses(unsafeHtml)
// Output: <strong>Test parser</strong>

Example React

'use client'
import { kses } from '@codesync/kses'

export default function ClientPage() {
  const unsafeHtml =
    '<strong>Test parser</strong><script>alert("hello")</script>'
  const safeHtml = kses(unsafeHtml)

  // Output: <div><strong>Test parser</strong></div>
  return <div dangerouslySetInnerHTML={{ __html: safeHtml }} />
}

Example RSC

import { kses } from '@codesync/kses/server'

export default function ServerPage() {
  const unsafeHtml =
    '<strong>Test parser</strong><script>alert("hello")</script>'
  const safeHtml = kses(unsafeHtml)

  // Output: <div><strong>Test parser</strong></div>
  return <div dangerouslySetInnerHTML={{ __html: safeHtml }} />
}

About

Equivalent helper `wp_kses` written in JavaScript

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published