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

Could the search be more powerful? #134

Closed
zfk513 opened this issue Mar 2, 2020 · 7 comments
Closed

Could the search be more powerful? #134

zfk513 opened this issue Mar 2, 2020 · 7 comments

Comments

@zfk513
Copy link

zfk513 commented Mar 2, 2020

now it only search for the title, could it also search on Markdown file?

@mehedi-sharif
Copy link
Contributor

Please try this theme
https://gethugothemes.com/products/godocs-hugo-documentation-theme/ .
It has advance search feature

@tfsomrat
Copy link
Contributor

tfsomrat commented Mar 3, 2020

hey @zfk513 ,

This search is also working for markdown file, And it has also a feature called search keyword, You can set a custom keyword for searching.

@zfk513
Copy link
Author

zfk513 commented Mar 3, 2020

hey @zfk513 ,

This search is also working for markdown file, And it has also a feature called search keyword, You can set a custom keyword for searching.

is it support Chinese?
and i update the theme,try on my local ,i still find it can only search the title,though the code show it search the content,thx for reply

@tfsomrat
Copy link
Contributor

tfsomrat commented Mar 4, 2020

It supports multilingual. you just need to add the Chinese language. And search can search for everything, but it just shows the title for going to this inner page.

@joelhoisko
Copy link

joelhoisko commented Mar 4, 2020

I've added my own custom search by overriding dot's search with my own:
global_search_script.hml:

<!-- JavaScript -->
<!-- 
This file overrides the default search of the dot theme.
Also gets imported in the head.html for more optimal performance.
-->
<script>
  $(function() {
    const projects = [
      {{ range $.Site.Pages }}
        {
          value: "{{ .Title }}",
          label: "{{ .Plain }}",
          url:"{{ .Permalink }}",
          description: "{{ .Description }}",
          keywords: "{{ .Keywords }}"
        },
      {{ end }}
    ];
    $( "#search" ).autocomplete({
        minLength: 3,
        // overrides the default search with our own
        source: (request, response) => {
            // the user input is in the term property. also remove special chars so that regex doesn't cry
            const sanitizedInput = request.term.replace(/[^\w\s]/gi, '');
            if (!sanitizedInput) {
              return response();
            }
            const substrRegex = new RegExp(sanitizedInput, 'i');
            return response(
              projects.filter(page => {
                // filter out contentless pages like _index.md's
                if (page.label) {
                  // match with either the pages title or keywords.
                  // Note that the keywords include "[]" at the start and end of them
                  return substrRegex.test(page.value) || substrRegex.test(page.keywords);
                }
            }));
        },
        select: function(event, ui) {
          // takes you to the url when you select it, also works with enter
          window.location.href = ui.item.url;
        },
    })
    .data( "ui-autocomplete" )._renderItem = function( ul, item ) {
      return $( "<li>" )
      .append(`<a href="${item.url}">${item.value}</a>`)
      .append(`<a href="${item.url}" style="font-size: 80%">${item.description}</a>`)
      .appendTo( ul );
    };
  });
</script>

You have to add it as a partial to the project so that you can override the dots search with it in head.tml and all the pages that use the search bar. Putting it into header of the document is a bit nicer as then the whole big list of all the pages gets cached by the browser.

Here's the change we did to our own page to make it work.

@zfk513
Copy link
Author

zfk513 commented Mar 5, 2020

It supports multilingual. you just need to add the Chinese language. And search can search for everything, but it just shows the title for going to this inner page.

thanks for reply ,it really works! ,but i have couple questions,

  1. if we can search the whole content,why should we have keywords?
  2. it is possible to add another line under the title when we searching ,like using the search engine,and yellow light out the search word in some context ?cause only see the title maybe not help,

@somratpro
Copy link
Contributor

Did you solve your problem? I am going to close this issue, if you need more help, don't hesitate to reopen this issue.

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

No branches or pull requests

5 participants