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.

336 lines 10.9 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BlockTransformers = exports.ObjectTransformers = exports.getMedia = exports.applyAnnotations = void 0; const media_service_1 = __importDefault(require("./media_service")); function applyAnnotations(text, annotations) { let out = text; if (annotations.code) out = `\`${out}\``; if (annotations.strikethrough) out = `~~${out}~~`; if (annotations.italic) out = `*${out}*`; if (annotations.bold) out = `**${out}**`; return out; } exports.applyAnnotations = applyAnnotations; function transformAllObjectsWithPrefix(objects, prefix) { return `${prefix}${exports.ObjectTransformers.transform_all(objects)}`; } /** * @visibleForTesting */ function getMedia(object, rule) { const captionMd = transformAllObjectsWithPrefix(object.caption, ''); let source = ''; if (object.type === 'external') { source = object.external.url; } else { source = media_service_1.default.instance.stageFetchRequest(object.file.url, rule); } return { src: source, captionMarkdown: captionMd, }; } exports.getMedia = getMedia; /** * The object transformers. * * The keys are the object types and the value is the object type to * Markdown transformer. * * These can be overwritten to provide custom implementations. * * See all object types here: src/models/notion_objects.ts */ exports.ObjectTransformers = { text: (object) => { let out = object.text.content; if (object.text.link) out = `[${out}](${object.text.link.url})`; return applyAnnotations(out, object.annotations); }, mention: (object) => { let out = ''; if (object.mention.type === 'user') { const user = object.mention.user; if (user.type === 'person' || user.type === 'bot') { out = user.name; } else { out = object.plain_text; } if (object.href) out = `[${out}](${object.href})`; } else if (object.mention.type === 'date') { const date = object.mention.date; if (date.end != null) out = `${date.start} to ${date.end}`; else out = date.start; if (object.href) out = `[${out}](${object.href})`; } else if (object.mention.type === 'page') { const page = object.mention.page; out = `[${object.plain_text}](:::pathTo:::${page.id}:::)`; } else if (object.mention.type === 'database') { out = `[${object.plain_text}](${object.href})`; } return applyAnnotations(out, object.annotations); }, equation: (object) => { let out = `$${object.equation.expression}$`; if (object.href) out = `[${out}](${object.href})`; return applyAnnotations(out, object.annotations); }, transform_all: (objects) => { var _a; return (_a = objects === null || objects === void 0 ? void 0 : objects.map((object) => exports.ObjectTransformers[object.type](object)).join('')) !== null && _a !== void 0 ? _a : ''; }, }; /** * The block transformers. * * The keys are the block types and the value is the NotionBlock to * Markdown controller for the block. * * These can be overwritten to provide custom implementations. */ exports.BlockTransformers = { paragraph: (block) => { const blk = block; if (blk.type === 'paragraph') { return transformAllObjectsWithPrefix(blk.paragraph.rich_text, ''); } return ''; }, heading_1: (block) => { const blk = block; if (blk.type === 'heading_1') { return transformAllObjectsWithPrefix(blk.heading_1.rich_text, '# '); } return ''; }, heading_2: (block) => { const blk = block; if (blk.type === 'heading_2') { return transformAllObjectsWithPrefix(blk.heading_2.rich_text, '## '); } return ''; }, heading_3: (block) => { const blk = block; if (blk.type === 'heading_3') { return transformAllObjectsWithPrefix(blk.heading_3.rich_text, '### '); } return ''; }, bulleted_list_item: (block) => { const blk = block; if (blk.type === 'bulleted_list_item') { return transformAllObjectsWithPrefix(blk.bulleted_list_item.rich_text, '- '); } return ''; }, numbered_list_item: (block, _) => { const blk = block; if (blk.type === 'numbered_list_item') { return transformAllObjectsWithPrefix(blk.numbered_list_item.rich_text, `1. `); } return ''; }, to_do: (block) => { const blk = block; if (blk.type === 'to_do') { const check = blk.to_do.checked ? 'X' : ' '; return transformAllObjectsWithPrefix(blk.to_do.rich_text, `- [${check}] `); } return ''; }, toggle: (block) => { const blk = block; if (blk.type === 'toggle') { return transformAllObjectsWithPrefix(blk.toggle.rich_text, ''); } return ''; }, child_page: (block) => { const blk = block; if (blk.type === 'child_page') { const id = block.id.split('-').join(''); return `[${blk.child_page.title}](https://www.notion.so/${id})`; } return ''; }, child_database: (block) => { const blk = block; if (blk.type === 'child_database') { const id = block.id.split('-').join(''); return `[${blk.child_database.title}](https://www.notion.so/${id})`; } return ''; }, embed: (block) => { const blk = block; if (blk.type === 'embed') { const caption = transformAllObjectsWithPrefix(blk.embed.caption, ''); return `[${caption}](${blk.embed.url} ':include')`; } return ''; }, image: (block, rule) => { const blk = block; if (blk.type === 'image') { const media = getMedia(blk.image, rule); return `![${media.captionMarkdown}](${media.src} "${media.captionMarkdown}")`; } return ''; }, video: (block, rule) => { const blk = block; if (blk.type === 'video') { const media = getMedia(blk.video, rule); return `[${media.captionMarkdown}](${media.src} ':include')`; } return ''; }, file: (block, rule) => { const blk = block; if (blk.type === 'file') { const media = getMedia(blk.file, rule); return `[${media.captionMarkdown}](${media.src} ':include')`; } return ''; }, pdf: (block, rule) => { const blk = block; if (blk.type === 'pdf') { const media = getMedia(blk.pdf, rule); return `[${media.captionMarkdown}](${media.src} ':include')`; } return ''; }, audio: (block, rule) => { const blk = block; if (blk.type === 'audio') { const media = getMedia(blk.audio, rule); return `[${media.captionMarkdown}](${media.src} ':include')`; } return ''; }, bookmark: (block) => { const blk = block; if (blk.type === 'bookmark') { const url = blk.bookmark.url; return `[${url}](${url})`; } return ''; }, callout: (block) => { var _a; const blk = block; if (blk.type === 'callout') { const text = transformAllObjectsWithPrefix(blk.callout.rich_text, ''); if (((_a = blk.callout.icon) === null || _a === void 0 ? void 0 : _a.type) === 'emoji') { const icon = blk.callout.icon.emoji; return `> ${icon} ${text}`; } return `> ${text}`; } return ''; }, quote: (block) => { const blk = block; if (blk.type === 'quote') { const text = transformAllObjectsWithPrefix(blk.quote.rich_text, ''); return `> ${text}`; } return ''; }, equation: (block) => { const blk = block; if (blk.type === 'equation') { const expression = blk.equation.expression; return `$$${expression}$$`; } return ''; }, divider: (block) => { return '---'; }, table_of_contents: (block) => { return ''; }, breadcrumb: (block) => { return ''; }, code: (block) => { const blk = block; if (blk.type === 'code') { const text = transformAllObjectsWithPrefix(blk.code.rich_text, ''); return '```' + blk.code.language + '\n' + text + '\n```'; } return ''; }, unsupported: (block) => { return 'Unsupported'; }, }; class MarkdownService { genMarkdownForBlocks(blocks, rule) { return this.genMarkdownForBlocksWithIndent(blocks, rule, ''); } genMarkdownForBlocksWithIndent(blocks, rule, indent) { let out = ''; for (const block of blocks) { const transformer = this.getTransformerForBlock(block); const markdown = this.getMarkdownForBlock(block, rule, transformer); if (out.length === 0) { out = `${indent}${markdown}\n`; } else { out = `${out}\n${indent}${markdown}\n`; } const childrenMarkdown = this.genMarkdownForBlocksWithIndent(block.children, rule, `${indent} `).trimEnd(); if (childrenMarkdown.length !== 0) { out = `${out}\n${childrenMarkdown}\n`; } } return out; } getTransformerForBlock(block) { for (const blockType of Object.keys(exports.BlockTransformers)) { if (blockType === block.data.type) { return exports.BlockTransformers[blockType]; } } return undefined; } getMarkdownForBlock(block, rule, transformer) { if (transformer) { return transformer(block.data, rule); } else { return `Unknown Block: ${block.data.type}`; } } static get instance() { var _a; return (_a = this._instance) !== null && _a !== void 0 ? _a : (this._instance = new MarkdownService()); } static setMockedInstance(instance) { this._instance = instance; } } exports.default = MarkdownService; //# sourceMappingURL=markdown_service.js.map