-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
new_audit(anchor-text-audit): descriptive anchor text audit #3490
Changes from 6 commits
b9e8bda
2c1600e
5794d61
068da8e
f4a7989
db23054
b710137
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* @license Copyright 2017 Google Inc. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
const Audit = require('../audit'); | ||
const URL = require('../../lib/url-shim'); | ||
const BLOCKLIST = new Set([ | ||
'click here', | ||
'click this', | ||
'go', | ||
'here', | ||
'this', | ||
'start', | ||
'right here', | ||
'more', | ||
'learn more', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😱 our report would fail like crazy! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The help text for this audit has a "learn more" link 😆 (as bad of an example as it sets, in our defense this tool will not be crawled by search bots) |
||
]); | ||
|
||
class AnchorText extends Audit { | ||
/** | ||
* @return {!AuditMeta} | ||
*/ | ||
static get meta() { | ||
return { | ||
category: 'Content Best Practices', | ||
name: 'anchor-text', | ||
description: 'Anchors have descriptive text.', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
cc @rviscomi you okay with calling these "links" instead of 'anchors' ? IMO its more natural and i dont think the technical correctness is worth it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "links" or " @kdzwinel please also update the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing |
||
failureDescription: 'Anchors do not have descriptive text', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Anchors => Links |
||
helpText: 'Descriptive anchor text helps search engines understand your content. ' + | ||
'[Learn more](https://webmasters.googleblog.com/2008/10/importance-of-link-architecture.html)', | ||
requiredArtifacts: ['URL', 'CrawlableAnchors'], | ||
}; | ||
} | ||
|
||
/** | ||
* @param {!Artifacts} artifacts | ||
* @return {!AuditResult} | ||
*/ | ||
static audit(artifacts) { | ||
const failingAnchors = artifacts.CrawlableAnchors | ||
.filter(anchor => { | ||
if ( | ||
anchor.href.toLowerCase().startsWith('javascript:') || | ||
URL.equalWithExcludedFragments(anchor.href, artifacts.URL.finalUrl) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are links within the page not counted? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I understand, these links have no SEO value and often are handled by JS anyway (e.g. |
||
) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we decided to skip all links that do not link to another page (e.g. '#') |
||
return false; | ||
} | ||
|
||
return BLOCKLIST.has(anchor.text.trim().toLowerCase()); | ||
}); | ||
|
||
const headings = [ | ||
{key: 'href', itemType: 'url', text: 'Link destination'}, | ||
{key: 'text', itemType: 'text', text: 'Link Text'}, | ||
]; | ||
|
||
const details = Audit.makeTableDetails(headings, failingAnchors); | ||
let displayValue; | ||
|
||
if (failingAnchors.length) { | ||
displayValue = failingAnchors.length > 1 ? | ||
`${failingAnchors.length} anchors found` : '1 anchor found'; | ||
} | ||
|
||
return { | ||
rawValue: failingAnchors.length === 0, | ||
details, | ||
displayValue, | ||
}; | ||
} | ||
} | ||
|
||
module.exports = AnchorText; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* @license Copyright 2017 Google Inc. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
const Gatherer = require('../gatherer'); | ||
const DOMHelpers = require('../../../lib/dom-helpers.js'); | ||
|
||
class CrawlableAnchors extends Gatherer { | ||
/** | ||
* @param {{driver: !Object}} options Run options | ||
* @return {!Promise<!Array<{href: string, text: string}>>} | ||
*/ | ||
afterPass(options) { | ||
const expression = `(function() { | ||
${DOMHelpers.getElementsInDocumentFnString}; // define function on page | ||
const selector = 'a[href]:not([rel~="nofollow"])'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would it be better to combine this with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sg. filed as #3581 |
||
const elements = getElementsInDocument(selector); | ||
return elements | ||
.map(node => ({ | ||
href: node.href, | ||
text: node.innerText | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. disagree. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. jk I'm wrong :) |
||
})); | ||
})()`; | ||
|
||
return options.driver.evaluateAsync(expression); | ||
} | ||
} | ||
|
||
module.exports = CrawlableAnchors; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* @license Copyright 2017 Google Inc. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
const AnchorTextAudit = require('../../../audits/seo/anchor-text.js'); | ||
const assert = require('assert'); | ||
|
||
/* eslint-env mocha */ | ||
|
||
describe('SEO: anchor text audit', () => { | ||
it('fails when anchor with non descriptive text is found', () => { | ||
const invalidAnchor = {href: 'https://example.com/otherpage.html', text: 'click here'}; | ||
const artifacts = { | ||
URL: { | ||
finalUrl: 'https://example.com/page.html', | ||
}, | ||
CrawlableAnchors: [ | ||
{href: 'https://example.com/otherpage.html', text: 'legit anchor text'}, | ||
invalidAnchor, | ||
{href: 'https://example.com/otherpage.html', text: 'legit anchor text'}, | ||
], | ||
}; | ||
|
||
const auditResult = AnchorTextAudit.audit(artifacts); | ||
assert.equal(auditResult.rawValue, false); | ||
assert.equal(auditResult.details.items.length, 1); | ||
assert.equal(auditResult.details.items[0][0].text, invalidAnchor.href); | ||
assert.equal(auditResult.details.items[0][1].text, invalidAnchor.text); | ||
}); | ||
|
||
it('ignores links pointing to the main document', () => { | ||
const artifacts = { | ||
URL: { | ||
finalUrl: 'https://example.com/page.html', | ||
}, | ||
CrawlableAnchors: [ | ||
{href: 'https://example.com/otherpage.html', text: 'legit anchor text'}, | ||
{href: 'https://example.com/page.html', text: 'click here'}, | ||
{href: 'https://example.com/page.html#test', text: 'click here'}, | ||
{href: 'https://example.com/otherpage.html', text: 'legit anchor text'}, | ||
], | ||
}; | ||
|
||
const auditResult = AnchorTextAudit.audit(artifacts); | ||
assert.equal(auditResult.rawValue, true); | ||
}); | ||
|
||
it('ignores javascript: links', () => { | ||
const artifacts = { | ||
URL: { | ||
finalUrl: 'https://example.com/page.html', | ||
}, | ||
CrawlableAnchors: [ | ||
{href: 'javascript:alert(1)', text: 'click here'}, | ||
{href: 'JavaScript:window.location="/otherpage.html"', text: 'click here'}, | ||
{href: 'JAVASCRIPT:void(0)', text: 'click here'}, | ||
], | ||
}; | ||
|
||
const auditResult = AnchorTextAudit.audit(artifacts); | ||
assert.equal(auditResult.rawValue, true); | ||
}); | ||
|
||
it('passes when all anchors have descriptive texts', () => { | ||
const artifacts = { | ||
URL: { | ||
finalUrl: 'https://example.com/page.html', | ||
}, | ||
CrawlableAnchors: [ | ||
{href: 'https://example.com/otherpage.html', text: 'legit anchor text'}, | ||
{href: 'http://example.com/page.html?test=test', text: 'legit anchor text'}, | ||
{href: 'file://Users/user/Desktop/file.png', text: 'legit anchor text'}, | ||
], | ||
}; | ||
|
||
const auditResult = AnchorTextAudit.audit(artifacts); | ||
assert.equal(auditResult.rawValue, true); | ||
}); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe
details: { items: {length: 3}}
as well to make sure they're in there? (you could also assert the actualitems
contents if you thought that was worthwhile...the only cost is more verbose/harder to scan test expectations)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checking length SGTM, but checking all contents gets very verbose (showing just one of three items):
I'd leave that to unit tests (in fact, since we are getting rid of
extendedInfo
, I'll have to usedetails
in unit tests).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM