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 Request: Search #104

Open
brandonprajogo opened this issue Jan 27, 2025 · 2 comments
Open

Feature Request: Search #104

brandonprajogo opened this issue Jan 27, 2025 · 2 comments
Labels
enhancement New feature or request

Comments

@brandonprajogo
Copy link

Hi, I'm curious, do you accept feature request? If you do, I want to request search feature to make user easier when search for spesific content.

I saw this feature is already present in Herman's Bear Blog, which would be nice if you also add this feature to Hugo Bear Blog theme since it also based on Bear Blog. Thanks!

Image
@janraasch janraasch added the enhancement New feature or request label Feb 8, 2025
@janraasch
Copy link
Owner

Hi @brandonprajogo,

Thanks for suggesting this feature!

I should mention that search functionality isn't part of the core Bear Blog platform, as it's intentionally JavaScript-free. Herman likely added search as a custom script to his blog. While I aim to keep hugo-bearblog JavaScript-free as well, you can add this functionality on your site yourself.

Here's how to implement a search feature:

  • Create a file at layout/partials/custom_body.html
  • Add this search implementation to the file:
 <script>
    //
    // Credit: Adapted with minor modifications from Herman's blog (https://herman.bearblog.dev/blog/)
    //
    // Select the content container and the blog posts list
    const mainContainer = document.querySelector('content');
    const blogPosts = document.querySelector('ul.blog-posts');
  
    if (blogPosts) {
    const searchInput = document.createElement('input');
    searchInput.type = 'text';
    searchInput.id = 'searchInput';
    searchInput.placeholder = 'Search...';
    searchInput.style.display = 'block';
    searchInput.style.marginTop = '16px';
  
    mainContainer.insertBefore(searchInput, blogPosts);
  
    // Add event listener to filter posts based on input
    searchInput.addEventListener('input', function() {
      const searchTerm = this.value.toLowerCase();
      const posts = document.querySelectorAll('.blog-posts li');
  
      posts.forEach(post => {
        const title = post.textContent.toLowerCase();
        post.style.display = title.includes(searchTerm) ? '' : 'none';
      });
    });
  }
</script>

Let me know if you need any clarification or run into any issues!

Best regards,
Jan

CC: @rokcso - This could be an interesting addition to hugo-bearblog-neo if it aligns with your vision! 💡

@rokcso
Copy link

rokcso commented Feb 8, 2025

@janraasch Thank you for your suggestion, I have been trying to implement this feature and it will be updated to hugo-bearblog-neo soon.

Image

CC: @brandonprajogo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants