Support images in comments, truncate addresses

This commit is contained in:
Sander Ferdinand 2018-08-19 15:48:56 +02:00
parent 0c9c0cd5a5
commit 569fa6ee1d
3 changed files with 95 additions and 4 deletions

View file

@ -178,5 +178,72 @@
</div>
<script>
$(document).ready(function(){
let regexp_url = /^[a-zA-Z0-9.:\/]+$/;
let regexp_address = /(W[o|W][a-zA-Z0-9]{95})/g;
let truncated_addy = function(obj){ return `<span data-addy="${obj}" class="wow_addy">${obj.substring(0, 4)}...${obj.slice(-4)}</span>`; }
function rich_addy(obj) {
let html = obj.html();
var matches = html.match(regexp_address);
if(matches) {
matches.filter(function(value, index,self){ return self.indexOf(value) === index; }).forEach(function (obj) {
html = html.replace(new RegExp(obj, 'g'), truncated_addy(obj));
});
}
obj.html(html);
}
function rich_img(obj) {
let content = obj.html();
//let x = obj.attr('data-id');
//if(x == 52){
// debugger;
//}
let spl = content.split(' ');
let lines = [];
spl.forEach(function (line) {
line = line.trim();
if (line && line.search(regexp_url) != -1 && line.indexOf("..") < 0) {
if (line.startsWith('https://i.imgflip.com/')) {
line = `<img src="${line}"/></a><br>`;
} else if (line.startsWith('https://i.imgur.com/')) {
line = `<img src="${line}"/></a><br>`;
}
}
if (line) {
lines.push(line);
}
});
obj.html(lines.join(" "));
}
$(document).on('click', '.wow_addy', function(event){
let obj = $(this);
if(obj.attr('data-active') === "true"){
//obj.attr('data-active', 'false');
//obj.html(truncated_addy(obj.attr('data-addy')));
} else {
obj.attr('data-active', 'true');
obj.html(obj.attr('data-addy'));
}
});
$('.comments-panel .comment-container .media-body span.body').each(function (i, obj){
obj = $(obj);
// convert images to <img>
rich_img(obj);
// truncate addys
let html = rich_addy(obj);
});
});
</script>
<!-- /.container -->
{% endblock %}