Commit a2069ed1 authored by Isabella Skořepová's avatar Isabella Skořepová

Move image-related code from markdown.js to image.js

+ Change logic detecting if article was modified
Fixes #43
parent 9fa1463a
Pipeline #225 failed with stage
......@@ -191,10 +191,6 @@ module.exports = function(obj) {
}
article.metadata.date.modification = newestCommit.date;
var sameDay = function(a,b) {
return a.getYear()==b.getYear() && a.getMonth() == b.getMonth() && a.getDate() == b.getDate();
}
article.metadata.date.modified = !sameDay(newestCommit.date, oldestCommit.date);
if(article.metadata.author === undefined)
article.metadata.author = {}
......
'use strict';
var youtube = require('./utils/youtube.js');
var path = require('path');
var paramInHrefParser = function(href) {
var sepIndex = href.indexOf(' =');
var ret = {
href: href,
options: {}
}
if(sepIndex < 0) {
return ret;
} else {
ret.href = href.substr(0, sepIndex);
var options = href.substr(sepIndex+2);
options = options // split by ',' but not '\,'
.replace('a,','\u0007')
.split(',')
.map(function(e) {return e.replace('\u0007','a,')});
options.forEach(function(option) {
var index = option.indexOf('=');
if(index < 0) {
ret.options[option] = true;
} else {
var key = option.substr(0,index);
var value = option.substr(index+1);
ret.options[key] = value;
}
});
return ret;
}
}
var renderYoutube = function(href, options) {
var info = youtube.info(href);
let out = '<div class="video-container"><div class="videodiv">';
out += '<iframe src="https://www.youtube.com/embed/'+info.videoid+
'?modestbranding&start='+info.time+'" frameborder="0" allowfullscreen></iframe>'
out += '</div></div>'
return out;
}
module.exports = function(href, title, text, curFilename) {
var parsed = paramInHrefParser(href);
href = parsed.href;
var options = parsed.options;
for(let option in options) {
if( !options.hasOwnProperty(option) ) continue;
if(option.match(/x[0-9]+/) || option.match(/[0-9]+x[0-9]*/)) { // 123x or x123 or 123x123
options.size = option.split('x');
if(options.size !== undefined) {
if(options.size[0].length > 0) {
options.width = options.size[0];
}
if(options.size.length > 1 && options.size[1].length > 0) {
options.height = options.size[1];
}
}
}
}
if(youtube.isVideo(href)) {
return renderYoutube(href, options);
} else {
var rel = path.relative(process.cwd(),path.resolve(curFilename,href));
var out = '<img src="' + href + '" alt="' + text + '"';
if(options.width) {
out += ' width="' + options.width + '"';
}
if(options.height) {
out += ' height="' + options.height + '"';
}
if (title) {
out += ' title="' + title + '"';
}
out += '>';
if(!options.nolightbox) { // if lightbox
var a = '<a href="'+href+'"';
a += ' data-lightbox="group"';
a += ' data-title="' + text + '"';
a += '>';
out = a+out+'</a>';
}
return out;
}
}
......@@ -6,14 +6,44 @@
var marked = require('marked');
var renderer = new marked.Renderer();
var highlightjs = require('highlight.js');
var path = require('path');
var jobs = require('./jobs');
var youtube = require('./utils/youtube.js');
var lang;
var opts = {
highlight: function (code) {
var h;
if(lang) {
if(highlightjs.getLanguage(lang))
h = highlightjs.highlight(lang, code, true);
else
h = highlightjs.highlight(code);
} else {
h = highlightjs.highlightAuto(code);
}
return h.value;
},
renderer: renderer,
gfm: true,
tables: true,
breaks: false,
pedantic: false,
sanitize: false,
smartLists: true,
smartypants: false,
};
var parser = new marked.Parser(opts);
var lexer = new marked.Lexer(opts);
var toURL = function(url) {
return jobs.run('toURL', url);
}
var renderImage = function(href, title, text, curFilename) {
return jobs.run('image', href, title, text, curFilename);
}
renderer.heading = function(text, level, raw) {
return '<h' +
(level+2) +
......@@ -31,107 +61,8 @@ renderer.paragraph = function(text) {
else return '<p>'+text+'</p>\n';
}
var paramInHrefParser = function(href) {
var sepIndex = href.indexOf(' =');
var ret = {
href: href,
options: {}
}
if(sepIndex < 0) {
return ret;
} else {
ret.href = href.substr(0, sepIndex);
var options = href.substr(sepIndex+2);
options = options // split by ',' but not '\,'
.replace('a,','\u0007')
.split(',')
.map(function(e) {return e.replace('\u0007','a,')});
options.forEach(function(option) {
var index = option.indexOf('=');
if(index < 0) {
ret.options[option] = true;
} else {
var key = option.substr(0,index);
var value = option.substr(index+1);
ret.options[key] = value;
}
});
return ret;
}
}
var renderYoutube = function(href, options) {
var info = youtube.info(href);
let out = '<div class="video-container"><div class="videodiv">';
out += '<iframe src="https://www.youtube.com/embed/'+info.videoid+
'?modestbranding&start='+info.time+'" frameborder="0" allowfullscreen></iframe>'
out += '</div></div>'
return out;
}
var renderImage = function(href, title, text, curFilename) {
var parsed = paramInHrefParser(href);
href = parsed.href;
var options = parsed.options;
for(let option in options) {
if( !options.hasOwnProperty(option) ) continue;
if(option.match(/x[0-9]+/) || option.match(/[0-9]+x[0-9]*/)) { // 123x or x123 or 123x123
options.size = option.split('x');
if(options.size !== undefined) {
if(options.size[0].length > 0) {
options.width = options.size[0];
}
if(options.size.length > 1 && options.size[1].length > 0) {
options.height = options.size[1];
}
}
}
}
if(youtube.isVideo(href)) {
return renderYoutube(href, options);
} else {
var rel = path.relative(process.cwd(),path.resolve(curFilename,href));
var out = '<img src="' + href + '" alt="' + text + '"';
if(options.width) {
out += ' width="' + options.width + '"';
}
if(options.height) {
out += ' height="' + options.height + '"';
}
if (title) {
out += ' title="' + title + '"';
}
out += '>';
if(!options.nolightbox) { // if lightbox
var a = '<a href="'+href+'"';
a += ' data-lightbox="group"';
a += ' data-title="' + text + '"';
a += '>';
out = a+out+'</a>';
}
return out;
}
}
renderer._link = renderer.link;
marked.setOptions({
renderer: renderer,
gfm: true,
tables: true,
breaks: false,
pedantic: false,
sanitize: false,
smartLists: true,
smartypants: false,
});
module.exports = function(obj) {
console.log('Build step: Markdown');
var options = obj.config.options;
......@@ -152,24 +83,8 @@ module.exports = function(obj) {
renderer.image = function(href, title, text) {
return renderImage(href, title, text, article.filename);
};
article.content = marked(article.content, {
highlight: function (code) {
var h;
if(highlightjs.getLanguage(article.metadata.lang))
h = highlightjs.highlight(article.metadata.lang, code, true);
else
h = highlightjs.highlightAuto(code);
return h.value;
},
renderer: renderer
});
lang = article.metadata.lang;
article.content = parser.parse(lexer.lex(article.content));
});
return Promise.resolve(obj);
}
module.exports._test = {
renderImage: renderImage,
paramInHrefParser: paramInHrefParser,
renderYoutube: renderYoutube
};
......@@ -18,6 +18,7 @@ module.exports = function(config) {
['resetJobs', './resetJobs'],
['sitemap', './sitemap'],
['parseRedirects','./parseRedirects'],
['image', './image'],
['pipeline','./pipeline']
)
......
......@@ -40,7 +40,7 @@
<span style="color: #626262">{{ metadata.date.creation | date("D. M. YYYY") }}</span>
napsal
<span style="color: #626262">{{ metadata.author.name }}</span>
{% if metadata.date.modified %}
{% if (metadata.date.modification | date("D. M. YYYY")) != (metadata.date.creation | date("D. M. YYYY")) %}
naposledy upraveno <span style="color: #626262">{{ metadata.date.modification | date("D. M. YYYY") }}</span>
{% endif %}
{% else %}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment