Skip to content

Commit

Permalink
Merge pull request #80 from amazingrando/master
Browse files Browse the repository at this point in the history
Add the option to order file output
  • Loading branch information
accraze authored Aug 12, 2019
2 parents 495101e + a6a81f2 commit 13e815f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ coverage/
node_modules/
npm-debug.log
.DS_STORE
.vscode
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
14 changes: 9 additions & 5 deletions src/splitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -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 = "<!-- order:" + counter + " -->\n";
Expand All @@ -25,9 +26,14 @@ function splitter(readPath, pattern, cleanName, writePath, limit){
break;
file = "<!-- order:" + counter + " -->\n";
}
title = writePath + array[i].replace(cleanName, "").trim()

var fileName = array[i].replace(cleanName, "").trim();
if (fileName.length <= 0) {
fileName = "<empty-filename>";
}

title = hasCounter ? writePath + counter + "-" + fileName : title = writePath + fileName;
title = title.replace(":", "").replace(" ", "") + ".md"
console.log(title)
file += array[i];
first = false;
}
Expand All @@ -42,5 +48,3 @@ function splitter(readPath, pattern, cleanName, writePath, limit){
if (err) throw err;
});
}


35 changes: 35 additions & 0 deletions tests/splitterWithCounter.test.js
Original file line number Diff line number Diff line change
@@ -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-<empty-filename>.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();
});
});
7 changes: 6 additions & 1 deletion tests/testdata.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### v0.0.1

Hey bro! Whats up...
Hey friend! Whats up...
not much. you?

### v0.0.2
Expand All @@ -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.

0 comments on commit 13e815f

Please sign in to comment.