Skip to content

Configuration

忘忧北萱草 edited this page Apr 24, 2020 · 3 revisions

When initializing TidSearch, you can pass in the following parameters:

input (HTMLElement|String) [optional]

An <input> or similar element. TidSearch will automatically listen to its oninput and onpaste events (on IE8 is onpropertychange event), and immediately display the search content.

output (HTMLElement|String) [required]

The container element that holds the search results is generally <ul> or <div>.

json (String|JSON) [required]

The URL path of search.json, or you can load the JSON file in advance and pass in the parsed result.

template (String) [optional]

The presentation style template of the search results needs to contain complete HTML tags. Use {...} to represent page attributes in the template.

E.g.

<li> <a href="{url}"> {title} </a> </li>

Among them, {url} and {title} will be replaced with the address and title of the search result. Similarly, you can also use {category}, {tags}, {date}, {content}, etc. Not only that, you can also add custom fields in search.json, and reference them here in the form of {field_name} .

The default template is:

<p> <a href="{url}"> {title} </a> </p>

templateMiddleWare (function: Object, String-> String) [optional]

Specifies how to get a field from a JSON object. The default version is as follows:

function defaultTemplateMiddleware (page, field) {
    return page[field];
}

You can use this configuration item to achieve some display effects, for example, for full-text search containing content, only the first 100 words of the text are displayed.

function customTemplateMiddleware (page, field) {
    if (field === 'content')
        return page.content.slice(0, 100) + '...';
    return page[field];
}

noResult (String)

The text displayed when the search result is not found. It can be a plain text or HTML code.

segmenter (function: String -> Promise<Array[String]>) [optional]

Custom word breaker.

If this item is not passed in, the default tokenizer will call the API of pullword to extract keywords and remove stop words. Currently, it supports Chinese and English. (Thanks for the online word segmentation API provided by Dr. Liang Bin of Tsinghua University and the remote data interface proxy service provided by @xCss.)

The tokenizer is a function that takes a string parameter and returns a Promise, which should return an array of strings in resolve, representing the extracted keyword list.

If you are not used to the asynchronous form, you can wrap the synchronous tokenizer as asynchronous as follows:

function asyncCustomSegmenter (str) {
    const result = customSegmenter (str); // Call the synchronous tokenizer here, the return value is a string array
    return Promise.resolve (result); // Wrap as Promise
}

match (function: Array[String], Object-> Number) [optional]

Custom matching function. The matching function accepts two parameters: a keyword list and an object that stores information about a page in JSON. The return value is a number, which represents the score of matching between the keyword and the page. The final search results will be displayed in the order of matching score from high to low, and pages with matching score less than or equal to 0 will not be displayed.

The default matching function searches keywords in title, category, tags, and content, and when each keyword appears in the above four positions, it will get 3 points, 2 points, 2 points, and 1 point, and use the total score as the matching score.

paginator (function: Array[String]-> Array [Array[String]]) [optional]

Custom pager. The paging function has not yet been implemented.

afterSearch (function: Array[Object]-> void) [optional]

The operation performed after each search. The incoming parameter is the list of matched pages.