Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: import nested categories #89

Merged
merged 2 commits into from
Aug 1, 2020
Merged

Conversation

curbengh
Copy link
Contributor

@curbengh curbengh commented Jul 27, 2020

Fixes #36 cc @jashsayani @adnan360

Example categories:

  1. lorem => ipsum
  2. dolor

lorem and dolor are parent (top-level) categories, ipsum is child of lorem.

After import, the front-matter should have:

categories:
  - - lorem
    - ipsum
  - - dolor

Equivalent to:

categories:
  - [lorem, ipsum]
  - [dolor]

Next PR can add an option to skip Uncategorized category.

@coveralls
Copy link

coveralls commented Jul 27, 2020

Coverage Status

Coverage increased (+0.5%) to 96.273% when pulling b17721c on curbengh:post-category into b5b2565 on hexojs:master.

@curbengh
Copy link
Contributor Author

curbengh commented Jul 27, 2020

Currently it's only limited to two-level categories. b17721c

Input:

  1. lorem => ipsum => dolor (dolor is child of ipsum, ipsum is child of lorem)
  2. foo => bar
  3. baz

I have limited skill. If anyone wants to help, the objective is to have following output for the above input.

Output: [ ['lorem', 'ipsum', 'dolor'], ['foo', 'bar'], ['baz'] ]

WP describes the relationship like this:

[
  { name: 'lorem', parent: '' },
  { name: 'ipsum', parent: 'lorem' },
  { name: 'dolor', parent: 'ipsum' },
  { name: 'foo', parent: '' },
  { name: 'bar', parent: 'foo' },
  { name: 'baz', parent: '' }
]

Then converted to object for easier access,

{
  lorem: '',
  ipsum: 'lorem',
  dolor: 'ipsum',
  foo: '',
  bar: 'foo',
  baz: '',
}

The goal is to formats the object into that output. Each category can only have one parent (one-to-many).

@adnan360
Copy link

adnan360 commented Jul 27, 2020

@curbengh Here is a code. It should support infinite nesting of categories:

var cats = [
	{ name: 'lorem', parent: '' },
	{ name: 'ipsum', parent: 'lorem' },
	{ name: 'dolor', parent: 'ipsum' },
	{ name: 'foo', parent: '' },
	{ name: 'bar', parent: 'foo' },
	{ name: 'baz', parent: '' }
];

console.log(cats);
console.log('---------------------------');

// To enable deep level flatten. Uses recursion with reduce and concat.
// @source https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat#Alternatives
function flatDeep(arr, d = 1) {
	return d > 0 ? arr.reduce((acc, val) => acc.concat(Array.isArray(val) ? flatDeep(val, d - 1) : val), [])
				: arr.slice();
};

// Nests array based on their parent data.
// @source https://stackoverflow.com/a/55241491
const nest = (items, name = '', link = 'parent') =>
	items
		.filter(item => item[link] === name)
		.map(item => ( flatDeep( [ item.name, nest(items, item.name) ], Infinity ) ));
console.log( nest(cats) );

Outputs:

[ [ 'lorem', 'ipsum', 'dolor' ], [ 'foo', 'bar' ], [ 'baz' ] ]

@curbengh
Copy link
Contributor Author

curbengh commented Jul 28, 2020

@adnan360 Fantastic.

Since this plugin requires Node 12+, Array.flat() can be utilised

const nest = (items, name = '', link = 'parent') => {
  return items
    .filter(item => item[link] === name)
    .map(item => [item.name, nest(items, item.name)].flat(Infinity));
}

@curbengh
Copy link
Contributor Author

@jashsayani @adnan360

This PR is ready for test, it now supports non-nested categories and >2 levels categories.

@adnan360
Copy link

Working fine on my end
wp-multi-category-02

@curbengh curbengh merged commit bf32100 into hexojs:master Aug 1, 2020
@curbengh curbengh deleted the post-category branch August 1, 2020 06:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

multiple categories exported as nested categories
3 participants