turndown icon indicating copy to clipboard operation
turndown copied to clipboard

[WIP] Command Line Interface

Open domchristie opened this issue 6 years ago • 6 comments

Closes #29 Closes #110

I’m not experienced in designing CLIs—any thoughts @asimjalis @academyofzhuang @michaelandrepearce?

domchristie avatar Dec 19 '17 23:12 domchristie

@domchristie if it helps i actually ended up finding https://github.com/fabianmoronzirfas/to-markdown-cli which is based on old version of turndown (when was to-markdown), but with minor mods, i got it running with latest.

#!/usr/bin/env node

/* eslint no-undefined: "off" */

const fs = require('fs');
const path = require('path');

const program = require('commander');
const toMarkdown = require('turndown');
const clipboardy = require('clipboardy');

const pkg = require('./package.json');

let inPath = null;
let outPath = null;
let toStdout = true;
let data = '';

function parseInPath (val) {
  if(fs.existsSync(path.resolve(process.cwd(), val))) {
    inPath = val;
  }else{
    console.log('the specified file path for the input file does not exist');
    // process.exit(1);
  }
}

function parseOutPath(val) {
  outPath = val;
}


program
  .version(pkg.version)
  .option('-i, --input <input>', 'path to the input file', parseInPath)
  .option('-o, --output <output>', 'path to the output file', parseOutPath)
  .option('-c, --clipboard', 'use only the clipboard for input and output');

program.on('--help', ()=>{
  console.log('');
  console.log('    html2md turns html into markdown');
  console.log('    if no input file is given it ueses the clipboard content');
  console.log('    if no output file is given it logs the result to stdout');
  console.log('');
  console.log('    Optional Options:');
  console.log('        -i, --input <input>', 'path to the input file');
  console.log('        -o, --output <output>', 'path to the output file');
  console.log('        -c, --clipboard', 'use the clipboard only. All other options will be dismissed');
  console.log('');
  console.log('    Examples:');
  console.log('        $ html2md -i ./foo.html <= output to stdout');
  console.log('        $ html2md -i ./foo.html -o out.md <= output to out.md');
  console.log('        $ html2md -o out.md <= clipboard to out.md');
  console.log('        $ html2md -c <= clipboard to clipboard');
  console.log('        $ html2md <= clipboard to stdout');

  console.log('');
  console.log('    Acknowledgments:');
  console.log('        Build on these great modules:');
  console.log('        - https://github.com/domchristie/to-markdown');
  console.log('        - https://github.com/sindresorhus/clipboardy');
  console.log('        - https://github.com/tj/commander.js');
  console.log('');

});
program.parse(process.argv);

var TurndownService = require('turndown')

var turndownService = new TurndownService()

if(program.clipboard !== undefined) {
  data = clipboardy.readSync();
  data = turndownService.turndown(data);
  clipboardy.writeSync(data);
  process.exit(0);
}
if(program.input !== undefined) {
  data = fs.readFileSync(inPath, 'utf8');
}else{
  data = clipboardy.readSync();
}

if(program.output !== undefined) {
  fs.writeFile(outPath, turndownService.turndown(data), 'utf8', (error, md) => {
    if(error) {
      throw error;
    }
    console.log(`wrote to ${outPath}`);
  });
}else{
  console.log(turndownService.turndown(data));
}

michaelandrepearce avatar Dec 19 '17 23:12 michaelandrepearce

Would be nice to have this merged.

GerkinDev avatar Jun 11 '18 06:06 GerkinDev

It appears that it should be possible to fun turndown from command line, but I could not. I even tried to copy the above code into a turndown.js file and tried to run it after making it executable. but it appears that alone was not enough.

any tips on how turndown can be run from the command line?

mals14 avatar Jan 08 '19 02:01 mals14

Hi, I made https://github.com/domchristie/turndown/pull/284 which is a remix of the code posted above, and configured it in package.json.

You may want to add all the options this PR contains (I did not), but I would advice to merge it first, as I probably won't have the time required to add support for all options.

Feel free to have a look

gpotter2 avatar May 21 '19 20:05 gpotter2

Hi, I made #284 which is a remix of the code posted above, and configured it in package.json.

You may want to add all the options this PR contains (I did not), but I would advice to merge it first, as I probably won't have the time required to add support for all options.

Feel free to have a look

Thank you @gpotter2. Thank you - #284 looks good, except I do not know how to use it. Meaning whether I should download the current version of Turndown and expect it to work or does something else has to be done - sounded like the latter with your reference to package.json etc.

mals14 avatar May 24 '19 17:05 mals14

Just saw this here today.

  1. I update the cli to accept stdin
  2. If you need help with the cli let me know

ff6347 avatar Dec 18 '19 06:12 ff6347