diff --git a/lib/plugins/tag/include_code.js b/lib/plugins/tag/include_code.js
index 7e06c44e10..bee71acd26 100644
--- a/lib/plugins/tag/include_code.js
+++ b/lib/plugins/tag/include_code.js
@@ -10,7 +10,8 @@ var extend = hexo.extend,
hexofs = hexo.util.file,
path = require("path");
-var code_prefix = '/downloads/code';
+var code_prefix = '/downloads/code',
+ code_path = path.join(hexo.source_dir, config.code_dir);
var regex = {
captionTitleFile:/(.*)?(\s+|^)(\/*\S+)/i,
@@ -33,36 +34,37 @@ extend.tag.register('include_code', function(args, content) {
title = match[1];
file = match[3];
} else {
- throw "File could not be found";
+ throw new Error("File could not be found");
}
- var local = path.join(config.code_dir, file);
+ var local = path.join(code_path, file);
var url = path.join(code_prefix, file);
- if (!fs.existsSync(config.code_dir) || !fs.existsSync(local)) {
- throw "File '" + local + "'could not be found";
+ if (!fs.existsSync(code_path) || !fs.existsSync(local)) {
+ throw new Error("File '" + local + "' could not be found");
}
- if (fs.lstatSync(config.code_dir).isSymbolicLink()) {
- throw "Code directory '" + config.code_dir + "' cannot be a symlink";
+ if (fs.lstatSync(code_path).isSymbolicLink()) {
+ throw new Error("Code directory '" + config.code_dir + "' cannot be a symlink");
}
var code = fs.readFileSync(local).toString();
+ title = title || path.basename(file);
+ lang = lang || path.extname(file).replace('.', '');
caption = '' + title + ' download';
return highlight(code, {lang: lang, caption:caption, gutter: lineNumConfig, tab: tabConfig});
});
extend.generator.register(function(locals, render, callback) {
- hexofs.dir(config.code_dir, function(files) {
+ hexofs.dir(code_path, function(files) {
for (var i in files) {
var file = files[i];
var url = path.join(code_prefix, file);
- var local = path.join(config.code_dir, file);
+ var local = path.join(code_path, file);
var code = fs.readFileSync(local).toString();
hexo.route.set(url, code);
}
callback();
});
});
-