{"id":13726936,"url":"https://github.com/posthtml/posthtml-parser","last_synced_at":"2025-05-16T09:03:08.372Z","repository":{"id":1764104,"uuid":"44296596","full_name":"posthtml/posthtml-parser","owner":"posthtml","description":"Parse HTML/XML to PostHTMLTree","archived":false,"fork":false,"pushed_at":"2024-09-19T06:10:52.000Z","size":1195,"stargazers_count":112,"open_issues_count":6,"forks_count":22,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-05-05T07:28:28.518Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/posthtml.png","metadata":{"files":{"readme":"readme.md","changelog":"changelog.md","contributing":null,"funding":".github/funding.yml","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},"funding":{"github":null,"patreon":"posthtml","open_collective":"posthtml","ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2015-10-15T05:54:12.000Z","updated_at":"2025-04-09T08:29:47.000Z","dependencies_parsed_at":"2024-01-04T19:05:50.165Z","dependency_job_id":"cd890d43-6449-4b3c-b505-a5a9be0ab92d","html_url":"https://github.com/posthtml/posthtml-parser","commit_stats":{"total_commits":208,"total_committers":20,"mean_commits":10.4,"dds":0.5432692307692308,"last_synced_commit":"e222457bb3f1e47cdfed79a968a0c819ddd1c267"},"previous_names":[],"tags_count":34,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posthtml%2Fposthtml-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posthtml%2Fposthtml-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posthtml%2Fposthtml-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posthtml%2Fposthtml-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/posthtml","download_url":"https://codeload.github.com/posthtml/posthtml-parser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254060820,"owners_count":22007908,"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":[],"created_at":"2024-08-03T01:03:32.152Z","updated_at":"2025-05-16T09:03:08.348Z","avatar_url":"https://github.com/posthtml.png","language":"TypeScript","readme":"\u003cdiv align=\"center\"\u003e\n  \u003cimg width=\"150\" height=\"150\" alt=\"PostHTML\" src=\"https://posthtml.github.io/posthtml/logo.svg\"\u003e\n  \u003ch1\u003ePostHTML Parser\u003c/h1\u003e\n  \n  Parse HTML/XML to [PostHTML AST](https://github.com/posthtml/posthtml-parser#posthtml-ast-format)\n\n  [![Version][npm-version-shield]][npm]\n  [![Build][github-ci-shield]][github-ci]\n  [![License][license-shield]][license]\n  [![Coverage][coverage-shield]][coverage]\n\u003c/div\u003e\n\n## Installation\n\n```sh\nnpm install posthtml-parser\n```\n\n## Usage\n\nInput HTML:\n\n```html\n\u003ca class=\"animals\" href=\"#\"\u003e\n  \u003cspan class=\"animals__cat\" style=\"background: url(cat.png)\"\u003eCat\u003c/span\u003e\n\u003c/a\u003e\n```\n\nParse with `posthtml-parser`:\n\n```js\nimport fs from 'fs'\nimport { parser } from 'posthtml-parser'\n\nconst html = fs.readFileSync('path/to/input.html', 'utf-8')\n\nconsole.log(parser(html))\n```\n\nResulting PostHTML AST:\n\n```js\n[\n  {\n    tag: 'a',\n    attrs: {\n      class: 'animals',\n      href: '#'\n    },\n    content: [\n      '\\n    ',\n      {\n        tag: 'span',\n        attrs: {\n          class: 'animals__cat',\n          style: 'background: url(cat.png)'\n        },\n        content: ['Cat']\n      },\n      '\\n'\n    ]\n  }\n]\n```\n\n## PostHTML AST Format\n\nAny parser used with PostHTML should return a standard PostHTML [Abstract Syntax Tree](https://www.wikiwand.com/en/Abstract_syntax_tree) (AST). \n\nFortunately, this is a very easy format to produce and understand. The AST is an array that can contain strings and objects. Strings represent plain text content, while objects represent HTML tags.\n\nTag objects generally look like this:\n\n```js\n{\n  tag: 'div',\n  attrs: {\n    class: 'foo'\n  },\n  content: ['hello world!']\n}\n```\n\nTag objects can contain three keys:\n\n- The `tag` key takes the name of the tag as the value. This can include custom tags. \n- The optional `attrs` key takes an object with key/value pairs representing the attributes of the html tag. A boolean attribute has an empty string as its value. \n- The optional `content` key takes an array as its value, which is a PostHTML AST. In this manner, the AST is a tree that should be walked recursively.\n\n## Options\n\n### `directives`\n\nType: `Array`\\\nDefault: `[{name: '!doctype', start: '\u003c', end: '\u003e'}]`\n\nAdds processing of custom directives.\n\nThe property ```name``` in custom directives can be of `String` or `RegExp` type.\n\n### `xmlMode`\n\nType: `Boolean`\\\nDefault: `false`\n\nIndicates whether special tags (`\u003cscript\u003e` and `\u003cstyle\u003e`) should get special treatment and if \"empty\" tags (eg. `\u003cbr\u003e`) can have children. If false, the content of special tags will be text only. \n\nFor feeds and other XML content (documents that don't consist of HTML), set this to `true`.\n\n### `decodeEntities`\n\nType: `Boolean`\\\nDefault: `false`\n\nIf set to `true`, entities within the document will be decoded.\n\n### `lowerCaseTags`\n\nType: `Boolean`\\\nDefault: `false`\n\nIf set to `true`, all tags will be lowercased. If `xmlMode` is disabled.\n\n### `lowerCaseAttributeNames`\n\nType: `Boolean`\\\nDefault: `false`\n\nIf set to `true`, all attribute names will be lowercased. \n\n**This has noticeable impact on speed.**\n\n### `recognizeCDATA`\n\nType: `Boolean`\\\nDefault: `false`\n\nIf set to `true`, CDATA sections will be recognized as text even if the `xmlMode` option is not enabled. \n\nIf `xmlMode` is set to `true`, then CDATA sections will always be recognized as text.\n\n### `recognizeSelfClosing`\n\nType: `Boolean`\\\nDefault: `false`\n\nIf set to `true`, self-closing tags will trigger the `onclosetag` event even if `xmlMode` is not set to `true`. \n\nIf `xmlMode` is set to `true`, then self-closing tags will always be recognized.\n\n### `sourceLocations`\n\nType: `Boolean`\\\nDefault: `false`\n\nIf set to `true`, AST nodes will have a `location` property containing the `start` and `end` line and column position of the node.\n\n### `recognizeNoValueAttribute`\n\nType: `Boolean`\\\nDefault: `false`\n\nIf set to `true`, AST nodes will recognize attribute with no value and mark as `true` which will be correctly rendered by `posthtml-render` package.\n\n\n[npm]: https://www.npmjs.com/package/posthtml-parser\n[npm-version-shield]: https://img.shields.io/npm/v/posthtml-parser.svg\n[github-ci]: https://github.com/posthtml/posthtml-parser/actions\n[github-ci-shield]: https://github.com/posthtml/posthtml-parser/actions/workflows/nodejs.yml/badge.svg\n[license]: ./LICENSE\n[license-shield]: https://img.shields.io/npm/l/posthtml-parser.svg\n[coverage]: https://coveralls.io/r/posthtml/posthtml-parser?branch=master\n[coverage-shield]: https://coveralls.io/repos/posthtml/posthtml-parser/badge.svg?branch=master\n","funding_links":["https://patreon.com/posthtml","https://opencollective.com/posthtml"],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fposthtml%2Fposthtml-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fposthtml%2Fposthtml-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fposthtml%2Fposthtml-parser/lists"}