Skip to content

Commit

Permalink
feat(main): enable custom derived category
Browse files Browse the repository at this point in the history
  • Loading branch information
voxpelli committed Aug 17, 2016
1 parent 0285b3e commit 15e832b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -69,6 +69,7 @@ formatter.preFormat(micropubDocument)
* **noMarkdown** – if set to `true` then no conversion to Markdown will happen for the content.
* **contentSlug** – if set to `true`, then the slug creation will use the `properties.content` data as a fallback to `properties.name` prior to basing the slug on the timestamp.
* **defaults** – a `micropubDocument` with defaults that will be added as part of the `preFormat()`. Useful to eg. ensure that all documents have a language explicitly set.
* **deriveCategory** – a method that's provided the properties of the post and which returns a dervice category – or `false` to disable default category deriving
* **deriveLanguages** – an array defining what languages, using [ISO 639-3](https://en.wikipedia.org/wiki/ISO_639-3), to autodetect – or `true` to try and autodetect everything
* **permalinkStyle** – a Jekyll [permalink style](https://jekyllrb.com/docs/permalinks/). Defaults to Jekyll's default: `date`

Expand Down
10 changes: 9 additions & 1 deletion index.js
Expand Up @@ -56,6 +56,7 @@ const Formatter = function (options) {
this.defaults = options.defaults;
this.deriveLanguages = options.deriveLanguages || false;
this.permalinkStyle = options.permalinkStyle;
this.deriveCategory = options.deriveCategory === undefined ? true : options.deriveCategory;
};

Formatter.prototype._resolveFrontMatterData = function (data) {
Expand Down Expand Up @@ -257,7 +258,14 @@ Formatter.prototype.preFormat = function (data) {
}
}

if (
if (typeof this.deriveCategory === 'function') {
data.derived.category = this.deriveCategory(data.properties);
if (!data.derived.category) {
delete data.derived.category;
}
} else if (!this.deriveCategory) {
// Do nothing
} else if (
!_.isEmpty(data.properties.bookmark) ||
!_.isEmpty(data.properties['repost-of']) ||
!_.isEmpty(data.properties['bookmark-of'])
Expand Down
11 changes: 11 additions & 0 deletions test/formatter.spec.js
Expand Up @@ -402,6 +402,17 @@ describe('Formatter', function () {
return formatter.preFormat(baseMicroformatData).should.eventually.not.have.deep.property('derived.category');
});

it('should not derive interaction category when opted out of', function () {
formatter = new Formatter({ deriveCategory: false });
baseMicroformatData.properties['in-reply-to'] = ['http://example.com/replied/to/page'];
return formatter.preFormat(baseMicroformatData).should.eventually.not.have.deep.property('derived.category');
});

it('should use result of deriveCategory() method when provided', function () {
formatter = new Formatter({ deriveCategory: () => 'foo' });
return formatter.preFormat(baseMicroformatData).should.eventually.have.deep.property('derived.category', 'foo');
});

it('should extract person tags from regular tags', function () {
baseMicroformatData.properties.category = ['http://example.com/', 'foo', 'http://example.net/'];
return formatter.preFormat(baseMicroformatData).then(function (result) {
Expand Down

0 comments on commit 15e832b

Please sign in to comment.