Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

List of tags and categories #517

Closed
asarkar opened this issue Sep 18, 2024 · 1 comment
Closed

List of tags and categories #517

asarkar opened this issue Sep 18, 2024 · 1 comment

Comments

@asarkar
Copy link

asarkar commented Sep 18, 2024

I like the style, thanks for making the theme freely available. However, there’s one feature that I really want in my blog which isn’t present in the OOTB theme. It’s a list of tags, and optionally categories, ordered by popularity. Is this something you can add to the theme? If not, can a user of your user make this modification easily?

@mathieukh
Copy link
Contributor

mathieukh commented Sep 28, 2024

You might consider adding those snippets in blog.ts:

export const findTagsWithOccurrences = async (): Promise<Record<string, number>> => {
  const posts = await fetchPosts();
  const allTags = posts.flatMap(({ tags = [] }) => [...tags]).map(({ title }) => title);
  return allTags.reduce((acc, tag) => ({ ...acc, [tag]: acc[tag] ? acc[tag] + 1 : 1 }), {} as Record<string, number>);
};

export const findCategoriesWithOccurrences = async (): Promise<Record<string, number>> => {
  const posts = await fetchPosts();
  const allCategories = posts
    .map(({ category }) => category)
    .filter((category) => category !== undefined)
    .map(({ title }) => title);
  return allCategories.reduce(
    (acc, category) => ({ ...acc, [category]: acc[category] ? acc[category] + 1 : 1 }),
    {} as Record<string, number>
  );
};

It will return an object with the names of tags or categories considering the methods you will using with the number of occurrences found for them in the blog.

You can use those objects to create the expected behaviour from there.

for example:

const tagsWithOccurrences = await findTagsWithOccurrences();
const orderedTagsByUsage = Object.entries(tagsWithOccurrences).sort(([,to1], [,to2]) => to2 - to1);

This will result in an array of tuple with [tag, numberOfOccurrences] ordered by usage desc. (the most used is first)

@onwidget onwidget locked and limited conversation to collaborators Oct 4, 2024
@prototypa prototypa converted this issue into discussion #526 Oct 4, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants