build(aio): refactor dgeni packages

This is to tidy up the `author-packagse`, which currently duplicates a
lot of the configuration in the main packages. We need to
DRY this up so that we don't fall foul of a change in one being missed in
the other.
This commit is contained in:
Peter Bacon Darwin
2017-04-21 13:10:52 +01:00
committed by Pete Bacon Darwin
parent 7a8bd99ab1
commit 3cad5da5a4
66 changed files with 480 additions and 634 deletions

View File

@ -0,0 +1,62 @@
module.exports = function() {
// var MIXIN_PATTERN = /\S*\+\S*\(.*/;
return {
name: 'indentForMarkdown',
process: function(str, width) {
if (str == null || str.length === 0) {
return '';
}
width = width || 4;
var lines = str.split('\n');
var newLines = [];
var sp = spaces(width);
var spMixin = spaces(width - 2);
var isAfterMarkdownTag = true;
lines.forEach(function(line) {
// indent lines that match mixin pattern by 2 less than specified width
if (line.indexOf('{@example') >= 0) {
if (isAfterMarkdownTag) {
// happens if example follows example
if (newLines.length > 0) {
newLines.pop();
} else {
// wierd case - first expression in str is an @example
// in this case the :marked appear above the str passed in,
// so we need to put 'something' into the markdown tag.
newLines.push(sp + '.'); // '.' is a dummy char
}
}
newLines.push(spMixin + line);
// after a mixin line we need to reenter markdown.
newLines.push(spMixin + ':marked');
isAfterMarkdownTag = true;
} else {
if ((!isAfterMarkdownTag) || (line.trim().length > 0)) {
newLines.push(sp + line);
isAfterMarkdownTag = false;
}
}
});
if (isAfterMarkdownTag) {
if (newLines.length > 0) {
// if last line is a markdown tag remove it.
newLines.pop();
}
}
// force character to be a newLine.
if (newLines.length > 0) newLines.push('');
var res = newLines.join('\n');
return res;
}
};
function spaces(n) {
var str = '';
for (var i = 0; i < n; i++) {
str += ' ';
}
return str;
}
};

View File

@ -0,0 +1,6 @@
module.exports = function toId() {
return {
name: 'toId',
process: function(str) { return str.replace(/[^(a-z)(A-Z)(0-9)._-]/g, '-'); }
};
};

View File

@ -0,0 +1,14 @@
var factory = require('./toId');
describe('toId filter', function() {
var filter;
beforeEach(function() { filter = factory(); });
it('should be called "toId"', function() { expect(filter.name).toEqual('toId'); });
it('should convert a string to make it appropriate for use as an HTML id', function() {
expect(filter.process('This is a big string with €bad#characaters¢\nAnd even NewLines'))
.toEqual('This-is-a-big-string-with--bad-characaters--And-even-NewLines');
});
});

View File

@ -0,0 +1,15 @@
module.exports = function() {
return {
name: 'trimBlankLines',
process: function(str) {
var lines = str.split(/\r?\n/);
while (lines.length && (lines[0].trim() === '')) {
lines.shift();
}
while (lines.length && (lines[lines.length - 1].trim() === '')) {
lines.pop();
}
return lines.join('\n');
}
};
};

View File

@ -0,0 +1,15 @@
var factory = require('./trimBlankLines');
describe('trimBlankLines filter', function() {
var filter;
beforeEach(function() { filter = factory(); });
it('should be called "trimBlankLines"',
function() { expect(filter.name).toEqual('trimBlankLines'); });
it('should remove empty lines from the start and end of the string', function() {
expect(filter.process('\n \n\nsome text\n \nmore text\n \n'))
.toEqual('some text\n \nmore text');
});
});