Skip to content

Commit

Permalink
gnab#105: Trim equal amount of leading whitespace from all lines to a…
Browse files Browse the repository at this point in the history
…llow for indenting entire presentation.
  • Loading branch information
DanTup committed Apr 13, 2014
1 parent a963f19 commit 7aa6d73
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/remark/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function Parser () { }
*/
Parser.prototype.parse = function (src) {
var lexer = new Lexer(),
tokens = lexer.lex(src),
tokens = lexer.lex(cleanInput(src)),
slides = [],

// The last item on the stack contains the current slide or
Expand Down Expand Up @@ -142,3 +142,17 @@ function extractProperties (source, properties) {

return source;
}

function cleanInput(source) {
// If all lines are indented, we should trim them all to the same point so that code doesn't
// need to start at column 0 in the source (see GitHub Issue #105)

// Calculate the minimum leading whitespace
var leadingWhitespacePattern = /^(\s*)/gm;
var whitespace = source.match(leadingWhitespacePattern).map(function (s) { return s.length; });
var minWhitespace = Math.min.apply(Math, whitespace);

// Trim off the exact amount of whitespace
var trimWhitespacePattern = new RegExp('^\\s{' + minWhitespace + '}', 'gm');
return source.replace(trimWhitespacePattern, '');
}
4 changes: 2 additions & 2 deletions test/remark/parser_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ describe('Parser', function () {
var slides = parser.parse(' 1\n ---\n 2\n ---\n 3');

slides[0].content.should.eql(['1']);
slides[1].content.should.eql([' 2']);
slides[1].content.should.eql([' 2\n']); // Note: lexer includes trailing newines in code blocks
slides[2].content.should.eql(['3']);
});

it('should preserve leading whitespace that goes beyond the minimum whitespace on the first line', function () {
var slides = parser.parse(' 1\n ---\n 2\n ---\n 3');

slides[0].content.should.eql([' 1']);
slides[0].content.should.eql([' 1\n']); // Note: lexer includes trailing newines in code blocks
slides[1].content.should.eql(['2']);
slides[2].content.should.eql(['3']);
});
Expand Down

0 comments on commit 7aa6d73

Please sign in to comment.