From 5adf92f996bbbeaf4bd92a085f33298abf81e5d1 Mon Sep 17 00:00:00 2001 From: Iain Collins Date: Sat, 14 Mar 2020 04:52:54 +0000 Subject: [PATCH] Add URLs for schemas and display them in CLI * 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. --- index.js | 30 +++++++++++++++++++++++++----- lib/cli.js | 4 +++- lib/group-test-results.js | 4 ++++ presets/facebook.js | 1 + presets/google.js | 1 + presets/twitter.js | 1 + 6 files changed, 35 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index c00cdb4..0b730ea 100644 --- a/index.js +++ b/index.js @@ -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, @@ -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`) @@ -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) { @@ -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 { @@ -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) + }) } @@ -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) }) }) @@ -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) }) } diff --git a/lib/cli.js b/lib/cli.js index 38bc4df..ab74339 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -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) => { diff --git a/lib/group-test-results.js b/lib/group-test-results.js index b77cc16..665cb96 100644 --- a/lib/group-test-results.js +++ b/lib/group-test-results.js @@ -8,6 +8,7 @@ const groupTestResults = (tests) => { if (!testResultsGrouped[testGroup]) { testResultsGrouped[testGroup] = { + urls: [], passed: [], optional: [], warnings: [], @@ -15,6 +16,9 @@ const groupTestResults = (tests) => { } } + 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) diff --git a/presets/facebook.js b/presets/facebook.js index f3668e6..fbf4941 100644 --- a/presets/facebook.js +++ b/presets/facebook.js @@ -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' }, diff --git a/presets/google.js b/presets/google.js index 0d2f9ba..950d4d8 100644 --- a/presets/google.js +++ b/presets/google.js @@ -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]), diff --git a/presets/twitter.js b/presets/twitter.js index 493f5db..f91d89d 100644 --- a/presets/twitter.js +++ b/presets/twitter.js @@ -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' },