diff --git a/.gitignore b/.gitignore index c92c4d1..6102a51 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ coverage/ node_modules/ npm-debug.log .DS_STORE +.vscode \ No newline at end of file diff --git a/README.md b/README.md index 7455978..5efbfa8 100644 --- a/README.md +++ b/README.md @@ -22,14 +22,15 @@ To use the CLI, type `split-md` followed by it's args: * cleanName (string): do you want to remove anything from the pattern? * writePath (path to where smaller files should be created) * limit (optionally limit the number of files created) +* hasCounter (optionally have the outputted files names be numbered) #### Example ``` -$ split-md 'tests/testdata.md' '### v' '###' '' 10 +$ split-md 'tests/testdata.md' '### v' '###' '' 10 true ``` -In the above example we are reading in `tests/testdata.md`. Our delimiter is whenever we see a line start with the pattern `### v`. We want to use this line for our new markdown file's name, however we want to remove the `###` by setting it as the cleanName variable. Lastly, we are setting the `writePath` to our current working directory by giving an empty string as the last variable. Also note that we are setting our limit to only create 10 files before exiting. +In the above example we are reading in `tests/testdata.md`. Our delimiter is whenever we see a line start with the pattern `### v`. We want to use this line for our new markdown file's name, however we want to remove the `###` by setting it as the cleanName variable. Next, we are setting the `writePath` to our current working directory by giving an empty string as this variable. Also note that we are setting our limit to only create 10 files before exiting. Lastly, we have `true` to show that we want the file names to be ordered. ## License: diff --git a/src/index.js b/src/index.js index 8597b5c..b2e94cd 100755 --- a/src/index.js +++ b/src/index.js @@ -2,4 +2,4 @@ var cli = require('cli') var splitter = require('./splitter') -splitter(cli.args[0],cli.args[1],cli.args[2],cli.args[3],cli.args[4]) +splitter(cli.args[0],cli.args[1],cli.args[2],cli.args[3],cli.args[4],cli.args[5]) diff --git a/src/splitter.js b/src/splitter.js index 1ccdb51..cfa90d5 100644 --- a/src/splitter.js +++ b/src/splitter.js @@ -2,7 +2,7 @@ var fs = require('fs'); module.exports = splitter -function splitter(readPath, pattern, cleanName, writePath, limit){ +function splitter(readPath, pattern, cleanName, writePath, limit, hasCounter){ try { var array = fs.readFileSync(readPath).toString().split("\n"); } @@ -11,6 +11,7 @@ function splitter(readPath, pattern, cleanName, writePath, limit){ } var title = "", file = "", first = true; var counter = 1; + for(var i=0; i < array.length; i++) { if (first) { file = "\n"; @@ -25,9 +26,14 @@ function splitter(readPath, pattern, cleanName, writePath, limit){ break; file = "\n"; } - title = writePath + array[i].replace(cleanName, "").trim() + + var fileName = array[i].replace(cleanName, "").trim(); + if (fileName.length <= 0) { + fileName = ""; + } + + title = hasCounter ? writePath + counter + "-" + fileName : title = writePath + fileName; title = title.replace(":", "").replace(" ", "") + ".md" - console.log(title) file += array[i]; first = false; } @@ -42,5 +48,3 @@ function splitter(readPath, pattern, cleanName, writePath, limit){ if (err) throw err; }); } - - diff --git a/tests/splitterWithCounter.test.js b/tests/splitterWithCounter.test.js new file mode 100644 index 0000000..c8a4f32 --- /dev/null +++ b/tests/splitterWithCounter.test.js @@ -0,0 +1,35 @@ +var expect = require("chai").expect; +var splitter = require("../src/splitter.js"); +var shell = require("shelljs"); + +var readPath = "tests/testdata.md", + pattern = "###", + cleanName = "###", + writePath = "tests/tmp/", + numberFiles = true; + +describe("splitterWithCounter", function() { + it("should split markdown file into smaller files", function(done) { + shell.mkdir("-p", "tests/tmp"); + splitter(readPath, pattern, cleanName, writePath, 10, numberFiles); + var filez = shell.ls("tests/tmp/*.md"); + console.log(filez); + expect(filez).to.contain("tests/tmp/1-v0.0.1.md"); + expect(filez).to.contain("tests/tmp/2-v0.0.2.md"); + expect(filez).to.contain("tests/tmp/3-v0.0.3.md"); + expect(filez).to.contain("tests/tmp/4-.md"); + shell.rm("-rf", "tests/tmp/"); + done(); + }); + it("should throw error when given invalid file to read", function(done) { + readPath = "blahblahblah"; + expect( + splitter.bind(splitter, readPath, pattern, cleanName, writePath) + ).to.throw(Error); + done(); + }); + it("should exist", function(done) { + expect(splitter).to.exist; + done(); + }); +}); diff --git a/tests/testdata.md b/tests/testdata.md index 82f3f04..2dbf12c 100644 --- a/tests/testdata.md +++ b/tests/testdata.md @@ -1,6 +1,6 @@ ### v0.0.1 -Hey bro! Whats up... +Hey friend! Whats up... not much. you? ### v0.0.2 @@ -12,3 +12,8 @@ We didn\'t have many changes. I believe I can fly. is the best song by R Kelly. + +### + +Some more text, +in an empty file name.