From 62a713ea1c3a318329bc109c6f307de39e84cb41 Mon Sep 17 00:00:00 2001 From: Dave Jensen Date: Sat, 13 Feb 2021 18:39:49 -0800 Subject: [PATCH 1/3] add an option to rename the slug field --- index.js | 1 + lib/reader.js | 9 ++++----- lib/workflow.js | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 0ee4e02..be2d4cb 100755 --- a/index.js +++ b/index.js @@ -12,6 +12,7 @@ program .option('-d, --drafts', 'Convert drafts too.') .option('-f, --frontMatter', 'Add front-matter.') .option('-i, --images', 'Download images in local directory.') + .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..1ff68a7 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) { const contents = fs.readFileSync(filePath); let $ = cheerio.load(contents); @@ -34,7 +33,7 @@ var readAll = function (filePath, frontMatterConfig) { // no tags available in the exported HTML files - const frontMatter = generateFrontMatter(title, subtitle, date, slug); + const frontMatter = generateFrontMatter(title, subtitle, date, slug, slugField); return { html, frontMatter }; } @@ -70,12 +69,12 @@ var readAll = function (filePath, frontMatterConfig) { // }; // } -var generateFrontMatter = function (title, subtitle, date, slug, tags) { +var generateFrontMatter = function (title, subtitle, date, slug, slugField, tags) { const frontMatter = Object.assign({}, frontMatterTemplate); 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..03b61e4 100644 --- a/lib/workflow.js +++ b/lib/workflow.js @@ -27,7 +27,7 @@ var processAll = function (inputDir, options) { 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); const converterResult = convert(readOutput.html, options.images); if (options.images === true) { for(const v of converterResult.images) { From 89ddbf0cc93c6e7ca03910752ec9a4ab629ab7df Mon Sep 17 00:00:00 2001 From: Dave Jensen Date: Sat, 13 Feb 2021 19:05:20 -0800 Subject: [PATCH 2/3] add option to change the prefix in the slug --- index.js | 1 + lib/reader.js | 5 +++-- lib/workflow.js | 7 ++++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index be2d4cb..a31b144 100755 --- a/index.js +++ b/index.js @@ -12,6 +12,7 @@ program .option('-d, --drafts', 'Convert drafts too.') .option('-f, --frontMatter', 'Add front-matter.') .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); diff --git a/lib/reader.js b/lib/reader.js index 1ff68a7..b921fc0 100644 --- a/lib/reader.js +++ b/lib/reader.js @@ -12,7 +12,7 @@ const frontMatterTemplate = { keywords: [], }; -var readAll = function (filePath, frontMatterConfig, slugField) { +var readAll = function (filePath, frontMatterConfig, slugField, prefixSlug) { const contents = fs.readFileSync(filePath); let $ = cheerio.load(contents); @@ -29,7 +29,8 @@ var readAll = function (filePath, frontMatterConfig, slugField) { 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 diff --git a/lib/workflow.js b/lib/workflow.js index 03b61e4..d2db682 100644 --- a/lib/workflow.js +++ b/lib/workflow.js @@ -27,7 +27,12 @@ var processAll = function (inputDir, options) { try { if (path.parse(file).ext === '.html') { if (checkDraft(file, options.drafts)) { - const readOutput = read.readAll(path.join(inputDir, file), options.frontMatter, options.slugField); + const readOutput = read.readAll( + path.join(inputDir, file), + options.frontMatter, + options.slugField, + options.prefixSlug + ); const converterResult = convert(readOutput.html, options.images); if (options.images === true) { for(const v of converterResult.images) { From 69271cd1419a3aef7099225bf309f9789207c786 Mon Sep 17 00:00:00 2001 From: Dave Jensen Date: Sun, 14 Feb 2021 12:41:48 -0800 Subject: [PATCH 3/3] add variadic param for custom front matter headers --- index.js | 1 + lib/reader.js | 8 ++++---- lib/workflow.js | 15 ++++++++++++++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index a31b144..2567541 100755 --- a/index.js +++ b/index.js @@ -11,6 +11,7 @@ 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') diff --git a/lib/reader.js b/lib/reader.js index b921fc0..c31de2d 100644 --- a/lib/reader.js +++ b/lib/reader.js @@ -12,7 +12,7 @@ const frontMatterTemplate = { keywords: [], }; -var readAll = function (filePath, frontMatterConfig, slugField, prefixSlug) { +var readAll = function (filePath, frontMatterConfig, slugField, prefixSlug, headers) { const contents = fs.readFileSync(filePath); let $ = cheerio.load(contents); @@ -34,7 +34,7 @@ var readAll = function (filePath, frontMatterConfig, slugField, prefixSlug) { // no tags available in the exported HTML files - const frontMatter = generateFrontMatter(title, subtitle, date, slug, slugField); + const frontMatter = generateFrontMatter(title, subtitle, date, slug, slugField, headers); return { html, frontMatter }; } @@ -70,8 +70,8 @@ var readAll = function (filePath, frontMatterConfig, slugField, prefixSlug) { // }; // } -var generateFrontMatter = function (title, subtitle, date, slug, slugField, 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() : ''; diff --git a/lib/workflow.js b/lib/workflow.js index d2db682..15a9cb3 100644 --- a/lib/workflow.js +++ b/lib/workflow.js @@ -22,6 +22,18 @@ 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 { @@ -31,7 +43,8 @@ var processAll = function (inputDir, options) { path.join(inputDir, file), options.frontMatter, options.slugField, - options.prefixSlug + options.prefixSlug, + headers ); const converterResult = convert(readOutput.html, options.images); if (options.images === true) {