From bf36c16ef9c6668d46bb7c7f1d59f30528c35e39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ronnie=20Andr=C3=A9=20Bj=C3=B8rvik=20Sletta?= Date: Sat, 28 Jul 2018 11:34:56 +0200 Subject: [PATCH] Add Markdown links to bigimg description --- static/js/main.js | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/static/js/main.js b/static/js/main.js index fea58db..99200c7 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -140,7 +140,37 @@ var main = { $(".intro-header.big-img").css("background-position", ""); } if (typeof desc !== typeof undefined && desc !== false) { - $(".img-desc").text(desc).show(); + // Check for Markdown link + var mdLinkRe = /\[(.*?)\]\((.+?)\)/; + if (desc.match(mdLinkRe)) { + // Split desc into parts, extracting md links + var splitDesc = desc.split(mdLinkRe); + + // Build new description + var imageDesc = $(".img-desc"); + splitDesc.forEach(function (element, index){ + // Check element type. If links every 2nd element is link text, and every 3rd link url + if (index % 3 === 0) { + // Regular text, just append it + imageDesc.append(element); + } + if (index % 3 === 1) { + // Link text - do nothing at the moment + } + if (index % 3 === 2) { + // Link url - Create anchor tag with text + var link = $("", { + "href": element, + "target": "_blank", + "rel": "noopener noreferrer" + }).text(splitDesc[index - 1]); + imageDesc.append(link); + } + }); + imageDesc.show(); + } else { + $(".img-desc").text(desc).show(); + } } else { $(".img-desc").hide(); }