npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dox-ray

v0.6.1

Published

Parse documentation from code comments

Downloads

25

Readme

Dox-ray

Dox-ray is a node module that can parse special code comments and return an array of objects containing document/code pairs. Comments are written in YAML and parsed into structured objects. The YAML structure is up to you. You define the documentation properties that's right for your code. Dox-ray can also write to a JS or JSON file which you can use to build completely client-side documentation sites that won't slow down your task runner.

Note that this project is currently in Beta.

Getting started

Install

$ npm install dox-ray

Usage (as a node module)

First, you'll need a file to parse

Here's how you write a Dox-ray comment:

styles.less:

/* doxray
label: Button
markup: <button class="btn">Button</button>
notes:
  - >
    Don't use anchor elements as buttons unless they actually link to
    another page."
*/
.btn {
    font-size: unit(14px / 16px, em);
}

Now set up Dox-ray to parse stuff

var doxray = require('dox-ray');
var docs = doxray(['styles.less']);

In the above example, docs is equal to the following:

[
  {
    "label": "Button",
    "markup": "<button class=\"btn\">Button</button>,"
    "notes": [ "Don't use anchor elements as buttons unless they actually link to another page." ],
    "filename": "styles.less",
    "less": ".btn {\nfont-size: unit(14px / 16px, em);\n}"
  }
]
You can also save it to a JS or JSON file
var docs = doxray(['styles.less'], {
  jsFile: 'styles.js',
  jsonFile: 'styles.json'
});

styles.js:

Doxray = {
  "patterns": [
    {
      "label": "Button",
      "markup": "<button class=\"btn\">Button</button>,"
      "notes": [ "Don't use anchor elements as buttons unless they actually link to another page." ],
      "filename": "styles.less",
      "less": ".btn {\nfont-size: unit(14px / 16px, em);\n}"
    }
  ],
  "getByProperty": function ( property, value ) {
    return this.patterns.filter(
      this.utils.hasProperty( property, value )
    );
  },
  "utils": {
    "hasProperty": function ( property, value ) {
      return function( pattern ) {
        if ( typeof value === 'undefined' ) {
          return pattern[ property ];
        } else {
          return pattern[ property ] && pattern[ property ].toLowerCase() === value.toLowerCase();
        }
      }
    }
  }
}

styles.json:

[
  {
    "label": "Button",
    "markup": "<button class=\"btn\">Button</button>,"
    "notes": [ "Don't use anchor elements as buttons unless they actually link to another page." ],
    "filename": "styles.less",
    "less": ".btn {\nfont-size: unit(14px / 16px, em);\n}"
  }
]

Dox-ray comment formatting

In order to make the regex simple, Dox-ray comments must start with an opening comment, a space, then the word "doxray". The closing comment must be on a new line.

<!-- doxray
label: my pattern
description: this is how you structure my pattern
-->

Supported comments

| Style | Example | | ----- | ------- | | CSS/JS | /* */ | | HTML | <!-- --> |

You can easily add more by extending Doxray.prototype.regex. See https://github.com/himedlooff/dox-ray/blob/master/doxray.js#L144-L155

YAML structure

You can structure the YAML within the Dox-ray comments however you want but there are a few top level property names that are reserved. They are:

  • filename
  • (any file type you want to parse, for example css, less, md, js, html, etc)

The built-in Dox-ray processors will also add the following extra top level properties:

  • colorPalette
  • label

You can disable these properties from getting generated by disabling the processors before running Dox-ray. For example

var doxray = require('dox-ray');
var docs = doxray(['styles.less'], { processors: [] });

Processors

Once Dox-ray parses the data it can run processing functions to manipulate the data. Dox-ray runs two processors out of the box.

Slugify

If you use the label property in your Dox-ray comment the Slugify processor will use that value to create a slug property. Slugs are useful for creating HTML id's so you can link to specific sections of a page.

For example, this comment:

/* doxray
label: Primary Button
*/

Will automatically parse to this:

{
  label: "Primary Button",
  slug: "primary-button"
}

Color Palette

Dox-ray will generate color palette data automatically if you specify a colorPalette property in your Dox-ray comment. All you need to do is set the value of the colorPalette property to the file type that contains variable/color pairs. Note that this only works when using a preprocessor like SASS or Less.

For example, this comment:

/* doxray
colorPalette: less
*/
@white: #fff;
@black: #000;

Will automatically parse to this:

{
  colorPalette: [
    { variable: "@white", value: "#fff" },
    { variable: "@black", value: "#000" }
  ]
}

Getting involved

Feedback and contributions are welcome. Please read CONTRIBUTING.

When submitting a pull request that changes or adds functionality please update the tests and run:

$ npm test

To file a bug please us this handy template.


Open source licensing info

This projected is licensed under the terms of the MIT license.


Credits and references

This project was inspired by topdoc. :smile: