Skip to content

Commit

Permalink
feat: 🎸 allow user defined rules and prefs. Writing to json
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Reese committed May 19, 2021
1 parent cf00d06 commit 3e61283
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
20 changes: 16 additions & 4 deletions packages/seo-check/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ Works in single page mode and site wide mode.

Pro users can easily use this plugin to fire off an email to the marketing/content team any time an SEO issue is encountered.

## Checks
## SEO Checks

This plugin is very opinionated based on years of [Nick Reese](https://nicholasreese.com)'s experience running major SEO assets.

If you think the rules are too strict or opinionated this plugin supports adding your own rules.

### Sitewide

Expand Down Expand Up @@ -103,7 +107,7 @@ Once installed, open your `elder.config.js` and configure the plugin by adding `
plugins: {
'@elderjs/plugin-seo-check': {
display: ['errors', 'warnings'], // what level of reporting would you like.
handleSiteResults: async (results) => { // default.
handleSiteResults: async ({ meta, ...results }) => {
// 'results' represents all of the issues found for the site wide build.
// power users can use this async function to post the issues to an endpoint or send an email
// so that the content or marketing team can address the issues.
Expand All @@ -113,8 +117,10 @@ plugins: {
console.log(`No SEO issues detected.`);
}
},
preferences: [], // define your own preferences. See below.
rules: [], // define your own rules. This overwrites existing rules. See below.
// writeLocation: './report.json' // used to write a JSON report. Relative to the root.
},

}
```

Expand All @@ -130,6 +136,12 @@ plugins: {
}
```

For access to the default rules and preferences:

```js
const { defaultPreferences, rules } = require('@elderjs/plugin-seo-check');
```

## Contributing:

- This plugin could be extended to accept new user defined rules. It just needs to be passed in to the `new Tester({rules:['rules', 'go', 'here']})` and the library should pick them up.
- If you end up writing your own rules, if you want to write a how-to that would be great.
40 changes: 37 additions & 3 deletions packages/seo-check/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable node/no-unsupported-features/es-syntax */
const { Tester } = require('@nickreese/seo-lint');
const { Tester, defaultPreferences, rules } = require('@nickreese/seo-lint');
const fs = require('fs');
const path = require('path');

const notProd = process.env.NODE_ENV !== 'production' && process.env.NODE_ENV !== 'PRODUCTION';

Expand All @@ -18,7 +20,7 @@ const plugin = {
},
config: {
display: ['errors', 'warnings'], // what level of reporting would you like.
handleSiteResults: async (results) => {
handleSiteResults: async ({ meta, ...results }) => {
// 'results' represents all of the issues found for the site wide build.
// power users can use this async function to post the issues to an endpoint or send an email
// so that the content or marketing team can address the issues.
Expand All @@ -28,6 +30,9 @@ const plugin = {
console.log(`No SEO issues detected.`);
}
},
preferences: defaultPreferences, // define your own preferences.
rules: rules, // define your own rules. This overwrites existing rules.
// writeLocation: './report.json' // used to write a JSON report. Relative to the root.
},
hooks: [
{
Expand All @@ -50,12 +55,41 @@ const plugin = {
description: 'test',
run: async ({ settings, plugin }) => {
if (settings.context === 'build') {
// destructure for below
const { meta, ...results } = await plugin.tester.folder(settings.distDir);
plugin.config.handleSiteResults(results);
plugin.config.handleSiteResults({ meta, ...results });

if (plugin.config.writeLocation && plugin.config.writeLocation.length > 0) {
const parsedWL = path.parse(plugin.config.writeLocation);
if (parsedWL.ext !== '.json') {
throw new Error('writeLocation in config must write to a .json file.');
}

// make dir
if (!fs.existsSync(parsedWL.dir)) {
fs.mkdirSync(parsedWL.dir, { recursive: true });
}

if (Object.keys(results).length > 0) {
fs.writeFileSync(
plugin.config.writeLocation,
JSON.stringify({ success: false, meta, results }, null, 2),
{
encoding: 'utf-8',
},
);
} else {
fs.writeFileSync(plugin.config.writeLocation, JSON.stringify({ success: true, meta, results }, null, 2), {
encoding: 'utf-8',
});
}
}
}
},
},
],
defaultPreferences,
rules,
};

module.exports = plugin;
2 changes: 1 addition & 1 deletion packages/seo-check/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
},
"dependencies": {
"cheerio": "^1.0.0-rc.9",
"@nickreese/seo-lint": "^1.0.3"
"@nickreese/seo-lint": "^1.1.0"
}
}

0 comments on commit 3e61283

Please sign in to comment.