Git Product home page Git Product logo

Comments (7)

joelhoisko avatar joelhoisko commented on July 17, 2024 1

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.

from dot-hugo.

mehedi-sharif avatar mehedi-sharif commented on July 17, 2024

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

from dot-hugo.

tfsomrat avatar tfsomrat commented on July 17, 2024

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.

from dot-hugo.

zfk513 avatar zfk513 commented on July 17, 2024

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

from dot-hugo.

tfsomrat avatar tfsomrat commented on July 17, 2024

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.

from dot-hugo.

zfk513 avatar zfk513 commented on July 17, 2024

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,

from dot-hugo.

somratpro avatar somratpro commented on July 17, 2024

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

from dot-hugo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.