{"_id":"html-ast-transform","_rev":"2169168","name":"html-ast-transform","description":"Flexible html transformations by mapping/reducing an AST","dist-tags":{"latest":"2.0.1"},"maintainers":[{"name":"jspaine","email":"painejs@hotmail.com"}],"time":{"modified":"2022-01-05T02:57:21.000Z","created":"2017-07-28T17:30:28.952Z","2.0.1":"2019-08-17T10:57:04.055Z","2.0.0":"2019-08-17T10:43:59.222Z","1.0.0":"2017-08-04T09:02:25.774Z","0.1.0":"2017-07-28T17:30:28.952Z"},"users":{},"author":{"name":"James Paine","email":"painejs@hotmail.com"},"repository":{"type":"git","url":"git+https://github.com/jspaine/html-ast-transform.git"},"versions":{"2.0.1":{"name":"html-ast-transform","version":"2.0.1","description":"Flexible html transformations by mapping/reducing an AST","main":"dist/html-ast-transform.js","scripts":{"commit":"git-cz","lint":"tslint '{src,_test}/**/*.ts'","prebuild":"rimraf dist","build":"webpack","test:once":"mocha -r ts-node/register -r babel-register '_test/*.spec.ts'","test":"npm run test:once -- -w --watch-extensions ts","semantic-release":"semantic-release pre && npm publish && semantic-release post"},"repository":{"type":"git","url":"git+https://github.com/jspaine/html-ast-transform.git"},"keywords":["html","ast","transform"],"author":{"name":"James Paine","email":"painejs@hotmail.com"},"license":"MIT","bugs":{"url":"https://github.com/jspaine/html-ast-transform/issues"},"homepage":"https://github.com/jspaine/html-ast-transform#readme","devDependencies":{"@babel/core":"^7.5.5","@babel/preset-env":"^7.5.5","@types/chai":"^4.0.1","@types/lodash.trim":"^4.5.2","@types/mocha":"^5.2.7","@types/parse5":"^5.0.2","babel-loader":"^8.0.6","babel-register":"^6.24.1","chai":"^4.1.0","commitizen":"^4.0.3","cz-conventional-changelog":"^3.0.2","lodash.trim":"^4.5.1","mocha":"^6.2.0","parse5":"^5.1.0","rimraf":"^3.0.0","semantic-release":"^15.13.21","ts-loader":"^6.0.4","ts-node":"^8.3.0","tslint":"^5.5.0","typescript":"^3.5.3","webpack":"^4.39.2","webpack-cli":"^3.3.6"},"peerDependencies":{"parse5":"^5.0.2"},"dependencies":{},"typings":"./dist/types/index.d.ts","czConfig":{"path":"node_modules/cz-conventional-changelog"},"gitHead":"1195daf16711e6ea92321d95ca00bf406131e6e3","_id":"html-ast-transform@2.0.1","_nodeVersion":"10.16.3","_npmVersion":"6.9.0","dist":{"shasum":"404f7cac93f8aecd425ee3e381718e5e253eaded","size":27026,"noattachment":false,"key":"/html-ast-transform/-/html-ast-transform-2.0.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/html-ast-transform/download/html-ast-transform-2.0.1.tgz"},"maintainers":[{"name":"jspaine","email":"painejs@hotmail.com"}],"_npmUser":{"name":"jspaine","email":"painejs@hotmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/html-ast-transform_2.0.1_1566039423924_0.6098715195811408"},"_hasShrinkwrap":false,"publish_time":1566039424055,"_cnpm_publish_time":1566039424055},"2.0.0":{"name":"html-ast-transform","version":"2.0.0","description":"Flexible html transformations by mapping/reducing an AST","main":"dist/html-ast-transform.js","scripts":{"commit":"git-cz","lint":"tslint '{src,_test}/**/*.ts'","prebuild":"rimraf dist","build":"webpack","test:once":"mocha -r ts-node/register -r babel-register '_test/*.spec.ts'","test":"npm run test:once -- -w --watch-extensions ts","semantic-release":"semantic-release pre && npm publish && semantic-release post"},"repository":{"type":"git","url":"git+https://github.com/jspaine/html-ast-transform.git"},"keywords":["html","ast","transform"],"author":{"name":"James Paine","email":"painejs@hotmail.com"},"license":"MIT","bugs":{"url":"https://github.com/jspaine/html-ast-transform/issues"},"homepage":"https://github.com/jspaine/html-ast-transform#readme","devDependencies":{"@babel/core":"^7.5.5","@babel/preset-env":"^7.5.5","@types/chai":"^4.0.1","@types/lodash.trim":"^4.5.2","@types/mocha":"^5.2.7","@types/parse5":"^5.0.2","babel-loader":"^8.0.6","babel-register":"^6.24.1","chai":"^4.1.0","commitizen":"^4.0.3","cz-conventional-changelog":"^3.0.2","lodash.trim":"^4.5.1","mocha":"^6.2.0","parse5":"^5.1.0","rimraf":"^3.0.0","semantic-release":"^15.13.21","ts-loader":"^6.0.4","ts-node":"^8.3.0","tslint":"^5.5.0","typescript":"^3.5.3","webpack":"^4.39.2","webpack-cli":"^3.3.6"},"peerDependencies":{"parse5":"^5.0.2"},"dependencies":{},"typings":"./dist/types/index.d.ts","czConfig":{"path":"node_modules/cz-conventional-changelog"},"gitHead":"25b1b8ecf85ce74322c0679208834990687002d4","_id":"html-ast-transform@2.0.0","_nodeVersion":"10.16.3","_npmVersion":"6.9.0","dist":{"shasum":"87d985433e2a089afc16c0b2aebede27a17fa427","size":26646,"noattachment":false,"key":"/html-ast-transform/-/html-ast-transform-2.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/html-ast-transform/download/html-ast-transform-2.0.0.tgz"},"maintainers":[{"name":"jspaine","email":"painejs@hotmail.com"}],"_npmUser":{"name":"jspaine","email":"painejs@hotmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/html-ast-transform_2.0.0_1566038639077_0.8321851816636017"},"_hasShrinkwrap":false,"publish_time":1566038639222,"_cnpm_publish_time":1566038639222},"1.0.0":{"name":"html-ast-transform","version":"1.0.0","description":"Flexible html transformations by mapping/reducing an AST","main":"dist/html-ast-transform.js","scripts":{"commit":"git-cz","lint":"tslint '{src,_test}/**/*.ts'","prebuild":"rimraf dist","build":"webpack","test:once":"mocha -r ts-node/register -r babel-register '_test/*.spec.ts'","test":"npm run test:once -- -w --watch-extensions ts","semantic-release":"semantic-release pre && npm publish && semantic-release post"},"repository":{"type":"git","url":"git+https://github.com/jspaine/html-ast-transform.git"},"keywords":["html","ast","transform"],"author":{"name":"James Paine","email":"painejs@hotmail.com"},"license":"MIT","bugs":{"url":"https://github.com/jspaine/html-ast-transform/issues"},"homepage":"https://github.com/jspaine/html-ast-transform#readme","devDependencies":{"@types/chai":"^4.0.1","@types/lodash.trim":"^4.5.2","@types/mocha":"^2.2.41","babel-core":"^6.25.0","babel-loader":"^7.1.1","babel-preset-es2015":"^6.24.1","babel-register":"^6.24.1","chai":"^4.1.0","commitizen":"^2.9.6","cz-conventional-changelog":"^2.0.0","lodash.trim":"^4.5.1","mocha":"^3.4.2","parse5":"^3.0.2","rimraf":"^2.6.1","semantic-release":"^6.3.6","ts-loader":"^2.3.1","ts-node":"^3.3.0","tslint":"^5.5.0","typescript":"^2.4.2","webpack":"^3.4.1"},"peerDependencies":{"parse5":"^3.0.0"},"dependencies":{},"files":["dist","LICENSE","README.md"],"typings":"./dist/types/index.d.ts","czConfig":{"path":"node_modules/cz-conventional-changelog"},"gitHead":"105e3622dedd9867467288307ba9623200d4bb05","_id":"html-ast-transform@1.0.0","_shasum":"ec47f8beee61e19a6921b36a1386b1df5fe02c68","_from":".","_npmVersion":"3.10.10","_nodeVersion":"6.11.2","_npmUser":{"name":"jspaine","email":"painejs@hotmail.com"},"dist":{"shasum":"ec47f8beee61e19a6921b36a1386b1df5fe02c68","size":28238,"noattachment":false,"key":"/html-ast-transform/-/html-ast-transform-1.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/html-ast-transform/download/html-ast-transform-1.0.0.tgz"},"maintainers":[{"name":"jspaine","email":"painejs@hotmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/html-ast-transform-1.0.0.tgz_1501837344732_0.391353519866243"},"directories":{},"publish_time":1501837345774,"_hasShrinkwrap":false,"_cnpm_publish_time":1501837345774},"0.1.0":{"name":"html-ast-transform","version":"0.1.0","description":"Flexible html transformations by mapping/reducing nodes","main":"dist/html-ast-transform.js","scripts":{"prepublish":"rimraf dist","publish":"webpack","test":"mocha -r ts-node/register -r babel-register '_test/*.spec.ts' --watch --watch-extensions ts"},"keywords":["html","ast","transform"],"files":["dist/html-ast-transform.js"],"author":{"name":"James Paine","email":"painejs@hotmail.com"},"license":"MIT","devDependencies":{"@types/chai":"^4.0.1","@types/lodash.trim":"^4.5.2","@types/mocha":"^2.2.41","babel-core":"^6.25.0","babel-loader":"^7.1.1","babel-preset-es2015":"^6.24.1","babel-register":"^6.24.1","chai":"^4.1.0","lodash.trim":"^4.5.1","mocha":"^3.4.2","parse5":"^3.0.2","rimraf":"^2.6.1","ts-loader":"^2.3.1","ts-node":"^3.3.0","typescript":"^2.4.2","webpack":"^3.4.1"},"peerDependencies":{"parse5":"^3.0.0"},"dependencies":{},"_id":"html-ast-transform@0.1.0","_shasum":"d0f4075a5795ed0792467ab1d44cef9cbb25cc35","_from":".","_npmVersion":"3.10.10","_nodeVersion":"6.11.0","_npmUser":{"name":"jspaine","email":"painejs@hotmail.com"},"dist":{"shasum":"d0f4075a5795ed0792467ab1d44cef9cbb25cc35","size":1963,"noattachment":false,"key":"/html-ast-transform/-/html-ast-transform-0.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/html-ast-transform/download/html-ast-transform-0.1.0.tgz"},"maintainers":[{"name":"jspaine","email":"painejs@hotmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/html-ast-transform-0.1.0.tgz_1501263028024_0.6771515924483538"},"directories":{},"publish_time":1501263028952,"_hasShrinkwrap":false,"_cnpm_publish_time":1501263028952}},"readme":"# html-ast-transform\n\nA set of helpers around [parse5](https://github.com/inikulin/parse5) for transforming HTML via an AST. Allows for flexible transformations useful for eg. processing rich text editor output for email.\n\n### Features\n- Strip tags or nodes\n- Replace nodes with string(s) or a function\n- Reduce child nodes for adding/removing/modifying sibling nodes\n- Utility functions to simplify creating nodes, adding and checking for attributes\n\n## Install\n```\nnpm install --save html-ast-transform parse5\n```\n\n## Usage\n\n```js\nimport {transform, h, getAttr, hasClass} from 'html-ast-transform'\n// or es5:\nvar transform = require('html-ast-transform').transform\n// umd:\nconst {transform, h, hasClass, getAttr} = HtmlAstTransform\n\n\nconst result = transform(html, options)\n```\n\n## Examples\n### Replace paragraph tags with table rows\n```js\nconst input = '<div><p class=\"text-center\">Some text</p></div>'\n\nconst output = transform(input, {\n  replaceTags: {\n    p: node => h('tr', [], [\n      h('td', node.attrs, node.childNodes)\n    ])\n  }\n})\n\n// output = '<div><tr><td class=\"text-center\">Some text</td></tr></div>'\n```\n\n### Generate plain text version retaining links and image alt text\n```js\nconst input = '<p>Text with <a href=\"example.com\">a link</a><img alt=\"and an image\" /></p>'\n\nconst stringifyLinks = (acc, node) => {\n  if (node.nodeName !== 'a') return acc.concat(node)\n\n  const href = getAttr(node, 'href')\n  return acc.concat(\n    node.childNodes,\n    h('#text', ` [${href}]`)\n  )\n}\n\nconst getAltText = node => {\n  const alt = getAttr(node, 'alt')\n  return h('#text', alt ? ` [${alt}]` : '')\n}\n\nconst output = transform(input, {\n  replaceTags: {\n    p: '\\n',\n    img: getAltText\n  },\n  reduceAll: stringifyLinks\n})\n\n// output = '\\nText with a link [example.com] [and an image]'\n```\n\n## API\n### transform\n```ts\ntransform(input: string, {\n  replaceTags?: {\n    [tagName: string]: string | string[] | (node: Element) => Node\n  },\n  reduceAll?: (acc: Node[], node: Element, index: number, nodes: Node[]) => Node[],\n  stripContent?: string[],\n  stripTags?: string[],\n  trimWhitespace?: boolean,\n  fragment?: boolean\n})\n```\n\n**replaceTags**: A mapping of tag names to their replacements. Replacements can be a string that will replace the opening tag, an array of strings that will replace opening and closing tags or a function that receives the node and returns a replacement node.\n\n**reduceAll**: A reducer to run over the children of all nodes. Receives the accumulated childNodes, the current childNode, the current index and the list of childNodes.\n\n**stripContent**: An array of tag names that will be removed along with their contents\n\n**stripTags** An array of tag names to be removed while retaining their contents\n\n**trimWhitespace**: Handle indentation and newlines by removing whitespace only text nodes and trimming text nodes with multiple leading/trailing whitespace to a single space. *default = true*\n\n**fragment**: Parse the input as an html fragment rather than a full document. *default = true*\n\n### Helpers\n#### node factory\n```ts\nh(html: string): Node\n\nh(type: string, value: string): TextNode | CommentNode\n\nh(\n  tagName: string,\n  attrs: Attribute[],\n  childNodes: Node[]\n): Element\n```\n#### attribute helpers\n```ts\n// get the value of the named attribute, if present\ngetAttr(node: Element, name: string): string | undefined\n\n// add an attribute to an element or update if it already exists\nwithAttr(node: Element, name: string, value: string): Element\n\n// check if the element has a class\nhasClass(node: Element, name: string): boolean\n\n// add a class to an element if not already present\nwithClass(node: Element, name: string): Element\n```\n","_attachments":{},"homepage":"https://github.com/jspaine/html-ast-transform#readme","bugs":{"url":"https://github.com/jspaine/html-ast-transform/issues"},"license":"MIT"}