{"id":19607873,"url":"https://github.com/mkdoc/mktransform","last_synced_at":"2025-04-27T20:32:28.412Z","repository":{"id":57298984,"uuid":"54813099","full_name":"mkdoc/mktransform","owner":"mkdoc","description":"Custom stream transformations","archived":true,"fork":false,"pushed_at":"2017-02-07T07:59:25.000Z","size":30,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-18T20:40:13.966Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mkdoc.png","metadata":{"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}},"created_at":"2016-03-27T04:47:18.000Z","updated_at":"2024-12-11T22:13:09.000Z","dependencies_parsed_at":"2022-08-26T18:12:41.169Z","dependency_job_id":null,"html_url":"https://github.com/mkdoc/mktransform","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mkdoc%2Fmktransform","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mkdoc%2Fmktransform/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mkdoc%2Fmktransform/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mkdoc%2Fmktransform/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mkdoc","download_url":"https://codeload.github.com/mkdoc/mktransform/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251204565,"owners_count":21552239,"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-11-11T10:12:44.982Z","updated_at":"2025-04-27T20:32:28.065Z","avatar_url":"https://github.com/mkdoc.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Stream Transform\n\n[![Build Status](https://travis-ci.org/mkdoc/mktransform.svg?v=3)](https://travis-ci.org/mkdoc/mktransform)\n[![npm version](http://img.shields.io/npm/v/mktransform.svg?v=3)](https://npmjs.org/package/mktransform)\n[![Coverage Status](https://coveralls.io/repos/mkdoc/mktransform/badge.svg?branch=master\u0026service=github\u0026v=3)](https://coveralls.io/github/mkdoc/mktransform?branch=master)\n\n\u003e Inject custom stream transformations\n\nAccepts a list of functions that should return stream classes to be injected into the transformation pipeline.\n\nThe [highlight][mkhighlight] transform implementation serves as a good example; it highlights code blocks with info strings and rewrites the document stream.\n\n## Install\n\n```\nnpm i mktransform --save\n```\n\nFor the command line interface install [mkdoc][] globally (`npm i -g mkdoc`).\n\n---\n\n- [Install](#install)\n- [Usage](#usage)\n- [Example](#example)\n- [Stream Functions](#stream-functions)\n- [Help](#help)\n- [API](#api)\n  - [transform](#transform)\n- [License](#license)\n\n---\n\n## Usage\n\nCreate a file for the transform function like [upper.js](https://github.com/mkdoc/mktransform/blob/master/doc/upper.js):\n\n```javascript\n// converts level one headings to upper case\nfunction upper(through, ast) {\n  var Node = ast.Node\n    , collect = ast.NodeWalker.collect\n    , i\n    , text;\n\n  function transform(chunk, encoding, cb) {\n    if(Node.is(chunk, Node.HEADING) \u0026\u0026 chunk.level === 1) {\n      text = collect(chunk, Node.TEXT);\n      for(i = 0;i \u003c text.length;i++) {\n        text[i].literal = text[i].literal.toUpperCase();\n      }\n    }\n    this.push(chunk);\n    cb();\n  }\n\n  return through.transform(transform);\n}\n\nmodule.exports = upper;\n```\n\nAnd pass in the transform function:\n\n```javascript\nvar tfm = require('mktransform')\n  , ast = require('mkast')\n  , upper = require('./upper');\n\nast.src('# Project\\n\\nThis is a paragraph.\\n\\n## Install')\n  .pipe(tfm(upper))\n  .pipe(ast.stringify({indent: 2}))\n  .pipe(process.stdout);\n```\n\n## Example\n\nRun a custom stream transformation:\n\n```shell\nmkcat README.md | mktransform doc/upper.js | mkout\n```\n\nRun multiple transformations:\n\n```shell\nmkcat README.md | mktransform test/fixtures/upper1.js test/fixtures/upper2.js | mkout\n```\n\n## Stream Functions\n\nA stream function has the signature:\n\n```javascript\nfunction(through, ast, opts)\n```\n\nIt is passed the [through][] module so you can easily create stream transform classes and [ast][mkast] so you may easily inspect nodes; the `opts` object is the original options object. The function **must** return a transform stream subclass.\n\nThe input and output data should always be abstract syntax tree nodes.\n\nTo create a transform stream subclass:\n\n```javascript\nfunction transformer(through) {\n\n  function transform(chunk, encoding, cb) {\n    // pass through stream\n    cb(null, chunk);\n  }\n\n  // return the stream subclass\n  return through.transform(transform);\n}\n\nmodule.exports = transformer;\n```\n\nIf you also need a `flush` function:\n\n```javascript\nfunction transformer(through) {\n\n  function transform(chunk, encoding, cb) {\n    cb(null, chunk);\n  }\n\n  function flush(cb) {\n    cb(); \n  }\n\n  return through.transform(transform, flush);\n}\n\nmodule.exports = transformer;\n```\n\nTo use a specific constructor:\n\n```javascript\nfunction transformer(through) {\n\n  function Component(opts) {\n    this.opts = opts || {}; \n  }\n\n  function transform(chunk, encoding, cb) {\n    cb(null, chunk);\n  }\n\n  return through.transform(transform, {ctor: Component});\n}\n\nmodule.exports = transformer;\n```\n\nSee [through][through], [ast][mkast] and the [api docs](#api) for more detail.\n\n## Help\n\n```\nUsage: mktransform [files...]\n\n  Custom stream transformations.\n\nOptions\n  -h, --help              Display help and exit\n  --version               Print the version and exit\n\nmktransform@1.0.5\n```\n\n## API\n\n### transform\n\n```javascript\ntransform(opts[, cb])\n```\n\nInjects custom stream transform classes into the pipeline.\n\nAccepts a single function, array of functions or an object with a\n`transforms` array.\n\nFunctions have the signature:\n\n```javascript\nfunction(through, ast, opts)\n```\n\nThey are passed the [through][] and [ast][mkast] modules to help with\ncreating stream subclasses and inspecting nodes and the original options.\n\nIf you are using multiple stream transformations the options are shared\nbetween them so take care to avoid name collisions.\n\nEach function **must** return a transform stream subclass.\n\nThe returned subclass is instantiated and when multiple transform functions\nare being used a pipeline is created between the streams in the order\nsupplied.\n\nWhen a single stream is being created it is returned otherwise an array\nof all the created streams is returned.\n\nIf both the `input` and `output` options are given additional wrapper\nstreams are created that parse JSON from the input stream and write JSON\nto the output stream, in this instance you may pass a callback function\nwhich is added as a listener for the `error` and `finish` events on the\noutput stream; the output stream is returned.\n\nReturns an output stream or array of streams.\n\n* `opts` Function|Array|Object processing options.\n* `cb` Function callback function.\n\n#### Options\n\n* `transforms` Array list of transform functions.\n* `input` Readable input stream.\n* `output` Writable output stream.\n\n#### Throws\n\n* `TypeError` if the target is not a function.\n* `TypeError` if the return value is not a function.\n* `TypeError` if the stream instance has no pipe function.\n\n## License\n\nMIT\n\n---\n\nCreated by [mkdoc](https://github.com/mkdoc/mkdoc) on April 18, 2016\n\n[mkdoc]: https://github.com/mkdoc/mkdoc\n[mkast]: https://github.com/mkdoc/mkast\n[mkhighlight]: https://github.com/mkdoc/mkhighlight\n[through]: https://github.com/tmpfs/through3\n[commonmark]: http://commonmark.org\n[jshint]: http://jshint.com\n[jscs]: http://jscs.info\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmkdoc%2Fmktransform","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmkdoc%2Fmktransform","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmkdoc%2Fmktransform/lists"}