Skip to content

Commit

Permalink
Merge pull request #1 from h-ikeda/intro-outro
Browse files Browse the repository at this point in the history
New intro/outro plugin options
  • Loading branch information
h-ikeda authored Jan 1, 2020
2 parents 736f5a5 + cfd997c commit c4ba335
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 4 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,20 @@ RegExp, or an array of them.
### prefix
Path prefix for generated markdown files. Files will be put at
\<asset path>/\<prefix>/\<module's relative path>

### intro/outro
String to be prepended before or appended after generated markdown strings. Can
be a string or a function. Function will be called with an argument object including
id of the module.

```js
export default {
...generalConfigs,
plugins: [
vuedoc({ intro: ({ id }) => `id` }),
...otherPlugins,
],
};

// Generates markdown files starting with module's ID string.
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rollup-plugin-vuedoc",
"version": "0.0.0",
"version": "0.1.0",
"description": "Rollup plugin to generate markdown documentation from Vue.js component source with @vuedoc/md.",
"main": "src/index.js",
"directories": {
Expand Down
16 changes: 13 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ function match(target, test) {
});
}

module.exports = function vuedoc({ test, prefix = '' } = {}) {
module.exports = function vuedoc({ test, prefix = '', intro = '', outro = '' } = {}) {

if (!/function|string/.test(typeof intro) | !/function|string/.test(typeof outro)) {
throw new TypeError('intro/outro option must be a function or a string.');
}

return {
name: 'vuedoc',
async buildStart({ input }) {
Expand All @@ -24,8 +29,13 @@ module.exports = function vuedoc({ test, prefix = '' } = {}) {
this.error(e);
}
}
const source = await md({ filename: id });
if (!source.length) return null;
const doc = await md({ filename: id });
if (!doc.length) return null;
const introString = typeof intro === 'function' ? intro({ id }) : intro;
const outroString = typeof outro === 'function' ? outro({ id }) : outro;
const introLf = introString.length ? '\n' : '';
const outroLf = outroString.length ? '\n' : '';
const source = `${introString}${introLf}${doc}${outroLf}${outroString}`;
const { dir, name } = parse(join(prefix, relative(this.vuedocInput, id)));
const fileName = format({ dir, name, ext: '.md' });
this.emitFile({ type: 'asset', source, fileName });
Expand Down
96 changes: 96 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,100 @@ module.exports = (rollup) => () => {
expect(output).not.toContainEqual(expect.objectContaining({ fileName: 'index.md' }));
expect(output).not.toContainEqual(expect.objectContaining({ fileName: 'test-component-b.md' }));
});
test('rollup should output markdowns that starts with intro option string', async () => {
const bundle = await rollup({
input: resolve(__dirname, 'fixtures/basic/index.js'),
plugins: [vuedoc({ intro: 'Prepended by intro option' })],
});
const { output } = await bundle.generate({ format: 'esm' });
expect(output).toEqual(expect.arrayContaining([
expect.objectContaining({
fileName: 'test-component-a.md',
isAsset: true,
source: `Prepended by intro option\n${a}`,
}),
expect.objectContaining({
fileName: 'test-component-b.md',
isAsset: true,
source: `Prepended by intro option\n${b}`,
}),
expect.objectContaining({
fileName: 'test-directory/test-component-c.md',
isAsset: true,
source: `Prepended by intro option\n${c}`,
}),
]));
});
test('rollup should output markdowns that starts with intro option function', async () => {
const bundle = await rollup({
input: resolve(__dirname, 'fixtures/basic/index.js'),
plugins: [vuedoc({ intro: ({ id }) => `${id}` })],
});
const { output } = await bundle.generate({ format: 'esm' });
expect(output).toEqual(expect.arrayContaining([
expect.objectContaining({
fileName: 'test-component-a.md',
isAsset: true,
source: `${resolve(__dirname, 'fixtures/basic/test-component-a.js')}\n${a}`,
}),
expect.objectContaining({
fileName: 'test-component-b.md',
isAsset: true,
source: `${resolve(__dirname, 'fixtures/basic/test-component-b.js')}\n${b}`,
}),
expect.objectContaining({
fileName: 'test-directory/test-component-c.md',
isAsset: true,
source: `${resolve(__dirname, 'fixtures/basic/test-directory/test-component-c.js')}\n${c}`,
}),
]));
});
test('rollup should output markdowns that starts with outro option string', async () => {
const bundle = await rollup({
input: resolve(__dirname, 'fixtures/basic/index.js'),
plugins: [vuedoc({ outro: 'Appended by outro option' })],
});
const { output } = await bundle.generate({ format: 'esm' });
expect(output).toEqual(expect.arrayContaining([
expect.objectContaining({
fileName: 'test-component-a.md',
isAsset: true,
source: `${a}\nAppended by outro option`,
}),
expect.objectContaining({
fileName: 'test-component-b.md',
isAsset: true,
source: `${b}\nAppended by outro option`,
}),
expect.objectContaining({
fileName: 'test-directory/test-component-c.md',
isAsset: true,
source: `${c}\nAppended by outro option`,
}),
]));
});
test('rollup should output markdowns that starts with intro option function', async () => {
const bundle = await rollup({
input: resolve(__dirname, 'fixtures/basic/index.js'),
plugins: [vuedoc({ outro: ({ id }) => `${id}` })],
});
const { output } = await bundle.generate({ format: 'esm' });
expect(output).toEqual(expect.arrayContaining([
expect.objectContaining({
fileName: 'test-component-a.md',
isAsset: true,
source: `${a}\n${resolve(__dirname, 'fixtures/basic/test-component-a.js')}`,
}),
expect.objectContaining({
fileName: 'test-component-b.md',
isAsset: true,
source: `${b}\n${resolve(__dirname, 'fixtures/basic/test-component-b.js')}`,
}),
expect.objectContaining({
fileName: 'test-directory/test-component-c.md',
isAsset: true,
source: `${c}\n${resolve(__dirname, 'fixtures/basic/test-directory/test-component-c.js')}`,
}),
]));
});
};

0 comments on commit c4ba335

Please sign in to comment.