mvw-search-index 2.2.6 → 2.2.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -124
- package/js/cli.js +3 -3
- package/js/cli.js.map +1 -1
- package/js/index.d.ts +9 -4
- package/js/index.js +8 -7
- package/js/index.js.map +1 -1
- package/package-lock.json +2426 -1800
- package/package.json +17 -17
- package/.github/workflows/nodejs.yml +0 -26
- package/.github/workflows/npm-publish.yml +0 -25
- package/.github/workflows/pr.yml +0 -31
- package/.github/workflows/update.yml +0 -41
package/README.md
CHANGED
|
@@ -4,28 +4,38 @@
|
|
|
4
4
|
[](https://www.npmjs.com/package/mvw-search-index)
|
|
5
5
|
|
|
6
6
|
## About
|
|
7
|
+
|
|
7
8
|
Small module to generate a [lunr](http://lunrjs.com/) index with result store.
|
|
8
9
|
|
|
9
|
-
Can e.g. be used to generate a search index for a static website, generated with hugo, jekyll,
|
|
10
|
+
Can e.g. be used to generate a search index for a static website, generated with hugo, jekyll, gatsby or manually.
|
|
10
11
|
|
|
11
12
|
## Usage
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
|
|
14
|
+
First you have to install this package, then create the index that you use on e.g. a search page. Find samples for
|
|
15
|
+
everything in the following chapters.
|
|
14
16
|
|
|
15
17
|
### Installation
|
|
18
|
+
|
|
16
19
|
Use `npm install --save-dev mvw-search-index` to add the dependency to your `package.json`.
|
|
17
20
|
|
|
18
21
|
### Index creation
|
|
22
|
+
|
|
19
23
|
There are multiple possibilities to generate the script. Some are demonstrated in the following sub chapters:
|
|
24
|
+
|
|
20
25
|
- From CLI
|
|
21
26
|
- From TypeScript
|
|
22
27
|
- From nodejs
|
|
23
28
|
|
|
24
29
|
#### From CLI
|
|
25
|
-
`mvw-search-index <glob> <destination> [css-selector]`
|
|
26
30
|
|
|
27
|
-
|
|
28
|
-
|
|
31
|
+
```bash
|
|
32
|
+
mvw-search-index <glob> <destination> [css-selector]
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
For example:
|
|
36
|
+
```bash
|
|
37
|
+
mvw-search-index ./build/**/*.html ./build/index.json
|
|
38
|
+
```
|
|
29
39
|
|
|
30
40
|
Or from an npm script e.g. for a hugo page (has to be executed after page creation, obviously):
|
|
31
41
|
|
|
@@ -37,17 +47,20 @@ Or from an npm script e.g. for a hugo page (has to be executed after page creati
|
|
|
37
47
|
}
|
|
38
48
|
```
|
|
39
49
|
|
|
40
|
-
#### From
|
|
41
|
-
|
|
50
|
+
#### From TypeScript
|
|
51
|
+
|
|
52
|
+
```ts
|
|
42
53
|
import { SearchIndex } from "mvw-search-index";
|
|
43
|
-
|
|
54
|
+
|
|
44
55
|
let index = SearchIndex.createFromHtml(files, bodySelector);
|
|
45
56
|
// or: let index = SearchIndex.createFromInfo(info);
|
|
46
57
|
// or: let index = SearchIndex.createFromGlob(glob, bodySelector, cb);
|
|
58
|
+
|
|
47
59
|
fs.writeFileSync("index.json", JSON.stringify(index));
|
|
48
60
|
```
|
|
49
61
|
|
|
50
|
-
### From
|
|
62
|
+
### From node.js
|
|
63
|
+
|
|
51
64
|
```js
|
|
52
65
|
"use strict";
|
|
53
66
|
|
|
@@ -55,124 +68,21 @@ const index = require("mvw-search-index");
|
|
|
55
68
|
const fs = require("fs")
|
|
56
69
|
|
|
57
70
|
index.SearchIndex.createFromGlob("./build/**/*.html",
|
|
58
|
-
|
|
59
|
-
|
|
71
|
+
"main",
|
|
72
|
+
(index) => fs.writeFileSync("./static/suche/index.json",
|
|
73
|
+
JSON.stringify(index)));
|
|
60
74
|
```
|
|
61
75
|
|
|
62
|
-
### Sample usage of the created
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
<html lang="en">
|
|
68
|
-
<head>
|
|
69
|
-
<meta charset="utf-8">
|
|
70
|
-
<meta content="IE=edge" http-equiv="X-UA-Compatible">
|
|
71
|
-
<meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
|
|
72
|
-
<title>Suche</title>
|
|
73
|
-
<style>
|
|
74
|
-
form {
|
|
75
|
-
text-align: center
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
.mvw-search-field {
|
|
79
|
-
font-size: 3rem;
|
|
80
|
-
margin: 15px 0 40px;
|
|
81
|
-
max-width: 100%
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
.results {
|
|
85
|
-
padding-left: 0
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
.results li {
|
|
89
|
-
list-style: none
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
.results a:first-of-type {
|
|
93
|
-
display: block;
|
|
94
|
-
font-size: 2.2rem;
|
|
95
|
-
padding: 15px 0 2px
|
|
96
|
-
}
|
|
97
|
-
</style>
|
|
98
|
-
</head>
|
|
99
|
-
|
|
100
|
-
<body itemscope itemtype="http://schema.org/SearchResultsPage">
|
|
101
|
-
<h1 class="page-header">Suche</h1>
|
|
102
|
-
|
|
103
|
-
<div>
|
|
104
|
-
<form itemprop="potentialAction" itemscope itemtype="http://schema.org/SearchAction">
|
|
105
|
-
<meta content="search.html?q={query}" itemprop="target">
|
|
106
|
-
<input class="mvw-search-field" itemprop="query-input" name="query" placeholder="Suche..." type="search">
|
|
107
|
-
</form>
|
|
108
|
-
|
|
109
|
-
<ol class="results"></ol>
|
|
110
|
-
</div>
|
|
111
|
-
|
|
112
|
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
|
|
113
|
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/lunr.js/2.0.1/lunr.min.js"></script>
|
|
114
|
-
<script>
|
|
115
|
-
var MVW;
|
|
116
|
-
(function (MVW) {
|
|
117
|
-
var Search;
|
|
118
|
-
(function (Search) {
|
|
119
|
-
var index;
|
|
120
|
-
var store;
|
|
121
|
-
function handleSearch(e) {
|
|
122
|
-
var query = $("input.mvw-search-field").val();
|
|
123
|
-
var result = index.search(query);
|
|
124
|
-
var resultContainer = $(".results");
|
|
125
|
-
if (result.length === 0) {
|
|
126
|
-
resultContainer.hide();
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
resultContainer.empty();
|
|
130
|
-
for (var item in result) {
|
|
131
|
-
if (result.hasOwnProperty(item)) {
|
|
132
|
-
var ref = result[item].ref;
|
|
133
|
-
var i = "<li><a href=\"" + ref + ".html\">" + store[ref].title + "</a><span>" + store[ref].description + "</span></li>";
|
|
134
|
-
resultContainer.append(i);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
resultContainer.show();
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
function getParameterByName(name) {
|
|
141
|
-
var url = window.location.href;
|
|
142
|
-
name = name.replace(/[\[\]]/g, "\\$&");
|
|
143
|
-
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)");
|
|
144
|
-
var results = regex.exec(url);
|
|
145
|
-
if (!results) {
|
|
146
|
-
return null;
|
|
147
|
-
}
|
|
148
|
-
if (!results[2]) {
|
|
149
|
-
return "";
|
|
150
|
-
}
|
|
151
|
-
return decodeURIComponent(results[2].replace(/\+/g, " "));
|
|
152
|
-
}
|
|
153
|
-
function initialize() {
|
|
154
|
-
$.getJSON("/index.json", function (data) {
|
|
155
|
-
index = lunr.Index.load(data.index);
|
|
156
|
-
store = data.store;
|
|
157
|
-
var query = getParameterByName("query") || getParameterByName("q");
|
|
158
|
-
var inputField = $("input.mvw-search-field");
|
|
159
|
-
if (query) {
|
|
160
|
-
inputField.val(query);
|
|
161
|
-
}
|
|
162
|
-
inputField.on("keyup", handleSearch);
|
|
163
|
-
handleSearch(null);
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
Search.initialize = initialize;
|
|
167
|
-
})(Search = MVW.Search || (MVW.Search = {}));
|
|
168
|
-
})(MVW || (MVW = {}));
|
|
169
|
-
$(function () { return MVW.Search.initialize(); });
|
|
170
|
-
</script>
|
|
171
|
-
</body>
|
|
172
|
-
</html>
|
|
173
|
-
```
|
|
76
|
+
### Sample usage of the created index
|
|
77
|
+
|
|
78
|
+
You can run `npm run serve` from this repository to run the sample site contained in [./demo](./demo).
|
|
79
|
+
|
|
80
|
+
This contains a very basic static website with a search form on the landing page `index.html`.
|
|
174
81
|
|
|
175
82
|
## Releases
|
|
83
|
+
|
|
84
|
+
- 2.2.10 updates dependencies
|
|
85
|
+
- 2.2.9 gets rid of vinyl and provides a demo application
|
|
176
86
|
- 2.1.4 - 2.1.12: dependency update
|
|
177
87
|
- 2.1.3: added support of glob on API
|
|
178
88
|
- 2.1.1: added CLI
|
package/js/cli.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
var
|
|
4
|
+
var commander_1 = require("commander");
|
|
5
5
|
var fs = require("fs");
|
|
6
6
|
var index_1 = require("./index");
|
|
7
|
-
program
|
|
8
|
-
.version("2.
|
|
7
|
+
commander_1.program
|
|
8
|
+
.version("2.2.8")
|
|
9
9
|
.arguments("<glob> <dest> [bodySelector]")
|
|
10
10
|
.action(function (glob, dest, bodySelector) {
|
|
11
11
|
index_1.SearchIndex.createFromGlob(glob, bodySelector, function (index) {
|
package/js/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../ts/cli.ts"],"names":[],"mappings":";;;AAEA,
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../ts/cli.ts"],"names":[],"mappings":";;;AAEA,uCAAoC;AACpC,uBAAyB;AAEzB,iCAAsC;AAEtC,mBAAO;KACJ,OAAO,CAAC,OAAO,CAAC;KAChB,SAAS,CAAC,8BAA8B,CAAC;KACzC,MAAM,CAAC,UAAC,IAAI,EAAE,IAAI,EAAE,YAAY;IAC/B,mBAAW,CAAC,cAAc,CAAC,IAAI,EAAE,YAAY,EAAE,UAAC,KAAK;QACnD,OAAA,EAAE,CAAC,aAAa,CAAC,IAAI,IAAI,cAAc,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IAA/D,CAA+D,CAChE,CAAC;AACJ,CAAC,CAAC;KACD,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC"}
|
package/js/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="lunr" />
|
|
2
|
-
|
|
2
|
+
/// <reference types="node" />
|
|
3
3
|
export declare interface IResultStore {
|
|
4
4
|
[key: string]: {
|
|
5
5
|
title: string;
|
|
@@ -17,12 +17,17 @@ export declare interface ISearchIndexResult {
|
|
|
17
17
|
index: lunr.Index;
|
|
18
18
|
store: IResultStore;
|
|
19
19
|
}
|
|
20
|
+
declare interface ReadFileWithContents {
|
|
21
|
+
contents: Buffer;
|
|
22
|
+
relative: string;
|
|
23
|
+
}
|
|
20
24
|
export declare class SearchIndex {
|
|
21
|
-
static createFromInfo(files: IFileInformation[]): ISearchIndexResult;
|
|
22
|
-
static createFromHtml(files: File[], bodySelector?: string): ISearchIndexResult;
|
|
23
|
-
static createFromGlob(glob: string, bodySelector: string, cb: (index: ISearchIndexResult) => void): void;
|
|
24
25
|
private readonly store;
|
|
25
26
|
private readonly index;
|
|
26
27
|
private constructor();
|
|
28
|
+
static createFromInfo(files: IFileInformation[]): ISearchIndexResult;
|
|
29
|
+
static createFromHtml(files: ReadFileWithContents[], bodySelector?: string): ISearchIndexResult;
|
|
30
|
+
static createFromGlob(glob: string, bodySelector: string, cb: (index: ISearchIndexResult) => void): void;
|
|
27
31
|
private getResult;
|
|
28
32
|
}
|
|
33
|
+
export {};
|
package/js/index.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.SearchIndex = void 0;
|
|
4
4
|
var cheerio = require("cheerio");
|
|
5
5
|
var globber = require("glob");
|
|
6
|
-
var
|
|
6
|
+
var fs = require("fs");
|
|
7
7
|
var lunr = require("lunr");
|
|
8
8
|
var SearchIndex = /** @class */ (function () {
|
|
9
9
|
function SearchIndex(files) {
|
|
@@ -33,11 +33,9 @@ var SearchIndex = /** @class */ (function () {
|
|
|
33
33
|
console.info(file.relative);
|
|
34
34
|
var dom = cheerio.load(file.contents.toString());
|
|
35
35
|
return {
|
|
36
|
-
body: dom(bodySelector || "body").
|
|
37
|
-
cheerio(elem).append(" ");
|
|
38
|
-
}).text().replace(/\s\s+/g, " "),
|
|
39
|
-
description: dom("meta[name='description']").attr("content"),
|
|
36
|
+
body: dom(bodySelector || "body").text().replace(/\s\s+/g, " "),
|
|
40
37
|
href: file.relative,
|
|
38
|
+
description: dom("meta[name='description']").attr("content"),
|
|
41
39
|
keywords: dom("meta[name='keywords']").attr("content"),
|
|
42
40
|
title: dom("head title").text(),
|
|
43
41
|
};
|
|
@@ -50,8 +48,11 @@ var SearchIndex = /** @class */ (function () {
|
|
|
50
48
|
throw err;
|
|
51
49
|
}
|
|
52
50
|
else {
|
|
53
|
-
var
|
|
54
|
-
|
|
51
|
+
var readFiles = files.map(function (file) { return ({
|
|
52
|
+
relative: file,
|
|
53
|
+
contents: fs.readFileSync(file)
|
|
54
|
+
}); });
|
|
55
|
+
cb(SearchIndex.createFromHtml(readFiles, bodySelector));
|
|
55
56
|
}
|
|
56
57
|
});
|
|
57
58
|
};
|
package/js/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../ts/index.ts"],"names":[],"mappings":";;;AAAA,iCAAmC;AACnC,8BAAgC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../ts/index.ts"],"names":[],"mappings":";;;AAAA,iCAAmC;AACnC,8BAAgC;AAChC,uBAAyB;AAEzB,IAAM,IAAI,GAAQ,OAAO,CAAC,MAAM,CAAC,CAAC;AA2BlC;IAIE,qBAAoB,KAAyB;QAA7C,iBAiBC;QAhBC,IAAI,CAAC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