Skip to content

Commit

Permalink
Add URLs for schemas and display them in CLI
Browse files Browse the repository at this point in the history
* Presets and tests now have URL property that is displayed on the command line and that points to the offical documentation for the items they refer to.
* Multiple URLs can be displayed if a preset is made up of other presets.
*  Schema.org schemas link to the relevant page on https://schema.org (NB: uses HTTPS URLs to make it easier to open in browers that may not support HTTP URLs)

Would resolve #18 except this feature is not yet documented.
  • Loading branch information
iaincollins committed Mar 14, 2020
1 parent 9e0e828 commit 5adf92f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
30 changes: 25 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ const _structuredDataTest = async (structuredData, options) => {
tests.push({
test: testPath,
schema: name,
url: `https://schema.org/${name}`,
type: structuredDataType || 'any',
group: name,
groups: groups,
Expand Down Expand Up @@ -235,7 +236,7 @@ const _structuredDataTest = async (structuredData, options) => {
})

// This is a recursive function scoped to this function
const _addTestsFromPresets = (presets, structuredData, tests, testsSkipped, testGroups) => {
const _addTestsFromPresets = (presets, structuredData, tests, testsSkipped, testGroups, parentPresetUrl) => {
presets.forEach(preset => {
if (!preset)
throw new Error(`Invalid preset specified`)
Expand All @@ -245,6 +246,8 @@ const _structuredDataTest = async (structuredData, options) => {

const groups = (Array.isArray(testGroups)) ? testGroups.concat(preset.name) : [preset.name]

let presetUrl = preset.url || parentPresetUrl || null

let ignorePreset = false

if (preset.conditional) {
Expand All @@ -270,6 +273,12 @@ const _structuredDataTest = async (structuredData, options) => {
skipTest = true
}

// If the test doesn't have an explict URL assigned to it but the preset does
// then assign the URL on the preset (which should point to documentation for
// preset) to the test, so it is easily acessible when displaying results.
if (!test.url && presetUrl)
test.url = presetUrl

if (skipTest) {
testsSkipped.push(test)
} else {
Expand All @@ -279,9 +288,9 @@ const _structuredDataTest = async (structuredData, options) => {
}

// If the preset has other presets associated with it, apply them too.
if (preset.presets) {
_addTestsFromPresets(preset.presets, structuredData, tests, testsSkipped, groups)
}
if (preset.presets)
_addTestsFromPresets(preset.presets, structuredData, tests, testsSkipped, groups, presetUrl)

})
}

Expand Down Expand Up @@ -663,7 +672,13 @@ const getTestsFromPreset = (preset, structuredData, testGroup) => {
test.groups = groups
if (!test.description)
test.description = test.test.replace(/(.*)?\[\d\]\./, '').replace(/"/g, '')


// If the test doesn't have an explict URL assigned to it but the preset does
// then assign the URL on the preset (which should point to documentation for
// preset) to the test, so it is easily acessible when displaying results.
if (!test.url && preset.url)
test.url = preset.url

tests.push(test)
})
})
Expand All @@ -674,6 +689,11 @@ const getTestsFromPreset = (preset, structuredData, testGroup) => {
// If preset does not have a schema, then return only the tests in the preset
preset.tests.forEach(test => {
_setTestGroup(test, preset)
// If the test doesn't have an explict URL assigned to it but the preset does
// then assign the URL on the preset (which should point to documentation for
// preset) to the test, so it is easily acessible when displaying results.
if (!test.url && preset.url)
test.url = preset.url
tests.push(test)
})
}
Expand Down
4 changes: 3 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ async function printTestResults(testResult, options) {

console.log(
` ${testGroupName}`,
(numberOfTestsInGroup) ? info(`- ${percentageTestPassed}% (${testResultsGrouped[group].passed.length } passed, ${testResultsGrouped[group].failed.length} failed)`) : ''
(numberOfTestsInGroup) ? info(`- ${percentageTestPassed}% (${testResultsGrouped[group].passed.length } passed, ${testResultsGrouped[group].failed.length} failed)`) : '',
testResultsGrouped[group].urls ? chalk.grey(`${testResultsGrouped[group].urls.map(url => `\n ${url}`)}`) : '',
"\n"
)

const showTestInfo = (test) => {
Expand Down
4 changes: 4 additions & 0 deletions lib/group-test-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ const groupTestResults = (tests) => {

if (!testResultsGrouped[testGroup]) {
testResultsGrouped[testGroup] = {
urls: [],
passed: [],
optional: [],
warnings: [],
failed: [],
}
}

if (test.url && !testResultsGrouped[testGroup].urls.includes(test.url))
testResultsGrouped[testGroup].urls.push(test.url)

if (test.optional) {
if (!testResultsGrouped[testGroup].optional.includes(test))
testResultsGrouped[testGroup].optional.push(test)
Expand Down
1 change: 1 addition & 0 deletions presets/facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const Facebook = {
name: 'Facebook',
description: 'Suggested metatags for Facebook',
url: 'https://developers.facebook.com/docs/sharing/webmasters',
tests: [
{ test: `"og:title"`, type: 'metatag', description: 'must have page title' },
{ test: `"og:type"`, type: 'metatag', description: 'must have page type' },
Expand Down
1 change: 1 addition & 0 deletions presets/google.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const DiscussionForumPosting = require('./google/schemas/CreativeWork/SocialMedi
const Google = {
name: 'Google',
description: 'Check for common markup used by Google',
url: 'https://developers.google.com/search/docs/guides/intro-structured-data',
presets: [
...Object.keys(WebPage).map(schema => WebPage[schema]),
...Object.keys(ClaimReview).map(schema => ClaimReview[schema]),
Expand Down
1 change: 1 addition & 0 deletions presets/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const Twitter = {
name: 'Twitter',
description: 'Suggested metatags for Twitter',
url: 'https://developer.twitter.com/en/docs/tweets/optimize-with-cards',
tests: [
{ test: `"twitter:card"`, type: 'metatag', description: 'must have card type' },
{ test: `"twitter:title"`, type: 'metatag', description: 'must have title' },
Expand Down

0 comments on commit 5adf92f

Please sign in to comment.