Skip to content

Commit

Permalink
feat(statistics): include til tags on tag-cloud
Browse files Browse the repository at this point in the history
fix #797
  • Loading branch information
mateusfg7 committed Feb 27, 2024
1 parent 95ca6ee commit 94fbdc1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"remark-rehype": "^11.0.0",
"sharp": "^0.33.0",
"shiki": "^1.1.7",
"shuffle-array": "^1.0.1",
"tailwindcss-animated": "^1.0.1",
"unified": "^11.0.4",
"unist-util-visit": "^5.0.0",
Expand Down
7 changes: 0 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import arrayShuffle from 'shuffle-array'

import { Tag } from '@phosphor-icons/react/dist/ssr'
import { getTagsAndNumberOfPosts } from '~/lib/tags'
import { shuffleArray } from '~/lib/shuffleArray'
import { countAllTags } from '~/lib/tags'

// https://github.com/madox2/react-tagcloud/blob/43a706b36a606ddd8a39c4060510166aebdf445e/src/helpers.js#L4
const fontSizeConverter = (
Expand All @@ -23,11 +22,13 @@ const fontSizeConverter = (
}

export async function TagCloud() {
const tags: { value: string; count: number }[] = arrayShuffle(
getTagsAndNumberOfPosts().map(value => ({
value: value.tag,
count: value.numberOfPosts
}))
const tagsCount = countAllTags().map(({ count, tag }) => ({
value: tag,
count
}))

const tags: { value: string; count: number }[] = shuffleArray(
tagsCount.sort((a, b) => b.count - a.count).slice(0, 100)
)

return (
Expand Down
15 changes: 14 additions & 1 deletion src/lib/tags.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { slug } from '~/lib/slug'
import { removeRepeatedValuesFromArray } from '~/lib/remove-repeated-values-from-array'
import { getFrequencyOfValue } from '~/lib/get-frequency-of-value'
import { Post, posts } from '#content'
import { Post, posts, tils } from '#content'

const getRawTagListFromPosts = () =>
posts.filter((post: Post) => !post.test).flatMap(post => post.tags)
Expand Down Expand Up @@ -43,3 +43,16 @@ export function getNormalTagString(tag: string) {

return allTags.find(currTag => slug(currTag) === slug(tag))
}

export function countAllTags() {
const tagList = [
...tils.flatMap(til => til.tags),
...posts.flatMap(post => post.tags)
]
const uniqueTagList = removeRepeatedValuesFromArray(tagList)

return uniqueTagList.map(tag => ({
tag,
count: getFrequencyOfValue(tagList, tag)
}))
}

0 comments on commit 94fbdc1

Please sign in to comment.