{"id":16874520,"url":"https://github.com/joway/hexo-tokenize-search","last_synced_at":"2025-04-05T11:43:05.754Z","repository":{"id":57263802,"uuid":"56900787","full_name":"joway/hexo-tokenize-search","owner":"joway","description":"hexo 分词 搜索插件","archived":false,"fork":false,"pushed_at":"2019-04-09T10:54:36.000Z","size":6,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-14T05:04:09.785Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://joway.wang/search/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joway.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-04-23T04:43:21.000Z","updated_at":"2022-02-17T08:53:29.000Z","dependencies_parsed_at":"2022-08-25T02:51:52.014Z","dependency_job_id":null,"html_url":"https://github.com/joway/hexo-tokenize-search","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joway%2Fhexo-tokenize-search","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joway%2Fhexo-tokenize-search/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joway%2Fhexo-tokenize-search/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joway%2Fhexo-tokenize-search/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joway","download_url":"https://codeload.github.com/joway/hexo-tokenize-search/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247332526,"owners_count":20921852,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-13T15:33:08.018Z","updated_at":"2025-04-05T11:43:05.729Z","avatar_url":"https://github.com/joway.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# hexo-tokenize-search\n\n![](https://img.shields.io/npm/v/hexo-tokenize-search.svg)\n\n[Website Demo](https://joway.wang/search/)\n\n[Json Demo](https://joway.wang/search.json)\n\n[中文: Hexo折腾记——静态站点搜索](https://joway.wang/posts/Hexo/2016-04-23-hexo-search.html) \n\n## Dependence\n\nTokenize Dependence: [Segment](https://github.com/leizongmin/node-segment)\n\nSupports chinese and english tokenize .\n\n## Installation\n\n    $ npm install hexo-tokenize-search --save\n\n## Usage :\n\n### Config the search json\n\nYou can custom the json path in your global _config.xml\n\n\tsearch:\n\t  path: search.json\n\nWhen the plugin is installed , it will auto generator search.json (by default) in your root context.\n\nThe json is a array and each element will be included 5 field like this :\n``` json\n[\n{\n\"title\": \"xxxxxx\",\n\"url\": \"xxxxxx\" ,\n\"content\": \"xxxxxx\" ,\n\"title_tokenize\": \"xx xx xx xx\",\n\"content_tokenize\": \"xx xx xx xx\"\n},\n{\n...\n}\n]\n```\n\nI removed html tags in the content field and remove punctuations in the tokenize field .\n\n### Use the search.json\n\nYou can use your query to custom sort the json array.\n\nThere is my simple example:\n\n\u003e search.html\n\n``` html\n\u003cform action=\"/search\" method=\"get\" style=\"text-align: center\"\u003e\n    \u003cinput type=\"text\" class=\"st-default-search-input search\" id=\"search\"\n           placeholder=\" Search...\" name=\"query\" style=\"height: 40px\"\u003e\n\u003c/form\u003e\n\u003cdiv id='result'\u003e\u003c/div\u003e\n\u003cscript src='/js/search.js'\u003e\u003c/script\u003e\n```\n\n\u003e search.js\n\n\tfunction getParameterByName(name, url) {\n\t    if (!url) url = window.location.href;\n\t    name = name.replace(/[\\[\\]]/g, \"\\\\$\u0026\");\n\t    var regex = new RegExp(\"[?\u0026]\" + name + \"(=([^\u0026#]*)|\u0026|#|$)\"),\n\t        results = regex.exec(url);\n\t    if (!results) return null;\n\t    if (!results[2]) return '';\n\t    return decodeURIComponent(results[2].replace(/\\+/g, \" \"));\n\t}\n\t\n\tfunction calcScore(key, tokenize) {\n\t    var score = 0;\n\t    for (var x = 0; x \u003c tokenize.length; ++x) {\n\t        if (tokenize[x] == null) {\n\t            break;\n\t        }\n\t        for (var i = 0; i \u003c key.length; ++i) {\n\t            for (var t = 0; t \u003c tokenize[x].length; ++t) {\n\t                if (tokenize[x][t] == key[i]) {\n\t                    score++;\n\t                }\n\t            }\n\t\n\t        }\n\t    }\n\t    return score;\n\t}\n\t\n\tfunction addCountToJson(key, json) {\n\t    for (var i = 0; i \u003c json.length; ++i) {\n\t        // 题目匹配权重100 , 内容匹配权重1 , 匹配算法: 单字匹配 (待优化)\n\t        json[i]['score'] = calcScore(key, json[i].content_tokenize) + calcScore(key, json[i].title_tokenize) * 100;\n\t    }\n\t}\n\t\n\t\n\t// 按 json 元素 score 值进行排序\n\tfunction quickSort(json) {\n\t    if (json.length \u003c= 1) {\n\t        return json;\n\t    }\n\t    var pivotIndex = Math.floor(json.length / 2);\n\t    var pivot = json.splice(pivotIndex, 1)[0]; // 基准\n\t    var left = [];\n\t    var right = [];\n\t    for (var i = 0; i \u003c json.length; i++) {\n\t        if (json[i].score \u003c pivot.score) {\n\t            left.push(json[i]);\n\t        } else {\n\t            right.push(json[i]);\n\t        }\n\t    }\n\t    return quickSort(left).concat(pivot, quickSort(right));\n\t}\n\t\n\twindow.onload = function () {\n\t\n\t    var query = getParameterByName('query');\n\t    if (query == null) {\n\t        query = '';\n\t    }\n\t\n\t    $(\".st-default-search-input\").val(query);\n\t    $.get(\"../search.json\", function (result) {\n\t        addCountToJson(query, result);\n\t        result = quickSort(result);\n\t        for (var i = result.length - 1; i \u003e= 0; --i) {\n\t            $(\"#result\").append('\u003ca href=\"' + result[i].url + '\"' + ' target=\"_blank\"' + '\u003e\u003cli\u003e' + result[i].title + ' 评分: ' + result[i].score + '\u003c/li\u003e\u003c/a\u003e');\n\t        }\n\t    });\n\t};\n\n## Thanks\n\n[hexo-search](https://github.com/forsigner/hexo-search)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoway%2Fhexo-tokenize-search","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoway%2Fhexo-tokenize-search","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoway%2Fhexo-tokenize-search/lists"}