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

gordon_lin-sb-seo

v1.0.8

Published

seo challenge

Downloads

4

Readme

SEO challenge

Usage

var { seo, exists, nestedExists, occurrence } = require('gordon_lin-sb-seo')

let html = __basedir + '/resources/html/input.html'

seo.readFile(html)
    .createCriteria()
    .useDefault()
    .execute().then(
        ouput => ouput.printf()
    )

Output

<head> dosen't have <title /> tag
<head> dosen't have <meta name="descriptions" content="anycontent..." /> tag
<head> dosen't have <meta name="keywords" /> tag
There are 3 <img> tag without alt attribute.
There are 1 <a> tag without rel attribute.
This HTML has less than 15 <strong> tag.
This HTML has more than 1 <h1> tag.

Read/Write File or output on Console

  • run execute() first, and get Output object from Promise.
  • you can chain printf() or writeFile() at will.
  • File Input/Output
  1. can read file from file path or Readable stream
  2. write file can be file path or WriteStream
let html = __basedir + '/resources/html/input.html'
let seoOut = __basedir + '/resources/html/output.html'

let readStream = fs.createReadStream(html, {
    flags: 'r',
    encoding: 'utf8',
    fd: null,
    mode: 0o666,
    autoClose: true,
    highWaterMark: 64 * 1024
})

let writeStream = fs.createWriteStream(seoOut, {
    flags: 'w',
    encoding: 'utf8',
    fd: null,
    mode: 0o666,
    autoClose: true
})
seo.readFile(html)
    .createCriteria()
    .useDefault()
    .execute().then(
        ouput => ouput.writeFile(writeStream)
    )
  • Console Output
seo.readFile(html)
    .createCriteria()
    .useDefault()
    .execute().then(
        ouput => ouput.printf()
    )

Define Constraints

p.s: run createCriteria() to create constraint block in background and then invoke useDefault() or addConstraint() to define rules.

Pre-defined Rules

  • useDefault() you still can chain your own rules to the end by addConstraint()
1. Detect if any <img /> tag without alt attribute
2. Detect if any <a /> tag without rel attribute
3. In <head> tag
  1) Detect if header doesn’t have <title> tag
  2) Detect if header doesn’t have <meta name=“descriptions” … /> tag
  3) Detect if header doesn’t have <meta name=“keywords” … /> tag
4. Detect if there’re more than 15 <strong> tag in HTML (15 is a value should be
configurable by user)
5. Detect if a HTML have more than one <H1> tag.
seo.readFile(html)
    .createCriteria()
    .useDefault()
    .execute().then(
        ouput => ouput.printf()
    )
  • Default with 5 pre-defined rules
seo.prototype.useDefault = function() {
    this.addConstraint(new nestedExists({
            tag: "head",
            exists: {
                tags: [{
                    tag: "title"
                }, {
                    tag: "meta",
                    attrs: [{
                        attr: "name",
                        value: "descriptions"
                    }, {
                        attr: "content",
                        value: "anycontent..."
                    }]
                }, {
                    tag: "meta",
                    attrs: [{
                        attr: "name",
                        value: "keywords"
                    }]
                }]
            }
        }))
        .addConstraint(new exists({
            tag: "img",
            exists: {
                attrs: [{
                    attr: "alt"
                }]
            }
        }))
        .addConstraint(new exists({
            tag: "A",
            exists: {
                attrs: [{
                    attr: "rel"
                }]
            }
        }))
        .addConstraint(new occurrence({
            tag: "strong",
            occurrence: {
                gt: 15
            }
        }))
        .addConstraint(new occurrence({
            tag: "h1",
            occurrence: {
                gt: 1
            }
        }))
    return this;
}

Contraints Class

  1. exists: detect multiple attributes within single tag is exist or not.
    {
        tag: "img",
        exists: { attrs: [{ attr: "alt" }] }
    }

e.g. Detect if any <img/> tag without alt attribute

  1. nestedExists: detect nested tags and attributes within target tag is exist or not.
{
    tag: "head",
    exists: {
        tags: [{
            tag: "meta",
            attrs: [{
                attr: "name",
                value: "descriptions"
            }]
        }]
    }
}

e.g. In tag, detect if header doesn’t have <meta name=“descriptions” … /> tag

  1. ocurrence: detect ocurrence count of tag or attribute is greater or less than digits.
{
    tag: "strong",
    occurrence:{gt:15}
}

e.g. Detect if there’re more than 15 <strong> tag in HTML

Develop Custom Constraint Class

SeoRuleHandler.prototype.onTag = function(name) {}
SeoRuleHandler.prototype.onAttrs = function(attr) {}
SeoRuleHandler.prototype.onTagClose = function(name) {}
SeoRuleHandler.prototype.printf = function() {}
  1. Constructor naming must be start with 'SeoRule'.
  2. implement onTag, onAttrs, onTagClose, printf function
  3. onTag(), onAttrs(), onTagClose() will be invoked in sequence. e.g. will be parsing to onTag(name)=>meta and onAttrs(attr)=>{name:'abc', value:'xyz'}
  4. printf() will be invoked at the last of the execute().