UNPKG

@mieweb/wikigdrive

Version:

Google Drive to MarkDown synchronization

73 lines (72 loc) 3.39 kB
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 { Controller, RouteGet } from './Controller.js'; import { LocalLinks } from '../../transform/LocalLinks.js'; import { UserConfigService } from '../../google_folder/UserConfigService.js'; import { MarkdownTreeProcessor } from '../../transform/MarkdownTreeProcessor.js'; import { getContentFileService } from '../../transform/utils.js'; export class BackLinksController extends Controller { constructor(subPath, filesService) { super(subPath); Object.defineProperty(this, "filesService", { enumerable: true, configurable: true, writable: true, value: filesService }); } async getBackLinks(ctx) { const driveId = await ctx.routeParamPath('driveId'); const fileId = await ctx.routeParamPath('fileId'); const googleFileSystem = await this.filesService.getSubFileService(driveId, '/'); const userConfigService = new UserConfigService(googleFileSystem); await userConfigService.load(); const transformedFileSystem = await this.filesService.getSubFileService(driveId + '_transform', ''); const contentFileService = await getContentFileService(transformedFileSystem, userConfigService); const markdownTreeProcessor = new MarkdownTreeProcessor(contentFileService); await markdownTreeProcessor.load(); const localLinks = new LocalLinks(contentFileService); await localLinks.load(); const linksArr = localLinks.getLinks(fileId); if (linksArr === false) { return { backlinks: [], links: [], notGenerated: true }; } const links = []; for (const linkObj of linksArr) { const { fileId, linksCount } = linkObj; const [file] = await markdownTreeProcessor.findById(fileId); if (file) { links.push({ folderId: file.parentId, fileId: fileId, linksCount, path: file.path, name: file.fileName }); } } const backLinkFileIds = localLinks.getBackLinks(fileId); const backlinks = []; for (const backLinkObj of backLinkFileIds) { const { fileId, linksCount } = backLinkObj; const [file] = await markdownTreeProcessor.findById(fileId); if (file) { backlinks.push({ folderId: file.parentId, fileId: fileId, linksCount, path: file.path, name: file.fileName }); } } return { backlinks, links }; } } __decorate([ RouteGet('/:driveId/:fileId') ], BackLinksController.prototype, "getBackLinks", null);