Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
Move config options to overridable array
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbitron committed Jun 5, 2014
1 parent 2859e3e commit c2aafd1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 44 deletions.
70 changes: 40 additions & 30 deletions raneto.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,25 @@ var path = require('path'),

var raneto = {

// Default content directory where Markdown files are stored
contentDir: './content/',

// Toggle debug logging
debug: false,
// Config array that can be overridden
config: {
// The base URL of your site (allows you to use %base_url% in Markdown files)
base_url: '',
// The base URL of your images folder (allows you to use %image_url% in Markdown files)
image_url: '/images',
// Excerpt length (used in search)
excerpt_length: 400,
// The meta value by which to sort pages (value should be an integer)
// If this option is blank pages will be sorted alphabetically
page_sort_meta: 'sort',
// Should categories be sorted numerically (true) or alphabetically (false)
// If true category folders need to contain a "sort" file with an integer value
category_sort: true,
// Specify the path of your content folder where all your '.md' files are located
content_dir: './content/',
// Toggle debug logging
debug: false
},

// Regex for page meta
_metaRegex: /^\/\*([\s\S]*?)\*\//i,
Expand Down Expand Up @@ -61,19 +75,17 @@ var raneto = {
},

// Replace content variables in Markdown content
processVars: function(markdownContent, config) {
config = config || {};
if(typeof config.base_url !== 'undefined') markdownContent = markdownContent.replace(/\%base_url\%/g, config.base_url);
if (typeof config.image_url !== 'undefined') markdownContent = markdownContent.replace(/\%image_url\%/g, config.image_url);
processVars: function(markdownContent) {
if(typeof raneto.config.base_url !== 'undefined') markdownContent = markdownContent.replace(/\%base_url\%/g, raneto.config.base_url);
if (typeof raneto.config.image_url !== 'undefined') markdownContent = markdownContent.replace(/\%image_url\%/g, raneto.config.image_url);
return markdownContent;
},

// Get a page
getPage: function(filePath, config) {
config = config || {};
getPage: function(filePath) {
try {
var file = fs.readFileSync(filePath),
slug = filePath.replace(raneto.contentDir, '').trim();
slug = filePath.replace(raneto.config.content_dir, '').trim();

if(slug.indexOf('index.md') > -1){
slug = slug.replace('index.md', '');
Expand All @@ -82,29 +94,28 @@ var raneto = {

var meta = raneto.processMeta(file.toString('utf-8')),
content = raneto.stripMeta(file.toString('utf-8'));
content = raneto.processVars(content, config);
content = raneto.processVars(content);
var html = marked(content);

return {
'slug': slug,
'title': meta.title ? meta.title : raneto.slugToTitle(slug),
'body': html,
'excerpt': _s.prune(_s.stripTags(_s.unescapeHTML(html)), config.excerpt_length)
'excerpt': _s.prune(_s.stripTags(_s.unescapeHTML(html)), (raneto.config.excerpt_length || 400))
};
}
catch(e){
if(raneto.debug) console.log(e);
if(raneto.config.debug) console.log(e);
return null;
}
},

// Get a structured array of the contents of contentDir
getPages: function(activePageSlug, config) {
getPages: function(activePageSlug) {
activePageSlug = activePageSlug || '';
config = config || {};
var page_sort_meta = config.page_sort_meta || '',
category_sort = config.category_sort || false,
files = glob.sync(raneto.contentDir +'**/*'),
var page_sort_meta = raneto.config.page_sort_meta || '',
category_sort = raneto.config.category_sort || false,
files = glob.sync(raneto.config.content_dir +'**/*'),
filesProcessed = [];

filesProcessed.push({
Expand All @@ -117,18 +128,18 @@ var raneto = {
});

files.forEach(function(filePath){
var shortPath = filePath.replace(raneto.contentDir, '').trim(),
var shortPath = filePath.replace(raneto.config.content_dir, '').trim(),
stat = fs.lstatSync(filePath);

if(stat.isDirectory()){
var sort = 0;
if(category_sort){
try {
var sortFile = fs.readFileSync(raneto.contentDir + shortPath +'/sort');
var sortFile = fs.readFileSync(raneto.config.content_dir + shortPath +'/sort');
sort = parseInt(sortFile.toString('utf-8'), 10);
}
catch(e){
if(raneto.debug) console.log(e);
if(raneto.config.debug) console.log(e);
}
}

Expand Down Expand Up @@ -166,7 +177,7 @@ var raneto = {
});
}
catch(e){
if(raneto.debug) console.log(e);
if(raneto.config.debug) console.log(e);
}
}
});
Expand All @@ -180,17 +191,16 @@ var raneto = {
},

// Index and search contents
doSearch: function(query, config) {
config = config || {};
var files = glob.sync(raneto.contentDir +'**/*.md');
doSearch: function(query) {
var files = glob.sync(raneto.config.content_dir +'**/*.md');
var idx = lunr(function(){
this.field('title', { boost: 10 });
this.field('body');
});

files.forEach(function(filePath){
try {
var shortPath = filePath.replace(raneto.contentDir, '').trim(),
var shortPath = filePath.replace(raneto.config.content_dir, '').trim(),
file = fs.readFileSync(filePath);

var meta = raneto.processMeta(file.toString('utf-8'));
Expand All @@ -201,14 +211,14 @@ var raneto = {
});
}
catch(e){
if(raneto.debug) console.log(e);
if(raneto.config.debug) console.log(e);
}
});

var results = idx.search(query),
searchResults = [];
results.forEach(function(result){
var page = raneto.getPage(raneto.contentDir + result.ref, config);
var page = raneto.getPage(raneto.config.content_dir + result.ref);
page.excerpt = page.excerpt.replace(new RegExp('('+ query +')', 'gim'), '<span class="search-query">$1</span>');
searchResults.push(page);
});
Expand Down
25 changes: 11 additions & 14 deletions test/raneto.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,42 +87,39 @@ describe('#stripMeta()', function() {

describe('#processVars()', function() {
it('replaces config vars in Markdown content', function() {
raneto.processVars(
'This is some Markdown with a %base_url%.',
{
base_url: '/base/url'
}
).should.equal('This is some Markdown with a /base/url.');
raneto.config.base_url = '/base/url';
raneto.processVars('This is some Markdown with a %base_url%.')
.should.equal('This is some Markdown with a /base/url.');
});
});

describe('#getPage()', function() {
it('returns an array of values for a given page', function() {
raneto.contentDir = __dirname +'/content/';
var result = raneto.getPage(raneto.contentDir +'example-page.md');
raneto.config.content_dir = __dirname +'/content/';
var result = raneto.getPage(raneto.config.content_dir +'example-page.md');
expect(result).to.have.property('slug', 'example-page');
expect(result).to.have.property('title', 'Example Page');
expect(result).to.have.property('body');
expect(result).to.have.property('excerpt');
});
it('returns null if no page found', function() {
raneto.contentDir = __dirname +'/content/';
var result = raneto.getPage(raneto.contentDir +'nonexistent-page.md');
raneto.config.content_dir = __dirname +'/content/';
var result = raneto.getPage(raneto.config.content_dir +'nonexistent-page.md');
expect(result).to.be.null;
});
});

describe('#getPages()', function() {
it('returns an array of categories and pages', function() {
raneto.contentDir = __dirname +'/content/';
raneto.config.content_dir = __dirname +'/content/';
var result = raneto.getPages();
expect(result[0]).to.have.property('is_index', true);
expect(result[0].files[0]).to.have.property('title', 'Example Page');
expect(result[1]).to.have.property('slug', 'sub');
expect(result[1].files[0]).to.have.property('title', 'Example Sub Page');
});
it('marks activePageSlug as active', function() {
raneto.contentDir = __dirname +'/content/';
raneto.config.content_dir = __dirname +'/content/';
var result = raneto.getPages('/example-page');
expect(result[0].files[0]).to.have.property('active', true);
expect(result[1].files[0]).to.have.property('active', false);
Expand All @@ -131,12 +128,12 @@ describe('#getPages()', function() {

describe('#doSearch()', function() {
it('returns an array of search results', function() {
raneto.contentDir = __dirname +'/content/';
raneto.config.content_dir = __dirname +'/content/';
var result = raneto.doSearch('example');
expect(result).to.have.length(2);
});
it('returns an empty array if nothing found', function() {
raneto.contentDir = __dirname +'/content/';
raneto.config.content_dir = __dirname +'/content/';
var result = raneto.doSearch('asdasdasd');
expect(result).to.be.empty;
});
Expand Down

0 comments on commit c2aafd1

Please sign in to comment.