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

FEATURE: display category icon in post body #36

Merged
merged 4 commits into from
Jul 16, 2024
Merged

FEATURE: display category icon in post body #36

merged 4 commits into from
Jul 16, 2024

Conversation

Lhcfl
Copy link
Contributor

@Lhcfl Lhcfl commented Jul 15, 2024

This commit allows displaying category icons in the body of the post.

Screenshots

New setting:
image

Before change:

image

After change:

image

Related meta topic: https://meta.discourse.org/t/category-icons-dont-show-up-in-post-text/315851

This commit allows displaying category icons in the body of the post.

Related meta topic: https://meta.discourse.org/t/category-icons-dont-show-up-in-post-text/315851
if (!color || color?.match(/categoryColo(u*)r/g)) {
opts.color = `#${cat.color}`;
}
slugMap[cat.slug.toLowerCase()] = opts;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if there are multiple categories with empty slugs?

(I'm basing this hypothetical off https://github.com/discourse/discourse/blob/main/app/models/category.rb#L688)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a

if (slug && icon) {

Therefore, the category with an empty slug will not enter this statement. 😊

}
}
api.decorateCookedElement((elem) => {
const categorgHashtags = elem.querySelectorAll(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nit) categorgHashtags seems like a typo - maybe categoryHashtags?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

Comment on lines 210 to 218
if (match === "partial") {
if (!cat.slug.toLowerCase().includes(slug.toLowerCase())) {
continue;
}
} else {
if (cat.slug.toLowerCase() !== slug.toLowerCase()) {
continue;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(minor) perhaps this could be simplified to something a single guard clause?

Suggested change
if (match === "partial") {
if (!cat.slug.toLowerCase().includes(slug.toLowerCase())) {
continue;
}
} else {
if (cat.slug.toLowerCase() !== slug.toLowerCase()) {
continue;
}
}
if ((match === "partial" && !cat.slug.toLowerCase().includes(slug.toLowerCase())) || cat.slug.toLowerCase() !== slug.toLowerCase()) {
continue
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably could DRY out the repeated lowerCase calls too 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

@Lhcfl
Copy link
Contributor Author

Lhcfl commented Jul 16, 2024

@tyb-talks Can you review it again for me? I rewrote it using the new API of Discourse instead of calling decorateCookedElement 🙂

Copy link
Contributor

@tyb-talks tyb-talks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sweet, this is a simpler solution. lg2m! 🚢

@Lhcfl Lhcfl merged commit 7f44f9b into main Jul 16, 2024
4 checks passed
@Lhcfl Lhcfl deleted the add-icon-in-post branch July 16, 2024 02:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants