{"id":27821509,"url":"https://github.com/rehypejs/rehype-format","last_synced_at":"2025-05-01T17:03:44.488Z","repository":{"id":45563356,"uuid":"71560014","full_name":"rehypejs/rehype-format","owner":"rehypejs","description":"plugin to format HTML","archived":false,"fork":false,"pushed_at":"2024-09-19T11:22:46.000Z","size":128,"stargazers_count":31,"open_issues_count":0,"forks_count":5,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-04-23T17:06:37.746Z","etag":null,"topics":["beautify","format","html","prettify","print","rehype","rehype-plugin"],"latest_commit_sha":null,"homepage":"https://unifiedjs.com","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rehypejs.png","metadata":{"funding":{"github":"unifiedjs","open_collective":"unified"},"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-10-21T11:37:40.000Z","updated_at":"2025-01-13T18:22:26.000Z","dependencies_parsed_at":"2024-06-18T15:35:18.631Z","dependency_job_id":"90cb83fd-77c5-4670-862c-52f5b82f7f8c","html_url":"https://github.com/rehypejs/rehype-format","commit_stats":{"total_commits":104,"total_committers":2,"mean_commits":52.0,"dds":0.009615384615384581,"last_synced_commit":"0d505500b2cf862ffa42af44ecd7527151b52a6c"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rehypejs%2Frehype-format","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rehypejs%2Frehype-format/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rehypejs%2Frehype-format/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rehypejs%2Frehype-format/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rehypejs","download_url":"https://codeload.github.com/rehypejs/rehype-format/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251912749,"owners_count":21664087,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["beautify","format","html","prettify","print","rehype","rehype-plugin"],"created_at":"2025-05-01T17:01:45.841Z","updated_at":"2025-05-01T17:03:44.431Z","avatar_url":"https://github.com/rehypejs.png","language":"JavaScript","funding_links":["https://github.com/sponsors/unifiedjs","https://opencollective.com/unified"],"categories":["Plugins"],"sub_categories":[],"readme":"# rehype-format\n\n[![Build][build-badge]][build]\n[![Coverage][coverage-badge]][coverage]\n[![Downloads][downloads-badge]][downloads]\n[![Size][size-badge]][size]\n[![Sponsors][sponsors-badge]][collective]\n[![Backers][backers-badge]][collective]\n[![Chat][chat-badge]][chat]\n\n**[rehype][]** plugin to format HTML.\n\n## Contents\n\n* [What is this?](#what-is-this)\n* [When should I use this?](#when-should-i-use-this)\n* [Install](#install)\n* [Use](#use)\n* [API](#api)\n  * [`unified().use(rehypeFormat[, options])`](#unifieduserehypeformat-options)\n  * [`Options`](#options)\n* [Examples](#examples)\n  * [Example: markdown input (remark)](#example-markdown-input-remark)\n  * [Example: tabs and blank lines (`indent`, `blanks`)](#example-tabs-and-blank-lines-indent-blanks)\n* [Types](#types)\n* [Compatibility](#compatibility)\n* [Security](#security)\n* [Related](#related)\n* [Contribute](#contribute)\n* [License](#license)\n\n## What is this?\n\nThis package is a [unified][] ([rehype][]) plugin to format whitespace in HTML.\nIn short, it works as follows:\n\n* collapse all existing whitespace to either a line ending or a single space\n* remove those spaces and line endings if they do not contribute to the\n  document\n* inject needed line endings\n* indent previously collapsed line endings properly\n\n**unified** is a project that transforms content with abstract syntax trees\n(ASTs).\n**rehype** adds support for HTML to unified.\n**hast** is the HTML AST that rehype uses.\nThis is a rehype plugin that changes whitespace in hast.\n\n## When should I use this?\n\nThis package is useful when you want to improve the readability of HTML source\ncode as it adds insignificant but pretty whitespace between elements.\nThe package [`hast-util-format`][hast-util-format] does the same as this plugin\nat the utility level.\nA different plugin, [`rehype-stringify`][rehype-stringify], controls how HTML\nis actually printed: which quotes to use, whether to put a `/` on `\u003cimg /\u003e`,\netc.\nYet another project, [`rehype-minify`][rehype-minify], does the inverse: improve\nthe size of HTML source code by making it hard to read.\n\n## Install\n\nThis package is [ESM only][esm].\nIn Node.js (version 16+), install with [npm][]:\n\n```sh\nnpm install rehype-format\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport rehypeFormat from 'https://esm.sh/rehype-format@5'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n\u003cscript type=\"module\"\u003e\n  import rehypeFormat from 'https://esm.sh/rehype-format@5?bundle'\n\u003c/script\u003e\n```\n\n## Use\n\nSay we have the following file `index.html`:\n\n```html\n\u003c!doCTYPE HTML\u003e\u003chtml\u003e\n \u003chead\u003e\n    \u003ctitle\u003eHello!\u003c/title\u003e\n\u003cmeta charset=utf8\u003e\n      \u003c/head\u003e\n  \u003cbody\u003e\u003csection\u003e    \u003cp\u003ehi there\u003c/p\u003e\n     \u003c/section\u003e\n \u003c/body\u003e\n\u003c/html\u003e\n```\n\n…and our module `example.js` looks as follows:\n\n```js\nimport rehypeFormat from 'rehype-format'\nimport rehypeParse from 'rehype-parse'\nimport rehypeStringify from 'rehype-stringify'\nimport {read} from 'to-vfile'\nimport {unified} from 'unified'\n\nconst file = await read('index.html')\n\nawait unified()\n  .use(rehypeParse)\n  .use(rehypeFormat)\n  .use(rehypeStringify)\n  .process(file)\n\nconsole.log(String(file))\n```\n\n…then running `node example.js` yields:\n\n```html\n\u003c!doctype html\u003e\n\u003chtml\u003e\n  \u003chead\u003e\n    \u003ctitle\u003eHello!\u003c/title\u003e\n    \u003cmeta charset=\"utf8\"\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    \u003csection\u003e\n      \u003cp\u003ehi there\u003c/p\u003e\n    \u003c/section\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n## API\n\nThis package exports no identifiers.\nThe default export is [`rehypeFormat`][api-rehype-format].\n\n### `unified().use(rehypeFormat[, options])`\n\nFormat whitespace in HTML.\n\n###### Parameters\n\n* `options` ([`Options`][api-options], optional)\n  — configuration\n\n###### Returns\n\nTransform ([`Transformer`][transformer]).\n\n### `Options`\n\nConfiguration (TypeScript type).\n\n###### Fields\n\n* `blanks` (`Array\u003cstring\u003e`, default: `[]`)\n  — list of tag names to join with a blank line (default: `[]`); these tags,\n  when next to each other, are joined by a blank line (`\\n\\n`); for example,\n  when `['head', 'body']` is given, a blank line is added between these two\n* `indent` (`number`, `string`, default: `2`)\n  — indentation per level (default: `2`); when number, uses that amount of\n  spaces; when `string`, uses that per indentation level\n* `indentInitial` (`boolean`, default: `true`)\n  — whether to indent the first level (default: `true`); this is usually the\n  `\u003chtml\u003e`, thus not indenting `head` and `body`\n\n## Examples\n\n### Example: markdown input (remark)\n\nThe following example shows how remark and rehype can be combined to turn\nmarkdown into HTML, using this plugin to pretty print the HTML:\n\n```js\nimport rehypeDocument from 'rehype-document'\nimport rehypeFormat from 'rehype-format'\nimport rehypeStringify from 'rehype-stringify'\nimport remarkParse from 'remark-parse'\nimport remarkRehype from 'remark-rehype'\nimport {unified} from 'unified'\n\nconst file = await unified()\n  .use(remarkParse)\n  .use(remarkRehype)\n  .use(rehypeDocument, {title: 'Neptune'})\n  .use(rehypeFormat)\n  .use(rehypeStringify)\n  .process('# Hello, Neptune!')\n\nconsole.log(String(file))\n```\n\nYields:\n\n```html\n\u003c!doctype html\u003e\n\u003chtml lang=\"en\"\u003e\n  \u003chead\u003e\n    \u003cmeta charset=\"utf-8\"\u003e\n    \u003ctitle\u003eNeptune\u003c/title\u003e\n    \u003cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1\"\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    \u003ch1\u003eHello, Neptune!\u003c/h1\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n### Example: tabs and blank lines (`indent`, `blanks`)\n\nThe following example shows how this plugin can format with tabs instead of\nspaces by passing the `indent` option and how blank lines can be added between\ncertain elements:\n\n```js\nimport rehypeFormat from 'rehype-format'\nimport rehypeParse from 'rehype-parse'\nimport rehypeStringify from 'rehype-stringify'\nimport {unified} from 'unified'\n\nconst file = await unified()\n  .use(rehypeParse)\n  .use(rehypeFormat, {blanks: ['body', 'head'], indent: '\\t'})\n  .use(rehypeStringify)\n  .process('\u003ch1\u003eHi!\u003c/h1\u003e\u003cp\u003eHello, Venus!\u003c/p\u003e')\n\nconsole.log(String(file))\n```\n\nYields:\n\n\u003c!--lint ignore no-tabs--\u003e\n\n```html\n\u003chtml\u003e\n\t\u003chead\u003e\u003c/head\u003e\n\n\t\u003cbody\u003e\n\t\t\u003ch1\u003eHi!\u003c/h1\u003e\n\t\t\u003cp\u003eHello, Venus!\u003c/p\u003e\n\t\u003c/body\u003e\n\u003c/html\u003e\n```\n\n\u003e 👉 **Note**: the added tags (`html`, `head`, and `body`) do not come from this\n\u003e plugin.\n\u003e They’re instead added by `rehype-parse`, which in document mode (default),\n\u003e adds them according to the HTML spec.\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports the additional type [`Options`][api-options].\n\n## Compatibility\n\nProjects maintained by the unified collective are compatible with maintained\nversions of Node.js.\n\nWhen we cut a new major release, we drop support for unmaintained versions of\nNode.\nThis means we try to keep the current release line, `rehype-format@5`,\ncompatible with Node.js 16.\n\nThis plugin works with `rehype-parse` version 3+, `rehype-stringify` version 3+,\n`rehype` version 5+, and `unified` version 6+.\n\n## Security\n\nUse of `rehype-format` changes whitespace in the tree.\nWhitespace in `\u003cscript\u003e`, `\u003cstyle\u003e`, `\u003cpre\u003e`, or `\u003ctextarea\u003e` is not modified.\nIf the tree is already safe, use of this plugin does not open you up for a\n[cross-site scripting (XSS)][xss] attack.\nWhen in doubt, use [`rehype-sanitize`][rehype-sanitize].\n\n## Related\n\n* [`rehype-minify`](https://github.com/rehypejs/rehype-minify)\n  — minify HTML\n* [`rehype-document`](https://github.com/rehypejs/rehype-document)\n  — wrap a fragment in a document\n* [`rehype-sanitize`](https://github.com/rehypejs/rehype-sanitize)\n  — sanitize HTML\n* [`rehype-toc`](https://github.com/JS-DevTools/rehype-toc)\n  — add a table of contents (TOC)\n* [`rehype-section`](https://github.com/agentofuser/rehype-section)\n  — wrap headings and their contents in sections\n\n## Contribute\n\nSee [`contributing.md`][contributing] in [`rehypejs/.github`][health] for ways\nto get started.\nSee [`support.md`][support] for ways to get help.\n\nThis project has a [code of conduct][coc].\nBy interacting with this repository, organization, or community you agree to\nabide by its terms.\n\n## License\n\n[MIT][license] © [Titus Wormer][author]\n\n\u003c!-- Definitions --\u003e\n\n[build-badge]: https://github.com/rehypejs/rehype-format/workflows/main/badge.svg\n\n[build]: https://github.com/rehypejs/rehype-format/actions\n\n[coverage-badge]: https://img.shields.io/codecov/c/github/rehypejs/rehype-format.svg\n\n[coverage]: https://codecov.io/github/rehypejs/rehype-format\n\n[downloads-badge]: https://img.shields.io/npm/dm/rehype-format.svg\n\n[downloads]: https://www.npmjs.com/package/rehype-format\n\n[size-badge]: https://img.shields.io/bundlejs/size/rehype-format\n\n[size]: https://bundlejs.com/?q=rehype-format\n\n[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg\n\n[backers-badge]: https://opencollective.com/unified/backers/badge.svg\n\n[collective]: https://opencollective.com/unified\n\n[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg\n\n[chat]: https://github.com/rehypejs/rehype/discussions\n\n[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c\n\n[esmsh]: https://esm.sh\n\n[npm]: https://docs.npmjs.com/cli/install\n\n[health]: https://github.com/rehypejs/.github\n\n[contributing]: https://github.com/rehypejs/.github/blob/main/contributing.md\n\n[support]: https://github.com/rehypejs/.github/blob/main/support.md\n\n[coc]: https://github.com/rehypejs/.github/blob/main/code-of-conduct.md\n\n[license]: license\n\n[author]: https://wooorm.com\n\n[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting\n\n[typescript]: https://www.typescriptlang.org\n\n[unified]: https://github.com/unifiedjs/unified\n\n[transformer]: https://github.com/unifiedjs/unified#transformer\n\n[hast-util-format]: https://github.com/syntax-tree/hast-util-format\n\n[rehype]: https://github.com/rehypejs/rehype\n\n[rehype-stringify]: https://github.com/rehypejs/rehype/tree/main/packages/rehype-stringify\n\n[rehype-sanitize]: https://github.com/rehypejs/rehype-sanitize\n\n[rehype-minify]: https://github.com/rehypejs/rehype-minify\n\n[api-options]: #options\n\n[api-rehype-format]: #unifieduserehypeformat-options\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frehypejs%2Frehype-format","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frehypejs%2Frehype-format","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frehypejs%2Frehype-format/lists"}