diff --git a/docs/blog.md b/docs/blog.md index fe3975697..f1f7a27dc 100644 --- a/docs/blog.md +++ b/docs/blog.md @@ -89,8 +89,9 @@ ng generate @scullyio/init:post --name="This is my post" The following table shows all available options: -| option | description | default | -| -------------- | ------------------------------------------------------ | --------- | -| `name` | Define the name for the created post | 'blog-X' | -| `target` | Define the target directory for the new post file | 'blog' | -| `metaDataFile` | Use a meta data yaml template from a file for the post | undefined | +| option | description | default | +| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------- | +| `name` | Define the name for the created post | 'blog-X' | +| `target` | Define the target directory for the new post file | 'blog' | +| `metaDataFile` | Use a meta data yaml template from a file for the post | undefined | +| `extension` | define the file extension for the target file.
Out of the box Scully supports `adoc` and `md`.
You can handle other files by setting up your own [_File Handle Plugin_](plugins.md#file-plugin). | 'md' | diff --git a/libs/scully-schematics/src/add-post/index.ts b/libs/scully-schematics/src/add-post/index.ts index 558c39b6c..f944991b0 100644 --- a/libs/scully-schematics/src/add-post/index.ts +++ b/libs/scully-schematics/src/add-post/index.ts @@ -12,15 +12,23 @@ import { yamlToJson, jsonToJaml, removeWrongCharacters } from '../utils/utils'; export default function(options: Schema): Rule { return (host: Tree, context: SchematicContext) => { const name = options.name; + const extension = options.extension || 'md'; const nameDasherized = options.name ? strings.dasherize(options.name) : 'blog-X'; const targetDasherized = options.target ? strings.dasherize(options.target) : 'blog'; + + if (!/^[\w]+$/.test(extension)) { + throw new SchematicsException( + `${extension} is not a valid file extension` + ); + } + const filename = `./${targetDasherized}/${removeWrongCharacters( nameDasherized - )}.md`; + )}.${extension}`; let metaData = { title: '', diff --git a/libs/scully-schematics/src/add-post/index_spec.ts b/libs/scully-schematics/src/add-post/index_spec.ts index b16f5aa35..cecf17dff 100644 --- a/libs/scully-schematics/src/add-post/index_spec.ts +++ b/libs/scully-schematics/src/add-post/index_spec.ts @@ -120,4 +120,41 @@ describe('add-post', () => { expect(getFileContent(appTree, defaultExpectedFileName)).toEqual('foo'); }); }); + + describe('when specify a file extension', () => { + it('should use the specified file extension', async () => { + const extension = 'adoc'; + const options = { ...defaultOptions, extension }; + appTree = await customRunner + .runSchematicAsync('post', options, appTree) + .toPromise(); + expect(appTree.files).toContain('/blog/foo-bar-baz.adoc'); + }); + + it('should use `md` file extension by default', async () => { + const extension = ''; + const options = { ...defaultOptions, extension }; + appTree = await customRunner + .runSchematicAsync('post', options, appTree) + .toPromise(); + expect(appTree.files).toContain('/blog/foo-bar-baz.md'); + }); + + it('should throw an error when file extension is invalid', async () => { + const extension = 'invalid?ext'; + const options = { ...defaultOptions, extension }; + let error = ''; + try { + appTree = await customRunner + .runSchematicAsync('post', options, appTree) + .toPromise(); + } catch (e) { + error = e; + } + expect(appTree.files).toContain('/blog/foo-bar-baz.adoc'); + expect(error).toMatch( + /Error: invalid\?ext is not a valid file extension/g + ); + }); + }); }); diff --git a/libs/scully-schematics/src/add-post/schema.json b/libs/scully-schematics/src/add-post/schema.json index fb6ce8521..2d94e89f5 100644 --- a/libs/scully-schematics/src/add-post/schema.json +++ b/libs/scully-schematics/src/add-post/schema.json @@ -16,6 +16,11 @@ "x-prompt": "What's the target folder for this post?", "default": "blog" }, + "extension": { + "type": "string", + "description": "define the file extension for the target file", + "default": "md" + }, "metaDataFile": { "type": "string", "description": "use a meta data template file that's data will be added to the post", diff --git a/libs/scully-schematics/src/add-post/schema.ts b/libs/scully-schematics/src/add-post/schema.ts index d9331720c..8f2e5698d 100644 --- a/libs/scully-schematics/src/add-post/schema.ts +++ b/libs/scully-schematics/src/add-post/schema.ts @@ -10,6 +10,10 @@ export interface Schema { * define the target directory for the new post file */ target?: string; + /** + * define the file extension for the target file + */ + extension?: string; /** * use a meta data template file that's data will be added to the post */