diff --git a/index.js b/index.js index 0ee4e02..2567541 100755 --- a/index.js +++ b/index.js @@ -11,7 +11,10 @@ program .description('Converts Medium exported html files to markdown from a local directory.') .option('-d, --drafts', 'Convert drafts too.') .option('-f, --frontMatter', 'Add front-matter.') + .option('-h, --headers [headers...]', 'Specify additional Front Matter headers, for example "header: value"') .option('-i, --images', 'Download images in local directory.') + .option('-p, --prefixSlug ', 'Use a different prefix for the slug path, replaces /@username/') + .option('-s, --slugField ', 'Specfiy the name of the slug field.', 'slug') .action(workflow.processAll); // Convert from url has been removed. diff --git a/lib/reader.js b/lib/reader.js index c5d01ce..c31de2d 100644 --- a/lib/reader.js +++ b/lib/reader.js @@ -10,10 +10,9 @@ const frontMatterTemplate = { date: "", categories: [], keywords: [], - slug: "" }; -var readAll = function (filePath, frontMatterConfig) { +var readAll = function (filePath, frontMatterConfig, slugField, prefixSlug, headers) { const contents = fs.readFileSync(filePath); let $ = cheerio.load(contents); @@ -30,11 +29,12 @@ var readAll = function (filePath, frontMatterConfig) { const title = $('.p-name').text(); const subtitle = $('.p-summary[data-field="subtitle"]').text(); const date = $('.dt-published').attr('datetime'); - const slug = canonical ? url.parse(canonical).path : ''; + const path = canonical ? url.parse(canonical).path : ''; + const slug = !prefixSlug ? path : path.replace(/^\/[^\/]*/, prefixSlug).replace(/\/\//, '/'); // no tags available in the exported HTML files - const frontMatter = generateFrontMatter(title, subtitle, date, slug); + const frontMatter = generateFrontMatter(title, subtitle, date, slug, slugField, headers); return { html, frontMatter }; } @@ -70,12 +70,12 @@ var readAll = function (filePath, frontMatterConfig) { // }; // } -var generateFrontMatter = function (title, subtitle, date, slug, tags) { - const frontMatter = Object.assign({}, frontMatterTemplate); +var generateFrontMatter = function (title, subtitle, date, slug, slugField, headers, tags) { + const frontMatter = Object.assign({}, Object.assign(frontMatterTemplate, headers)); frontMatter.title = title.toString().replace(/\n/g, ''); frontMatter.description = subtitle ? subtitle.toString().replace(/\n/g, '') : ''; frontMatter.date = date ? date.toString() : ''; - frontMatter.slug = slug ? slug.toString() : ''; + frontMatter[slugField] = slug ? slug.toString() : ''; frontMatter.keywords = tags ? tags : []; const yml = yaml.safeDump(frontMatter); return yml; diff --git a/lib/workflow.js b/lib/workflow.js index 7b761f0..15a9cb3 100644 --- a/lib/workflow.js +++ b/lib/workflow.js @@ -22,12 +22,30 @@ var processAll = function (inputDir, options) { } } + let headers = {}; + if (options.headers) { + for(const header of options.headers) { + const [key, value] = header.split(':'); + if (!key || !value) { + console.log(`Error parsing value for header due to invalid format: "${header}"`); + return; + } + headers[key] = value; + } + } + if (fs.existsSync(inputPath)) { fs.readdirSync(inputPath).forEach(async file => { try { if (path.parse(file).ext === '.html') { if (checkDraft(file, options.drafts)) { - const readOutput = read.readAll(path.join(inputDir, file), options.frontMatter); + const readOutput = read.readAll( + path.join(inputDir, file), + options.frontMatter, + options.slugField, + options.prefixSlug, + headers + ); const converterResult = convert(readOutput.html, options.images); if (options.images === true) { for(const v of converterResult.images) {