From fdfad5dff7f7319fad5387dcef3217d2f0921061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Sko=C5=99epa?= <jakub@skorepa.info> Date: Thu, 3 Mar 2016 17:36:30 +0100 Subject: [PATCH] Image related tweaks to markdown transformation - When paragraph has image its not p but div.has-image - You can specify image dimensions in markdown - Misc crash fixes --- sitegin/input-hugo.js | 20 ++++++++++++-------- sitegin/transformer-git.js | 6 +++--- sitegin/transformer-markdown.js | 17 +++++++++++++++++ 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/sitegin/input-hugo.js b/sitegin/input-hugo.js index abd9a69..a1725dc 100644 --- a/sitegin/input-hugo.js +++ b/sitegin/input-hugo.js @@ -19,15 +19,17 @@ function walk(currentDirPath, callback, end_cb) { if(err!=null) console.log(err); files.forEach(function(name) { var filePath = path.join(currentDirPath, name); - var stat = fs.statSync(filePath); - if (stat.isFile()) { - callback(filePath, stat); - } else if (stat.isDirectory()) { - if(filePath.indexOf(".git") < 0) { - subcalls++; - walk(filePath, callback, ender); + try { + var stat = fs.statSync(filePath); + if (stat.isFile()) { + callback(filePath, stat); + } else if (stat.isDirectory()) { + if(filePath.indexOf(".git") < 0) { + subcalls++; + walk(filePath, callback, ender); + } } - } + } catch(e) {} }); if(subcalls == 0) ender(); @@ -88,6 +90,8 @@ module.exports = function(_contentDir) { */ exports.forEachPage = function(callback, cb_end) { walk(contentDir+"articles",function(filePath, stat) { + console.log(filePath, path.basename(filePath)); + if(path.basename(filePath).substr(0,1) == ".") return; if(callback !== undefined) callback(filePath.substring(contentDir.length,filePath.length-3)); }, function() { if(cb_end !== undefined) cb_end(); diff --git a/sitegin/transformer-git.js b/sitegin/transformer-git.js index c4488c9..bb0dc37 100644 --- a/sitegin/transformer-git.js +++ b/sitegin/transformer-git.js @@ -84,10 +84,10 @@ module.exports = function() { articleList.forEach(function(article) { var file = path.relative(pathToRepo, article.origFile); var commits = filesHistory[file]; - commits.sort(function(a,b){ - return b.date - a.date; - }); if(commits !== undefined) { + commits.sort(function(a,b){ + return b.date - a.date; + }); var newestCommit = commits[0]; var oldestCommit = commits[commits.length-1]; diff --git a/sitegin/transformer-markdown.js b/sitegin/transformer-markdown.js index 7f549d7..abaf3e5 100644 --- a/sitegin/transformer-markdown.js +++ b/sitegin/transformer-markdown.js @@ -24,6 +24,11 @@ renderer.heading = function(text, level, raw) { + '>\n'; } +renderer.paragraph = function(text) { + if(text.match('\\<img')) return '<div class="has-image">'+text+'</div>\n'; + else return '<p>'+text+'</p>\n'; +} + marked.setOptions({ renderer: renderer, gfm: true, @@ -49,6 +54,9 @@ module.exports = function() { article.origContent = article.content; renderer.image = function(href, title, text) { href = href.split(path.sep).join("/"); + href = href.split(" ="); + var size = href[1]; + href = href[0]; var rel = path.relative(process.cwd(),path.resolve(article.file,href)); rel = rel.split(path.sep).join("/"); var img = images[rel]; @@ -68,6 +76,15 @@ module.exports = function() { } var out = '<img src="' + href + '" alt="' + text + '"'; + if(size !== undefined) { + size = size.split('x'); + if(size[0].length > 0) { + out += ' width="' + size[0] + '"'; + } + if(size.length > 1 && size[1].length > 0) { + out += ' height="' + size[1] + '"'; + } + } if (title) { out += ' title="' + title + '"'; } -- GitLab