medium-export
Version:
a cli to convert medium stories to markdown
44 lines (43 loc) • 2.13 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import fs from 'fs';
import path from 'path';
import { program } from 'commander';
// * utils
import { resolveOptions, getMediumMarkdown } from './utils/index.js';
program
.description('Command to convert Medium story to markdown')
.option('-o, --output <string>', 'Output file|directory')
.option('-s, --story <string>', 'Medium story url')
.option('-a, --append', 'Remove author info', false)
.option('--no-author', 'Remove author info', true)
.option('--no-image', 'Remove images', true)
.action((opts) => __awaiter(void 0, void 0, void 0, function* () {
opts = yield resolveOptions(opts);
const { output, append } = opts;
const [title, markdown] = yield getMediumMarkdown(opts);
if (append) {
if (!fs.existsSync(output))
throw new Error(`'${output}' does not exist`);
if (fs.statSync(output).isDirectory())
throw new Error(`'${output}' is not a file`);
fs.appendFileSync(output, markdown, 'utf-8');
console.log(`Appended '${title}' to '${output}'`);
return;
}
const timestamp = new Date().toISOString().replace(/:|\./g, '-');
const filePath = path.resolve(output, `${timestamp}.md`);
if (!fs.existsSync(output))
fs.mkdirSync(output, { recursive: true });
fs.writeFileSync(filePath, markdown, 'utf-8');
console.log(`Converted '${title}' to markdown`);
}));
program.parse(process.argv);