/**
* Service which exposes various utility methods.
*/
angular.module('quay').factory('UtilService', ['$sanitize', function($sanitize) {
var utilService = {};
utilService.isEmailAddress = function(val) {
var emailRegex = /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
return emailRegex.test(val);
};
utilService.getMarkedDown = function(string) {
return html = Markdown.getSanitizingConverter().makeHtml(string || '');
utilService.getFirstMarkdownLineAsText = function(commentString, placeholderNeeded) {
if (!commentString) {
if (placeholderNeeded) {
return '<p style="visibility:hidden">placeholder</p>';
}
return '';
var lines = commentString.split('\n');
var MARKDOWN_CHARS = {
'#': true,
'-': true,
'>': true,
'`': true
for (var i = 0; i < lines.length; ++i) {
// Skip code lines.
if (lines[i].indexOf(' ') == 0) {
continue;
// Skip empty lines.
if ($.trim(lines[i]).length == 0) {
// Skip control lines.
if (MARKDOWN_CHARS[$.trim(lines[i])[0]]) {
return utilService.getMarkedDown(lines[i]);
utilService.escapeHtmlString = function(text) {
var adjusted = text.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
return adjusted;
utilService.stringToHTML = function(text) {
text = utilService.escapeHtmlString(text);
text = text.replace(/\n/g, '<br>');
return text;
utilService.getRestUrl = function(args) {
var url = '';
for (var i = 0; i < arguments.length; ++i) {
if (i > 0) {
url += '/';
url += encodeURI(arguments[i])
return url;
utilService.textToSafeHtml = function(text) {
return $sanitize(utilService.escapeHtmlString(text));
return utilService;
}]);