Make URLs in MOTD messages clickable

A long-wanted feature! Since the MOTD can only be controlled by an
admin, the risk of abuse is very limited.

Change-Id: Iff6263059d76c9b75ae26e029e3063992ab7cb76
This commit is contained in:
Thierry Carrez 2020-11-13 17:30:56 +01:00
parent a0d3fbab41
commit 415ae5a05e
4 changed files with 11 additions and 3 deletions

View File

@ -24,7 +24,7 @@
</nav>
<script id="MOTDTemplate" type="text/x-handlebars-template">
{{#each motd}}
<div class="alert alert-{{this.level}}" role="alert">{{this.message}}</div>
<div class="alert alert-{{this.level}}" role="alert">{{ linkify this.message }}</div>
{{/each}}
</script>
<div id="MOTD"></div>

View File

@ -24,7 +24,7 @@
</nav>
<script id="MOTDTemplate" type="text/x-handlebars-template">
{{#each motd}}
<div class="alert alert-{{this.level}}" role="alert">{{this.message}}</div>
<div class="alert alert-{{this.level}}" role="alert">{{ linkify this.message }}</div>
{{/each}}
</script>
<div id="MOTD"></div>

View File

@ -4,6 +4,14 @@ var dsource = document.getElementById("MOTDTemplate").innerHTML;
// Handlebars compiles the above source into a template
var dtemplate = Handlebars.compile(dsource);
Handlebars.registerHelper('linkify', function(str) {
var pattern1 = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
var str1 = str.replace(pattern1, '<a target="_blank" href="$1">$1</a>');
var pattern2 =/(^|[^\/])(www\.[\S]+(\b|$))/gim;
var str2 = str1.replace(pattern2, '$1<a target="_blank" href="http://$2">$2</a>');
return new Handlebars.SafeString(str2);
});
$.getJSON("ptg.json", function(json) {
document.getElementById("MOTD").innerHTML = dtemplate(json);
});

View File

@ -24,7 +24,7 @@
</nav>
<script id="MOTDTemplate" type="text/x-handlebars-template">
{{#each motd}}
<div class="alert alert-{{this.level}}" role="alert">{{this.message}}</div>
<div class="alert alert-{{this.level}}" role="alert">{{ linkify this.message }}</div>
{{/each}}
</script>
<div id="MOTD"></div>