PreTeXtBook / JS_core

Javascript for standard PreTeXt
0 stars 8 forks source link

Javascript to support search #49

Closed bnmnetp closed 2 years ago

bnmnetp commented 2 years ago

This Javascript assumes that the page has:

  1. The standard MathJax preamble
  2. The following js and css
    <script src="https://unpkg.com/lunr/lunr.js"></script>
    <script src="lunr-pretext-search-index.js"></script>
    <link rel="stylesheet" href="ptx_search.css" />

    This HTML which supplies a search box as well as a div to place the results in.

    <div class="searchbox">
      <div class="searchwidget">
        <label for="ptxsearch">Search Terms</label><input id="ptxsearch" type="text" name="terms" onchange="doSearch()"><button id="helpbutt" type="button" onclick="showHelp()">Show Help</button>
      </div>
    </div>
    <div id="helpme" class="helpbox" style="display: none;">
      <p>
        By default search will return documents with any of your terms.
      <ul>
        <li>To search for documents with all of your terms use + to get get multiple AND choice search for +multiple +choice</li>
        <li>to search and exclude pages with a particular word use - +multiple -choice means must include multiple and must not include choice.</li>
        <li>To search the title only of the page title:subgroups</li>
        <li>To make one term more important you can boost it with ^ so multiple^10 choice would look for multiple OR choice and give a much higher score to those that contain multiple</li>
        <li>To do a fuzzy search subgroups~2 would match words with an edit distance of 2 from subgroups. This is useful if you are not sure of the spelling.</li>
      </ul>
      Lunr does not support searching for phrases. But you can get close with the logical AND.
      </p>
    </div>
    <div>
      <h2>Page Results</h2>
      <ol id="searchresults" class="all_results"></ol>
    </div>