UNPKG

@vrerv/md-to-notion

Version:

An upload of markdown files to a hierarchy of Notion pages.

62 lines 3.3 kB
#!/usr/bin/env node "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("./index"); const commander_1 = require("commander"); const package_json_1 = require("../package.json"); const client_1 = require("@notionhq/client"); const sync_to_notion_1 = require("./sync-to-notion"); const REPL_TEXT = "${text}"; const REPL_LINK_PATH_FROM_ROOT = "${linkPathFromRoot}"; const REPL_GITHUB_PATH = "${githubPath}"; const GIT_HUB_LINK_REPLACEMENT = `[${REPL_TEXT}](https://github.com/${REPL_GITHUB_PATH}/${REPL_LINK_PATH_FROM_ROOT}?raw=true)`; const program = new commander_1.Command(); async function main(directory, options) { let replacer; if (options.linkReplacer) { replacer = (text, linkFromRootPath) => options.linkReplacer .replace("${text}", text) .replace("${linkPathFromRoot}", linkFromRootPath); } else if (options.useGithubLinkReplacer) { replacer = (text, linkFromRootPath) => GIT_HUB_LINK_REPLACEMENT.replace("${githubPath}", options.useGithubLinkReplacer) .replace("${text}", text) .replace("${linkPathFromRoot}", linkFromRootPath); } const dir = (0, index_1.readMarkdownFiles)(directory, path => { const exclude = options.exclude || "node_modules"; const include = options.include || path; return path.includes(include) && !path.includes(exclude); }, replacer); if (options.verbose) { (0, index_1.printFolderHierarchy)(dir); } if (dir) { const notion = new client_1.Client({ auth: options.token }); if (options.renew) { await (0, sync_to_notion_1.archiveChildPages)(notion, options.pageId); } const linkMap = await (0, sync_to_notion_1.collectCurrentFiles)(notion, options.pageId); await (0, index_1.syncToNotion)(notion, options.pageId, dir, linkMap, options.delete); } console.log("Sync complete!"); } program .version(package_json_1.version) .description(package_json_1.description) .argument("<directory>", "Directory containing markdown files") .option("-t, --token <token>", "Notion API Token, default is environment variable NOTION_API_TOKEN", process.env["NOTION_API_TOKEN"]) .option("-p, --page-id <id>", "Target Notion root page ID, default is env MD_TO_NOTION_PAGE_ID", process.env["MD_TO_NOTION_PAGE_ID"]) .option("-i, --include <text>", "Scan only path includes text, default is all files") .option("-r, --link-replacer <replacement>", "Custom link replacer string.\n" + `Use ${REPL_TEXT}, ${REPL_LINK_PATH_FROM_ROOT} to replace text and link.\n` + "Try -g if you want to use GitHub raw link") .option("-g, --use-github-link-replacer <githubPath>", "Replace links with raw GitHub links.\n" + "<githubPath> will be 'vrerv/md-to-notion/blob/main' for example.\n" + `This is short version of -r '${GIT_HUB_LINK_REPLACEMENT.replace(REPL_GITHUB_PATH, "<githubPath>")}' option`) .option("-v, --verbose", "Print folder hierarchy", false) .option("-d, --delete", "Delete pages in Notion that don't exist locally", false) .option("-n, --renew", "Delete all pages in Notion before sync", false) .action(main); program.parse(process.argv); //# sourceMappingURL=md-to-notion-cli.js.map