Giter Site home page Giter Site logo

qrac / jsxlike Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 87 KB

Converting HTML to React JSX-like syntax

Home Page: https://jsxlike.qranoko.jp

License: MIT License

HTML 6.04% CSS 15.69% TypeScript 78.27%
browser html2jsx htmltojsx japanese javascript jsx react typescript jsxlike

jsxlike's Introduction

jsxlike

Web Tool

About

HTML 文字列を React の JSX で使える構文におおよそ変換します。

  • 依存ライブラリゼロ
  • 正規表現の文字列マッチングと replace 関数のみなので軽量で高速
  • 厳密な構文処理ではないので未実装のパターンは処理されない

How To Use

$ npm i jsxlike
import jsxlike from "jsxlike"

const htmlStr = `<p class="aaa">bbb</p>`

console.log(jsxlike(htmlStr, {} /* options */))
// => <p className="aaa">bbb</p>

Options

const defaultOptions = {
  extractTags: [],
  mapAttrs: {
    class: "className",
    charset: "charSet",
    for: "htmlFor",
    tabindex: "tabIndex",
    readonly: "readOnly",
    maxlength: "maxLength",
    colspan: "colSpan",
    rowspan: "rowSpan",
    cellspacing: "cellSpacing",
    cellpadding: "cellPadding",
    usemap: "useMap",
    frameborder: "frameBorder",
    "xlink:href": "href",
  },
  styleAttrs: true,
  styleTags: true,
  scriptTags: true,
  commentTags: true,
  voidTags: ["meta", "link", "img", "input", "br", "wbr", "hr"],
  shortTags: ["*"],
  absolutePath: "",
  absoluteAttrs: {
    link: ["href"],
    script: ["src"],
    img: ["src"],
    use: ["xlink:href", "href"],
  },
}

extractTags

type: string[]

特定の HTML タグを抽出できます。例えば ["link", "script"] とすることで <link /> <script /> タグのみを出力できます。

mapAttrs

type: { [attr: string]: string }

属性名 attrvalue に置換する設定。オプションを設定するとデフォルトオプションとマージされます。

styleAttrs

type: boolean

スタイル属性を置換するか否か。

styleTags

type: boolean | "erase"

スタイルタグを置換するか否か。または erase にすることで除去します。

scriptTags

type: boolean | "erase"

スクリプトタグを置換するか否か。または erase にすることで除去します。

commentTags

type: boolean | "erase"

コメントタグを置換するか否か。または erase にすることで除去します。

voidTags

type: string[]

設定した空要素に閉じスラッシュを追加します。

shortTags

type: string[]

設定した要素に子要素がない場合は閉じタグを省略します。配列に * を含むとすべての要素が対象となります。

absolutePath

type: string

URL を設定するとルートパスが絶対パスに置換されます。

absoluteAttrs

type: { [tagName: string]: string[] }

絶対パスに置換するタグと属性を指定します。

Security

jsxlike にエスケープ処理は含まれていません。ユーザーの入力を含んだ実行結果をそのままブラウザでレンダリングすると XSS の脆弱性に繋がる可能性があります。そういった場合はレンダリング前にサニタイジングを追加してください。

License

  • MIT

Credit

jsxlike's People

Contributors

qrac avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.