0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

HTMLのインデント整形 prettify

Posted at

HTML ソースコードのインデントを整形したかったので、rollup してブラウザでも動くコードを確認した。ast-util-from-html-isomorphic を使うと minify 後(gzip前)で約 40KB とコンパクトだった。

import {fromHtmlIsomorphic} from 'hast-util-from-html-isomorphic'
import {format} from 'hast-util-format'
import {toHtml} from 'hast-util-to-html'

export function formatHtml(html) {
  const hast = fromHtmlIsomorphic(html)
  format(hast)
  return toHtml(hast)
}

上記を rollup したビルドでは、ブラウザ向けの hast-util-from-dom を使ってくれる。

Node.js 上では hast-util-from-html に切り替えてくれるので、同じコードのままブラウザ・Node 両対応できる。

自前で hast-util-from-parse5 を使った場合は、200KB ほどに大きくなる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?