Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameters to customize Front Matter values #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <prefix>', 'Use a different prefix for the slug path, replaces /@username/')
.option('-s, --slugField <name>', 'Specfiy the name of the slug field.', 'slug')
.action(workflow.processAll);

// Convert from url has been removed.
Expand Down
14 changes: 7 additions & 7 deletions lib/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 };
}
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 19 additions & 1 deletion lib/workflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down