@mieweb/wikigdrive
Version:
Google Drive to MarkDown synchronization
55 lines (54 loc) • 2.81 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import lunr from 'lunr';
import { Controller, RouteErrorHandler, RouteGet, } from './Controller.js';
import { ShareErrorHandler } from './FolderController.js';
import { UserConfigService } from '../../google_folder/UserConfigService.js';
export class SearchController extends Controller {
constructor(subPath, filesService) {
super(subPath);
Object.defineProperty(this, "filesService", {
enumerable: true,
configurable: true,
writable: true,
value: filesService
});
}
async search(ctx) {
const driveId = await ctx.routeParamPath('driveId');
let queryParam = await ctx.routeParamQuery('q');
const googleFileSystem = await this.filesService.getSubFileService(driveId, '/');
const userConfigService = new UserConfigService(googleFileSystem);
await userConfigService.load();
const prefix = (userConfigService.config.transform_subdir || '').startsWith('/') ? `${userConfigService.config.transform_subdir}` : '';
const transformedFileSystem = await this.filesService.getSubFileService(driveId + '_transform', '');
const lunrData = await transformedFileSystem.readJson('/.private/lunr.json');
if (!lunrData?.index) {
return [];
}
const store = lunrData.store || {};
const lunrIndex = lunr.Index.load(lunrData.index);
queryParam = (queryParam || '').trim().replace(/:/g, ' ');
let result = lunrIndex.search(queryParam);
if (result.length === 0 && queryParam.indexOf('*') === -1) {
result = lunrIndex.search(queryParam.split(/\s+/g).map(w => w.length > 2 ? w + '*' : w).join(' '));
}
if (result.length === 0 && queryParam.replace(/[_-]*/g, '').length > 10) {
result = lunrIndex.search(queryParam.replace(/[_-]*/g, ''));
}
return { result: result.map((doc) => ({
path: prefix + doc.ref,
score: doc.score,
matchData: doc.matchData,
...store[doc.ref]
})) };
}
}
__decorate([
RouteGet('/:driveId'),
RouteErrorHandler(new ShareErrorHandler())
], SearchController.prototype, "search", null);