UNPKG

motionlink-cli

Version:

Making it easy to use Notion as a Content Management system for personal websites, portfolios, blogs, business homepages, and other kinds of static websites.

84 lines 3.58 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const file_system_service_1 = __importDefault(require("./file_system_service")); const get_relative_path_1 = __importDefault(require("get-relative-path")); const logger_1 = require("../logger"); /** * Buffers a file write and only writes it to the file system once all the links * it has to other pages have been resolved. * * Notion allows users to mention other pages in their pages. When the * Markdown for a page with such links is generated, we want to maintain these * links. However, by the time the page is generated, some of the pages * it mentions might not be generated yet, which means we will not know * their paths in the output directory. To fix this we allow the markdown * transfomers, and config files, to use path placeholders when linking to a * a page that is yet to be generated. * * A path placeholder has the form `:::pathTo:::page_id:::`, where page_id is the id * of the page being linked to. When the text content of a page is submitted, the * `PostProcessingService` looks for all the other buffered pages that link * to the page and overwrites the path placelders with the actual path to the page, * such that a page is only flushed to the file system once all the path placeholders * it has have been resolved. * * The {@link flush} method can be used to force all buffered pages to be written to the * file system. This should be called when there are no more pages expected to be * generated. * * Using post processing to solve the page linking problem only works if the pages * being linked to are going to be generated. If not, the links will be broken. */ class PostProcessingService { constructor() { this.pagePaths = []; this.pagesWithLinks = []; } submit(content, pgPath, pgId) { this.pagePaths.push({ pageId: pgId, path: pgPath, }); if (content.indexOf(':::pathTo:::') > 0) { this.pagesWithLinks.push({ path: pgPath, data: content, }); } else { file_system_service_1.default.instance.writeStringToFile(content, pgPath); (0, logger_1.getLogger)().logPageFlushed(pgPath); } } flush() { for (const pagePath of this.pagePaths) { for (const pageWithLink of this.pagesWithLinks) { const link = encodeURI(this.getRelativeLink(pageWithLink.path, pagePath.path)); pageWithLink.data = pageWithLink.data.replace(`:::pathTo:::${pagePath.pageId}:::`, link); } } for (const page of this.pagesWithLinks) { file_system_service_1.default.instance.writeStringToFile(page.data, page.path); (0, logger_1.getLogger)().logPageFlushed(page.path); } } getRelativeLink(from, to) { if (to.indexOf('/') < 0 || to.startsWith('./')) { // Path is already relative return to; } return (0, get_relative_path_1.default)(from, to); } static get instance() { var _a; return (_a = this._instance) !== null && _a !== void 0 ? _a : (this._instance = new PostProcessingService()); } static setMockedInstance(instance) { this._instance = instance; } } exports.default = PostProcessingService; //# sourceMappingURL=post_processing_service.js.map