UNPKG

@mieweb/wikigdrive

Version:

Google Drive to MarkDown synchronization

101 lines (100 loc) 3.31 kB
export const LINKS_NAME = '.wgd-local-links.csv'; export class LocalLinks { constructor(transformFileService) { Object.defineProperty(this, "transformFileService", { enumerable: true, configurable: true, writable: true, value: transformFileService }); Object.defineProperty(this, "links", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "notGenerated", { enumerable: true, configurable: true, writable: true, value: true }); } async load() { if (!await this.transformFileService.exists(LINKS_NAME)) { this.notGenerated = true; this.links = []; return; } const content = await this.transformFileService.readFile(LINKS_NAME) || ''; const rows = content.split('\n').map(row => row.trim()).filter(row => !!row); rows.shift(); const groups = {}; for (const row of rows) { const cells = row.split(';'); if (!groups[cells[0]]) { groups[cells[0]] = { fileId: cells[0], fileName: cells[1], links: [] }; } groups[cells[0]].links.push(cells[2]); } this.notGenerated = false; this.links = Object.values(groups); } append(fileId, fileName, links) { const link = this.links.find(l => l.fileId === fileId); if (link) { link.fileName = fileName; link.links = links; } else { this.links.push({ fileId, fileName, links }); } } getBackLinks(fileId) { const retVal = new Set(); for (const link of this.links) { for (const targetLink of link.links) { if (targetLink === 'gdoc:' + fileId) { retVal.add(link.fileId); } } } return Array.from(retVal) .map(fileId => ({ fileId, linksCount: this.links.find(link => link.fileId === fileId)?.links.length || 0 })); } getLinks(fileId) { if (this.notGenerated) { return false; } for (const link of this.links) { if (link.fileId === fileId) { const links = link.links .filter(link => link.startsWith('gdoc:')) .map(link => link.substring('gdoc:'.length).replace(/#.*/, '')); return links.map(fileId => ({ fileId, linksCount: this.links.find(link => link.fileId === fileId)?.links.length || 0 })); } } return []; } async save() { const content = 'source;name;dest'; const rowsContent = this.links.map(row => { return row.links.map(dest => { return `${row.fileId};${row.fileName};${dest}`; }).join('\n').trim(); }).join('\n'); await this.transformFileService.writeFile(LINKS_NAME, content + '\n' + rowsContent); } }