UNPKG

@codedoc/core

Version:

Create beautiful modern documentation websites.

48 lines 2.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GithubSearch$ = exports.GithubSearch = void 0; var ajax_1 = require("rxjs/ajax"); var rxjs_1 = require("rxjs"); var operators_1 = require("rxjs/operators"); var transport_1 = require("@connectv/sdh/transport"); var button_1 = require("../../page/toc/search/button"); function isCached(res) { return res.result !== undefined; } function GithubSearch(options, renderer) { var query = new rxjs_1.Subject(); var pick = new RegExp(options.pick); var drop = new RegExp(options.drop); var cache = {}; var results = query.pipe(operators_1.switchMap(function (q) { return (q in cache) ? rxjs_1.of({ result: cache[q] }) : // --> respond from cache if query in cache ajax_1.ajax.getJSON("https://api.github.com/search/code?q=" + encodeURIComponent(q) + "+in:file" // --> search in files + ("+path:" + options.root) // --> search in root directory + "+extension:md" // --> search in `.md` files + ("+repo:" + options.user + "/" + options.repo) // --> search in given repo of given user ).pipe(operators_1.catchError(function () { return rxjs_1.of(undefined); })); } // --> no sweat in case of error ), operators_1.map(function (res) { return res ? (isCached(res) ? res.result : // --> if cached result, no need to process res.items .map(function (item) { return item.path; }) .filter(function (x) { return pick.test(x); }) // --> check if it should be picked .filter(function (x) { return !drop.test(x); }) // --> check if it shouldn't be dropped .map(function (x) { return x.substr(0, x.length - 3); }) // --> remove the extension `.md` .map(function (x) { return x.substr(options.root.length); }) // --> remove the root path part .map(function (x) { return x === '/index' ? '/' : x; }) // --> turn `/index` to `/` ) : []; }), operators_1.share()); rxjs_1.zip(query, results).pipe(// --> for pairs of query and result... operators_1.tap(function (_a) { var query = _a[0], results = _a[1]; if (results.length > 0) // --> ...if the result is valid... cache[query] = results; // --> ...cache it. })).subscribe(); return renderer.create(button_1.ToCSearchBtn, { label: options.label, query: query, results: results }); } exports.GithubSearch = GithubSearch; exports.GithubSearch$ = transport_1.transport(GithubSearch); //# sourceMappingURL=search.js.map