parent
d28a3f7878
commit
bcbee13e26
|
@ -127,7 +127,8 @@ module.exports = new Package('angular-base', [
|
||||||
addImageDimensions.basePath = path.resolve(AIO_PATH, 'src');
|
addImageDimensions.basePath = path.resolve(AIO_PATH, 'src');
|
||||||
postProcessHtml.plugins = [
|
postProcessHtml.plugins = [
|
||||||
require('./post-processors/autolink-headings'),
|
require('./post-processors/autolink-headings'),
|
||||||
addImageDimensions
|
addImageDimensions,
|
||||||
|
require('./post-processors/h1-checker'),
|
||||||
];
|
];
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -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();
|
||||||
|
});
|
||||||
|
});
|
|
@ -28,19 +28,20 @@ module.exports = function postProcessHtml(log, createDocMessage) {
|
||||||
.data('settings', { fragment: true });
|
.data('settings', { fragment: true });
|
||||||
this.plugins.forEach(plugin => engine.use(plugin));
|
this.plugins.forEach(plugin => engine.use(plugin));
|
||||||
|
|
||||||
|
let vFile;
|
||||||
|
|
||||||
docs
|
docs
|
||||||
.filter(doc => this.docTypes.indexOf(doc.docType) !== -1)
|
.filter(doc => this.docTypes.indexOf(doc.docType) !== -1)
|
||||||
.forEach(doc => {
|
.forEach(doc => {
|
||||||
const vFile = engine.processSync(doc.renderedContent);
|
try {
|
||||||
vFile.messages.forEach(m => {
|
vFile = engine.processSync(doc.renderedContent);
|
||||||
const message = createDocMessage(m.message, doc);
|
|
||||||
if (m.fatal) {
|
|
||||||
throw new Error(message);
|
|
||||||
} else {
|
|
||||||
log.warn(message);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
doc.renderedContent = vFile.contents;
|
doc.renderedContent = vFile.contents;
|
||||||
|
vFile.messages.forEach(m => {
|
||||||
|
log.warn(createDocMessage(m.message, doc));
|
||||||
|
});
|
||||||
|
} catch(e) {
|
||||||
|
throw new Error(createDocMessage(e.message, doc));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,11 +2,12 @@ const testPackage = require('../../helpers/test-package');
|
||||||
const Dgeni = require('dgeni');
|
const Dgeni = require('dgeni');
|
||||||
|
|
||||||
describe('postProcessHtml', function() {
|
describe('postProcessHtml', function() {
|
||||||
let dgeni, injector, processor;
|
let dgeni, injector, processor, createDocMessage;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
dgeni = new Dgeni([testPackage('post-process-package', true)]);
|
dgeni = new Dgeni([testPackage('post-process-package', true)]);
|
||||||
injector = dgeni.configureInjector();
|
injector = dgeni.configureInjector();
|
||||||
|
createDocMessage = injector.get('createDocMessage');
|
||||||
processor = injector.get('postProcessHtml');
|
processor = injector.get('postProcessHtml');
|
||||||
processor.docTypes = ['a', 'b'];
|
processor.docTypes = ['a', 'b'];
|
||||||
});
|
});
|
||||||
|
@ -75,8 +76,9 @@ describe('postProcessHtml', function() {
|
||||||
const addError = (ast, file) => {
|
const addError = (ast, file) => {
|
||||||
file.fail('There was an error');
|
file.fail('There was an error');
|
||||||
};
|
};
|
||||||
|
const doc = { docType: 'a', renderedContent: '' };
|
||||||
processor.plugins = [() => addError];
|
processor.plugins = [() => addError];
|
||||||
expect(() => processor.$process([{ docType: 'a', renderedContent: '' }])).toThrow();
|
expect(() => processor.$process([doc])).toThrowError(createDocMessage('There was an error', doc));
|
||||||
expect(log.error).not.toHaveBeenCalled();
|
expect(log.error).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue