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

validator-nu

v2.2.2

Published

HTML5 validator using validator.nu, but not remotely

Downloads

104

Readme

Local HTML 5 Validator NU API for nodejs

Build Status devDependency Status

NPM

What this?

Validator NU is known as a backend of W3C HTML Validator, but it provides grunt task file only, it doesn't provide API for node module.

This lib provides API to validate HTML for nodeJS.

How to use ?

Theare are 2 ways to validate HTML since version 2.0.0.

Legacy Style

First one is a legacy style. It takes 3-7 secs to launch, then validate files, and finally, vnu.jar is closed. This takes time to launch for each call, but you don't need to create an instance of "class" described below.

How to call function

Since version 2.0 callback function is replaced with Q. Hence, you will need to replace callback function. For example, like this;

var vnu = require("validator-nu");
// Put HTML data, not the name of the file.
vnu.validate("html here").then(function (result) {
    // callback
    // This API returns messages array.
}).catch(function (e) {
    // Error callback
});

// If you got an error validatornu was not found,
// set vnu path to 4th parameter.
vnu.validate(
    "html here",
    undefined,
    undefined,
    "/path/to/vnu.jar"
).then(function (result) {
    // callback
    // This API returns messages array.
}).catch(function (e) {
  // Error callback
});

// To validate file(s), use validateFiles function
vnu.validateFiles(
  [
    "./test.html",
    "./test2.html"
  ],
  "/path/to/vnu.jar" // Of course this argument is optional. You need to include the file name.
).then(function (result) {
    // callback
    // This API returns messages array.
}).catch(function (e) {
  // Error callback
});

// If you have only a file to validate, this style is also acceptable:
vnu.validateFiles(
  "./test.html",
  undefined,
  undefined,
  "/usr/bin/vnu.jar" // Of course this argument is optional. You need to include the file name.
).then(function (result) {
    // callback
    // This API returns messages array.
}).catch(function (e) {
  // Error callback
});

Modern Style

Calling validate or validateFiles, vnu.jar is launched for each calls. Therefore, those functions are very slow as described above. To avoid this problem, Launching vnu.jar as a long-term process like HTTP service and using Web Interface API are needed. (And these procedures are a little-bit weird...)

Since version 2.0.0, There is a class named Vnu that launches vnu.jar as a HTTP server, and validate HTMLs.

How to call the functions

Note that, you need to ensure the server is ready. Fortunately, open method returns promise object and call resolve when the server is ready. For example, like this:

vnu = new require("validator-nu").Vnu(
  undefined,
  undefined,
  "/path/to/vnu.jar" // optional, needs to include the file name
);
// open = launch server!
vnu.open().then(function(pid) {
  console.log("validator server@pid:" + pid);
  // Validate raw data
  return vnu.validate("html input");
}).then(function (result) {
  // For result, check: https://github.com/validator/validator/wiki/Output:-JSON
  // This API returns messages array.
  console.log(result);
  // To validate file(s), use validateFiles method:
  return vnu.validateFiles(["test.html", "test2.html"]);
}).then(function (result) {
  /*
   * The result is an object structured below:
   * {
   * "file path you input validateFiles. e.g. test.html in this example": [the corresponding messages array]
   * "test2.html": [the corresponding messages array]
   * }
   */
  console.log(result);
  // If you have only a file to validate, you can also write like this:
  return vnu.validateFiles("test.html");
}).then(function (result) {
  // The result is the same as above. i.e
  /*
    {
      "test.html": [the corresponding message array]
    }
   */
  console.log(result);
  // Don't forget to call close method, or runs validation server forever.
  return vnu.close()
}).then(function() {
  // Do something after the server is closed
}).catch(function (e) {
  // Error callback
  // EEEEEEESSSSSSCCCCAAAAAPPPEEEE!!!
  process.exit(1);
});

Non-standard parameter for Java

As of 2.1.13, you can specify Non-standard parameters for Java (e.g. X prefixed option) as 2nd parameter for legacy version, and as 1st parameter for "class" version.

For example, like this:

vnu.validate("html here", {"ss": "512k"}).then(function (result) {
    // callback
    // This API returns messages array.
}).catch(function (e) {
    // Error callback
});

vnu.validateFiles("./test.html", {"ss": "512k"}).then(function (result) {
    // callback
    // This API returns messages array.
}).catch(function (e) {
  // Error callback
});

vnu = new require("validator-nu").Vnu(
  {"ss": "512k"},
  undefined,
  "/path/to/vnu.jar" // optional, needs to include the file name
);

Application-specific parameter

As of 2.1.13, you can ALSO specify application-specific commandline arguments like this example:

vnu.validate("html here", undefined, {"test": "test"}).then(function (result) {
    // callback
    // This API returns messages array.
}).catch(function (e) {
    // Error callback
});

vnu.validateFiles("./test.html", undefined, {"test": "test"}).then(function (result) {
    // callback
    // This API returns messages array.
}).catch(function (e) {
  // Error callback
});

vnu = new require("validator-nu").Vnu(
  undefined,
  {"test": "test"},
  "/path/to/vnu.jar" // optional, needs to include the file name
);

Exceptions

Because this API uses child_process.spawn and path.join, sometimes the API throws standard exceptions. In this case, you will need to check VNU Path and the target source file path...