
committed by
Pete Bacon Darwin

parent
d28a3f7878
commit
bcbee13e26
@ -127,7 +127,8 @@ module.exports = new Package('angular-base', [
|
||||
addImageDimensions.basePath = path.resolve(AIO_PATH, 'src');
|
||||
postProcessHtml.plugins = [
|
||||
require('./post-processors/autolink-headings'),
|
||||
addImageDimensions
|
||||
addImageDimensions,
|
||||
require('./post-processors/h1-checker'),
|
||||
];
|
||||
})
|
||||
|
||||
|
18
aio/tools/transforms/angular-base-package/post-processors/h1-checker.js
vendored
Normal file
18
aio/tools/transforms/angular-base-package/post-processors/h1-checker.js
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
const visit = require('unist-util-visit');
|
||||
const is = require('hast-util-is-element');
|
||||
const source = require('unist-util-source');
|
||||
|
||||
module.exports = function h1CheckerPostProcessor() {
|
||||
return (ast, file) => {
|
||||
let h1s = [];
|
||||
visit(ast, node => {
|
||||
if (is(node, 'h1')) {
|
||||
h1s.push(node);
|
||||
}
|
||||
});
|
||||
if (h1s.length > 1) {
|
||||
const h1Src = h1s.map(node => source(node, file)).join(', ');
|
||||
file.fail(`More than one h1 found [${h1Src}]`);
|
||||
}
|
||||
};
|
||||
};
|
@ -0,0 +1,49 @@
|
||||
var testPackage = require('../../helpers/test-package');
|
||||
var Dgeni = require('dgeni');
|
||||
const plugin = require('./h1-checker');
|
||||
|
||||
describe('h1Checker postprocessor', () => {
|
||||
let processor, createDocMessage;
|
||||
|
||||
beforeEach(() => {
|
||||
const dgeni = new Dgeni([testPackage('angular-base-package')]);
|
||||
const injector = dgeni.configureInjector();
|
||||
createDocMessage = injector.get('createDocMessage');
|
||||
processor = injector.get('postProcessHtml');
|
||||
processor.docTypes = ['a'];
|
||||
processor.plugins = [plugin];
|
||||
});
|
||||
|
||||
it('should complain if there is more than one h1 in a document', () => {
|
||||
const doc = {
|
||||
docType: 'a',
|
||||
renderedContent: `
|
||||
<h1>Heading 1</h2>
|
||||
<h2>Heading 2</h2>
|
||||
<h1>Heading 1a</h1>
|
||||
`
|
||||
};
|
||||
expect(() => processor.$process([doc])).toThrowError(createDocMessage('More than one h1 found [<h1>Heading 1, <h1>Heading 1a</h1>]', doc));
|
||||
});
|
||||
|
||||
it('should not complain if there is exactly one h1 in a document', () => {
|
||||
const doc = {
|
||||
docType: 'a',
|
||||
renderedContent: `
|
||||
<h1>Heading 1</h2>
|
||||
<h2>Heading 2</h2>
|
||||
`
|
||||
};
|
||||
expect(() => processor.$process([doc])).not.toThrow();
|
||||
});
|
||||
|
||||
it('should not complain if there are no h1s in a document', () => {
|
||||
const doc = {
|
||||
docType: 'a',
|
||||
renderedContent: `
|
||||
<h2>Heading 2</h2>
|
||||
`
|
||||
};
|
||||
expect(() => processor.$process([doc])).not.toThrow();
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user