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

code-part

v1.1.1

Published

Parts comments and code with htmlParser2 for html and line based parsing (like docco) for everything else

Downloads

16

Readme

code-part

npm version Build Status Coverage Status Dependency Status devDependency Status

Parts comments and code into a data structure with htmlParser2 for html and line based comment parsing for everything else. code-part tracks starting line numbers for each code section so it's possible to add line numbers if you plan to use a syntax highlighter like google-code-prettify to display your code.

code-part's code for line based parsing was modified from docco. This code base does not include docco. The only current dependencies are htmlParser2 and lodash.

Comment support for everything except html only extracts comments at the beginning of the line and only the single line version of the languages comment. For example /* not extracted */ does not get extracted but // extracted does.

To see a list of the languages supported by the line based parsing have a look at resources/languages.json.

Usage


  var part = require('code-part');

  // Path is used to decide which parser to use
  // (html or lineBased currently) and decides comment
  // parsing in lineBased.
  var sections = part(path, code, config);

  // If code is null, the specified path is assumed
  // to be a path on the file system and is read in
  // with `readFileSync`.

Configuration


  var section = part(path, code, {
    // Options used when parsing html.
    // By default the parser will skip comments that start with
    // `<!--[`. Set to `true` to include these as comments.
    noSkipDirectives: false, // default

    // Used instead of path's extension when determining
    // the parser (html or lineBased). Also used in the
    // lineBased parser.
    // when looking up comment markers and deciding if it
    // is literate (litcoffee).
    extension: '.css'
  });

Output

  • input:
// comment 1
var code = 1;
// comment 2
if (code) code += 1
  • output:
[ { docsText: 'comment 1\n',
    docsLine: 1,
    codeText: 'var code = 1;\n',
    codeLine: 2 },
  { docsText: 'comment 2\n',
    docsLine: 3,
    codeText: 'if (code) code += 1\n',
    codeLine: 4 } ]
  • input:
<html>
  <!-- title part -->
  <head><title> title </title></head>
<body>
  <!-- main body -->
  <h1>hello world</h1>
<!-- the end -->
</body>
</html>

output:

[ { docsText: '',
    docsLine: 1,
    codeText: '<html>\n  ',
    codeLine: 1 },
  { docsText: 'title part',
    docsLine: 2,
    codeText: '\n  <head><title> title </title></head>\n<body>\n  ',
    codeLine: 3 },
  { docsText: 'main body',
    docsLine: 5,
    codeText: '\n  <h1>hello world</h1>\n',
    codeLine: 6 },
  { docsText: 'the end',
    docsLine: 7,
    codeText: '\n</body>\n</html>\n',
    codeLine: 8 } ]

BUGS

It is possible to use jade, but multi-line comments are not supported.

Supported:


html
  // comment 1
  head
    // comment 2
    title A Title

Not Supported:

html
  //
    This is a
    multi-line comment
    and is not supported
  head
    title Another Title

TODO

  • Multi-line comment parsing like jade comments.

LICENSE

Copyright (C) 2014 Scott Beck, all rights reserved

Licensed under the MIT license