UNPKG

breakdance-util

Version:
156 lines (104 loc) 5.28 kB
# breakdance-util [![NPM version](https://img.shields.io/npm/v/breakdance-util.svg?style=flat)](https://www.npmjs.com/package/breakdance-util) [![NPM monthly downloads](https://img.shields.io/npm/dm/breakdance-util.svg?style=flat)](https://npmjs.org/package/breakdance-util) [![NPM total downloads](https://img.shields.io/npm/dt/breakdance-util.svg?style=flat)](https://npmjs.org/package/breakdance-util) [![Linux Build Status](https://img.shields.io/travis/breakdance/breakdance-util.svg?style=flat&label=Travis)](https://travis-ci.org/breakdance/breakdance-util) > Utility functions for breakdance plugins. ## Install Install with [npm](https://www.npmjs.com/): ```sh $ npm install --save breakdance-util ``` ## Usage ```js var utils = require('breakdance-util'); ``` Visit the [breakdance documentation](http://breakdance.io) for more information about using and customizing breakdance. ## API ### [.block](index.js#L36) Wrapper for creating the handlers for compiling a tag that has `*.open` and `*.close` nodes, in a single function call. **Params** * `open` **{String}**: The opening tag to render * `close` **{String}**: The closing tag to render * `state` **{Object}** * `handler` **{Function}**: Visitor function to modify the node * `returns` **{undefined}** **Example** ```js breakdance.set('div', utils.block('', '')); breakdance.set('address', block('\n<address>\n', '\n</address>\n')); // optionally pass a handler function to access the "parent" node breakdance.set('abbr', block('<abbr>', '</abbr>', function(node) { var attr = utils.toAttribs(node.attribs, ['title']); if (attr) { node.open = '<abbr' + attr + '>'; } })) ``` ### [.toAttribs](index.js#L89) Stringify the `attribs` for a node. **Params** * `attribs` **{Object}**: Object of attributes to stringify * `names` **{Array}**: Array of `names` to only stringify attributes with those names. * `returns` **{String}**: Returns a string of attributes, e.g. `src="foo.jpg"` **Example** ```js var str = utils.toAttribs(node.attribs); ``` ### [.getLang](index.js#L125) Attempt to get a "language" value from the given `attribs`. Used with code/pre tags. **Params** * `attribs` **{Object}**: The `node.attribs` object * `returns` **{String}** **Example** ```js breakdance.set('code', function(node) { var lang = utils.getLang(node.attribs); // console.log(lang); }); ``` ### [.formatLink](index.js#L226) Formats the link part of a "src" or "href" attribute on the given `node`. **Params** * `node` **{Object}** * `key` **{String}**: Either `'src'` or `'href'` * `compiler` **{Object}**: Pass the breakdance compiler instance, for state and options. * `returns` **{String}** **Example** ```js // this is how <img> tags are rendered breakdance.set('img', function(node) { var attribs = node.attribs || {}; if (attribs.src) { this.emit('![' + (attribs.alt || '').trim()); this.mapVisit(node); var src = utils.formatLink(node, 'src', this); this.emit(']' + src); //=> ![foo](bar.jpg) } }) ``` ## About ### Related projects * [breakdance-checklist](https://www.npmjs.com/package/breakdance-checklist): Plugin that adds checklist rendering support to breakdance, similar to task lists in github-flavored-markdown. | [homepage](https://github.com/jonschlinkert/breakdance-checklist "Plugin that adds checklist rendering support to breakdance, similar to task lists in github-flavored-markdown.") * [breakdance](https://www.npmjs.com/package/breakdance): Breakdance is a node.js library for converting HTML to markdown. Highly pluggable, flexible and easy… [more](http://breakdance.io) | [homepage](http://breakdance.io "Breakdance is a node.js library for converting HTML to markdown. Highly pluggable, flexible and easy to use. It's time for your markup to get down.") * [generate-breakdance](https://www.npmjs.com/package/generate-breakdance): Generate a new breakdance plugin project. | [homepage](https://github.com/generate/generate-breakdance "Generate a new breakdance plugin project.") ### Contributing Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). Please read the [contributing guide](.github/contributing.md) for advice on opening issues, pull requests, and coding standards. ### Building docs _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ To generate the readme, run the following command: ```sh $ npm install -g verbose/verb#dev verb-generate-readme && verb ``` ### Running tests Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: ```sh $ npm install && npm test ``` ### Author **Jon Schlinkert** * [github/jonschlinkert](https://github.com/jonschlinkert) * [twitter/jonschlinkert](https://twitter.com/jonschlinkert) ### License Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). MIT *** _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.4.2, on February 05, 2017._