-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathangularjs-bbcode.js
57 lines (48 loc) · 1.96 KB
/
angularjs-bbcode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//* AngularJS-BBcode 0.00.10 | Copyright (c) 2015 Nikita "donnikitos" Nitichevski | MIT License *//
"use strict";
angular.module('bbModule', [])
// Available BB code snippets
.value('snippets', {
"b": "<b>$1</b>", // Bolded text
"u": "<u>$1</u>", // Underlined text
"i": "<i>$1</i>", // Italicized text
"s": "<s>$1</s>", // Strikethrough text
"color=([^\\[\\]<>]+?)": "<span style=\"color:$1;\">$2</span>", // Font color
"img": "<img src=\"$1\" />", // Image without title
"img=([^\\[\\]<>]+?)": "<img src=\"$1\" alt=\"$2\" />", // Image with title
"url": "<a href=\"$1\" target=\"_blank\" title=\"$1\">$1</a>", // Simple URL
"url=([^\\[\\]<>]+?)": "<a href=\"$1\" target=\"_blank\" title=\"$2\">$2</a>", // URL with title
"media=https?:\\/\\/.*(youtube\\.com\\/watch\\?v=([^&\\?\\]]+).*)": "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/$2\"></iframe>", //youtube video embedding
"style": function(complete, y) { // Example of function use
return y.toUpperCase();
}
})
// Format BB code
.directive('ksBbcode', ['snippets', function(snippets) {
return {
"restrict": "A",
"link": function($scope, $element, $attrs) {
$scope.$watch(function() {
var contents = $element.html().replace(/^\s+|\s+$/i, '');
for(var i in snippets) {
var regexp = new RegExp('\\[' + i + '\\](.+?)?\\[\/' + i.replace(/=.*/g, '') + '\\]', 'gi');
contents = contents.replace(regexp, snippets[i]);
}
$element.html(contents);
});
}
};
}])
// Format new lines
.directive('ksNl2br', [function() {
return {
"restrict": "A",
"link": function($scope, $element, $attrs) {
$scope.$watch(function() {
var contents = $element.html().replace(/^\s+|\s+$/i, '');
contents = contents.replace(/(?:\r\n|\n|\r)/gi, '<br>');
$element.html(contents);
});
}
};
}]);