Skip to content

Commit

Permalink
Prevent parse file other than html and markdown #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfiana Sibuea committed Sep 6, 2014
1 parent 984cd85 commit 07d50ca
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
23 changes: 14 additions & 9 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ var print = require('./print.js');
var dir = require('./dir.js');

module.exports = {
build: build,
getFileList: getFileList
build: build
};

/**
Expand Down Expand Up @@ -50,10 +49,16 @@ function render(file, callback) {

// Get full path dan file content
var filePath = path.resolve(process.cwd(), config.get('source')+'/'+file);
var fileExt = path.extname(filePath).toLowerCase();

// Detect markdown file
var markdownExt = ['.markdown', '.mdown', '.mkdn', '.mkd', '.md'];

// Prevent parse file other than html and markdown
if (!parser.isHtml(filePath) && !parser.isMarkdown(filePath)) {

var destPath = path.resolve(process.cwd(), config.get('destination')+'/'+file);

return fs.copy(filePath, destPath, function(err) {
callback(err);
});
}

fs.readFile(filePath, {encoding: 'utf8'}, function(err, string) {

Expand All @@ -80,7 +85,7 @@ function render(file, callback) {
/**
* Parse markdown file
*/
if (config.get('markdown') && (markdownExt.indexOf(fileExt) !== -1)) {
if (config.get('markdown') && parser.isMarkdown(filePath)) {

// Convert string
string = marked(string, {sanitize: false});
Expand All @@ -106,7 +111,7 @@ function render(file, callback) {
/**
* Convert to html file
*/
if (config.get('markdown') && (markdownExt.indexOf(fileExt) !== -1)) {
if (config.get('markdown') && parser.isMarkdown(filePath)) {
file = path.basename(file, fileExt) + '.html';
}

Expand All @@ -115,7 +120,7 @@ function render(file, callback) {
*/
if (
config.get('permalink') &&
path.extname(file) === '.html' &&
parser.isHtml(filePath) &&
file.indexOf('index.html') === -1
) {
file = file.replace('.html', '') + '/index.html';
Expand Down
24 changes: 23 additions & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ module.exports = {
stripData: stripData,
insertString: insertString,
includeFile: includeFile,
getData: getData
getData: getData,
isHtml: isHtml,
isMarkdown: isMarkdown
};


Expand Down Expand Up @@ -120,4 +122,24 @@ function getData(string) {

data.content = JSON.parse(capture[1]);
return data;
}

/**
* Check if file is HTML or not
*/
function isHtml(filePath) {

var fileExt = path.extname(filePath).toLowerCase();
return fileExt === '.html';
}

/**
* Check if file is markdown file or not
*/
function isMarkdown(filePath) {

var fileExt = path.extname(filePath).toLowerCase();
var markdownExt = ['.markdown', '.mdown', '.mkdn', '.mkd', '.md'];

return markdownExt.indexOf(fileExt) !== -1;
}
30 changes: 30 additions & 0 deletions test/testParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,34 @@ describe('Parser module:', function() {

});
});

describe('isHtml() test', function() {

it('should return false if file is not html', function() {
assert.equal(false, parser.isHtml('file.css'));
});

it('should return true if file is html', function() {
assert.equal(true, parser.isHtml('file.html'));
});

});

describe('isMarkdown() test', function() {

it('should return false if file is not markdown', function() {
assert.equal(false, parser.isMarkdown('file.html'));

});

it('should return true if file is markdown', function() {
assert.equal(true, parser.isMarkdown('file.markdown'));
assert.equal(true, parser.isMarkdown('file.mdown'));
assert.equal(true, parser.isMarkdown('file.mkdn'));
assert.equal(true, parser.isMarkdown('file.mkd'));
assert.equal(true, parser.isMarkdown('file.md'));
});

});

});

0 comments on commit 07d50ca

Please sign in to comment.