Skip to content

Commit

Permalink
add possibility to append the content behind the anchor
Browse files Browse the repository at this point in the history
  • Loading branch information
broud committed Feb 25, 2014
1 parent 067f641 commit e840045
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Appendit [![NPM version](https://badge.fury.io/js/appendit.png)](http://badge.fury.io/js/appendit) [![Build Status](https://travis-ci.org/stefanbuck/appendit.png?branch=master)](https://travis-ci.org/stefanbuck/appendit) [![Coverage Status](https://coveralls.io/repos/stefanbuck/appendit/badge.png)](https://coveralls.io/r/stefanbuck/appendit) [![Dependency Status](https://david-dm.org/stefanbuck/appendit.png?theme=shields.io)](https://david-dm.org/stefanbuck/appendit)

Appendit allows you easily to add text at a specific line. It will works with any plain text format like ```.txt``` ```.md``` ```.js``` ...

## Installation

Expand Down
28 changes: 17 additions & 11 deletions lib/appendit.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module.exports = function (options) {
}

if (options.check === undefined) options.check = true;
if (options.insertAfter === undefined) options.insertAfter = false;
if (options.useParentSpace === undefined) options.useParentSpace = false;
if (options.matchIndex === undefined || options.matchIndex === 'last') options.matchIndex = -1;
if (options.matchIndex === 'first') options.matchIndex = 0;

Expand All @@ -31,34 +33,34 @@ function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
}

function rewrite(args) {
function rewrite(options) {

// check if content is already in the body text
if (args.check) {
var re = new RegExp(args.content.map(function (line) {
if (options.check) {
var re = new RegExp(options.content.map(function (line) {
return '\s*' + escapeRegExp(line);
})
.join('\n'));

if (re.test(args.source)) {
return args.source;
if (re.test(options.source)) {
return options.source;
}
}

var lines = args.source.split('\n');
var lines = options.source.split('\n');

var findMethod = null;
if (args.matchIndex === -1) {
if (options.matchIndex === -1) {
findMethod = _.findLastIndex;
} else {
findMethod = _.findIndex;
}

var count = 0;
var otherwiseLineIndex = findMethod(lines, function (line, i) {
var result = line.indexOf(args.anchor) !== -1;
if (result && args.matchIndex > 0) {
result = args.matchIndex === count;
var result = line.indexOf(options.anchor) !== -1;
if (result && options.matchIndex > 0) {
result = options.matchIndex === count;
count++;
}
return result;
Expand All @@ -74,7 +76,11 @@ function rewrite(args) {
spaceStr += ' ';
}

lines.splice(otherwiseLineIndex, 0, args.content.map(function (line) {
if(options.insertAfter) {
otherwiseLineIndex++;
}

lines.splice(otherwiseLineIndex, 0, options.content.map(function (line) {
return spaceStr + line;
})
.join('\n'));
Expand Down
11 changes: 11 additions & 0 deletions test/appendit_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ describe('html', function () {
equalHelper(options, 'single_with_content.html', 'single_insert.html');
});

it('plaintext', function () {
var options = {
anchor: '#Changelog',
insertAfter: true,
content: [
'\n## v1.1.0\nSecond release'
]
};
equalHelper(options, 'simple.md', 'simple.md');
});

it('without options', function () {
(function () {
appendit()
Expand Down
7 changes: 7 additions & 0 deletions test/expected/simple.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#Changelog

## v1.1.0
Second release

## v1.0.0
First release
4 changes: 4 additions & 0 deletions test/fixtures/simple.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#Changelog

## v1.0.0
First release

0 comments on commit e840045

Please sign in to comment.