Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

Commit

Permalink
build: more advanced import from medium
Browse files Browse the repository at this point in the history
  • Loading branch information
nlm-pro committed Aug 22, 2019
1 parent db5a3b5 commit 68975ba
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "tools/mediumexporter"]
path = tools/mediumexporter
url = [email protected]:xdamman/mediumexporter.git
73 changes: 73 additions & 0 deletions medium-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const fs = require('fs-extra');
const path = require('path');
const mediumexporter = require('./tools/mediumexporter/index.js');

async function importer(link, dir, name) {
return mediumexporter.getPost(link, {
output: path.resolve(dir, name),
hugo: true,
frontmatter: true
});
}

const getOption = name => {
const indexOf = process.argv.indexOf(`--${name}`);
return indexOf === -1 ? '' : process.argv[indexOf + 1];
};

const options = {
name: getOption('name'),
from: getOption('from'),
serie: getOption('serie')
};

const usage = `
use as follow:
medium-import.js --name=<shortname> --from=<medium-url> [--serie=<serie-name>]
name: file and folder name (required)
from: medium url from which to import (required)
serie: will create the new folder in a new or existing "serie-name" folder (false by default)
`;

if (!options.name || !options.from) {
console.log(usage);
process.exit(1);
}

(async function() {
const dir = path.resolve(__dirname, options.serie);

fs.ensureDirSync(dir);

console.log('generating markdown...\n')

await importer(options.from, dir, options.name);

const mdPath = path.resolve(dir, options.name, 'index.md');

if (!fs.existsSync(mdPath)) {
console.log('no markdown generated!');
process.exit(1);
} else {
console.log(`\nmarkdown generated at:\n${mdPath}\n`);
}

const mediumMd = fs.readFileSync(mdPath, 'utf8');
const title = mediumMd.match(/(?<=^---[\s\S]*title:[\s]+).+(?=[\s\S]*---)/)[0] || options.name;
const mediumMeta = mediumMd.match(/(?<=^---[\s])[\s\S]*?(?=---)/)[0];
const devtoMd = mediumMd.replace(
/^---[\s\S]*?---/,
`---
title: ${title}
published: false
description:
tags:
series: ${options.serie || ''}
---`
);

fs.writeFileSync(mdPath, devtoMd, 'utf8');
fs.writeFileSync(path.join(dir, options.name, 'medium.yaml'), mediumMeta, 'utf8');
})();
36 changes: 15 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
"devDependencies": {
"convert-svg-to-png": "^0.5.0",
"diagrams": "^0.10.0",
"mediumexporter": "^0.1.6"
"fs-extra": "^8.1.0"
}
}
1 change: 1 addition & 0 deletions tools/mediumexporter
Submodule mediumexporter added at b8b6cd

0 comments on commit 68975ba

Please sign in to comment.