2017-05-10 05:59:50 -04:00
|
|
|
const visit = require('unist-util-visit');
|
|
|
|
const is = require('hast-util-is-element');
|
|
|
|
const source = require('unist-util-source');
|
2017-05-30 15:24:20 -04:00
|
|
|
const toString = require('hast-util-to-string');
|
|
|
|
const filter = require('unist-util-filter');
|
2017-05-10 05:59:50 -04:00
|
|
|
|
|
|
|
module.exports = function h1CheckerPostProcessor() {
|
|
|
|
return (ast, file) => {
|
|
|
|
let h1s = [];
|
|
|
|
visit(ast, node => {
|
|
|
|
if (is(node, 'h1')) {
|
|
|
|
h1s.push(node);
|
2017-05-30 15:24:20 -04:00
|
|
|
file.title = getText(node);
|
2017-05-10 05:59:50 -04:00
|
|
|
}
|
|
|
|
});
|
2017-05-30 15:24:20 -04:00
|
|
|
|
2017-05-10 05:59:50 -04:00
|
|
|
if (h1s.length > 1) {
|
|
|
|
const h1Src = h1s.map(node => source(node, file)).join(', ');
|
|
|
|
file.fail(`More than one h1 found [${h1Src}]`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2017-05-30 15:24:20 -04:00
|
|
|
|
|
|
|
function getText(h1) {
|
|
|
|
// Remove the aria-hidden anchor from the h1 node
|
|
|
|
const cleaned = filter(h1, node => !(
|
|
|
|
is(node, 'a') && node.properties &&
|
|
|
|
(node.properties.ariaHidden === 'true' || node.properties['aria-hidden'] === 'true')
|
|
|
|
));
|
|
|
|
|
|
|
|
return toString(cleaned);
|
|
|
|
}
|