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

Traverse option to automatically add siblings #6

Closed
ignatiusmb opened this issue Apr 14, 2021 · 0 comments · Fixed by #50
Closed

Traverse option to automatically add siblings #6

ignatiusmb opened this issue Apr 14, 2021 · 0 comments · Fixed by #50
Labels
enhancement Pull requests that adds a new feature
Milestone

Comments

@ignatiusmb
Copy link
Owner

Ability to pass in a siblings option that could format the sibling object. Will definitely need to receive a callback that returns the formatted item, and maybe another callback to check the breakpoint. Putting in the code from mauss.dev as reference.

function fillSiblings<T extends Child>(
	articles: T[],
	base: string,
	breakpoint?: (next: T) => boolean
): T[] {
	for (let i = 0; i < articles.length; i++) {
		if (!articles[i]['siblings']) articles[i]['siblings'] = {};
		const [prev, next] = [articles[i - 1], articles[i + 1]];
		if (prev) articles[i]['siblings']['prev'] = { slug: base + prev.slug, title: prev.title };
		if (breakpoint && breakpoint(next)) return articles;
		if (next) articles[i]['siblings']['next'] = { slug: base + next.slug, title: next.title };
	}
	return articles;
}

The default value will definitely be undefined as it will not generate siblings by default. Passed options however, might either receive an object or just a single callback function. More inclined to the first one as the latter seems overcomplicated and inconvenient.

siblings: {
  item: ({ prev, next }) => any,
  breakpoint: (next: T) => boolean
}
@ignatiusmb ignatiusmb added the enhancement Pull requests that adds a new feature label May 29, 2021
@ignatiusmb ignatiusmb added this to the v0.3.0 milestone May 29, 2021
@ignatiusmb ignatiusmb modified the milestones: v0.3.0, v0.4.0 Aug 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Pull requests that adds a new feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant