Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions lib/plugins/tag/include_code.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = '<span>' + title + '</span> <a href="' + url + '">download</a>';
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();
});
});