-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarked-github-element.html
More file actions
133 lines (125 loc) · 3.55 KB
/
marked-github-element.html
File metadata and controls
133 lines (125 loc) · 3.55 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<!--
@license
Copyright (c) 2017 Preview-Code. All rights reserved.
This code may only be used under the BSD style license found in LICENSE.txt
-->
<link rel="import" href="../marked-element/marked-element.html">
<script>
/*exported SubstituteRenderedGitHubMarkdownBehavior*/
/**
* @polymerBehavior
*/
var SubstituteRenderedGitHubMarkdownBehavior = (function () {
var githublink = '<a href="https://www.github.com/';
function toLink(url, text) {
return githublink + url + '" target="_blank">' + text + '</a>';
}
/**
* Behavior to generate a regex substituter to turn GitHub flavored
* markdown into correctly rendered HTML.
*
* Examples include issue references (#1234) and user references (@TimvdLippe)
*/
return {
/**
* Create the substituter for the provided project.
* @param {Object} project The project to generate the GitHub flavored
* markdown for.
* @return {function} The function that transforms HTML text
* by applying regex substitutions.
*/
createMarkdownSubstituter: function (project) {
var owner = project.owner;
var name = project.name;
var replacers = [
// Remove direct link in GitHub comment to our application
{
regex: /\<hr\>\n\<p\>Review this pull request.*\<\/p\>$/m,
callback: function () {
return '';
}
},
// Substitute issue references #1234
{
regex: /&?#(\d+)/g,
callback: function (match, digits) {
// Character was escaped, we should modify these numbers
if (match.charAt(0) === '&') {
return match;
}
return toLink(owner + '/' + name + '/issues/' + digits, '#' + digits);
}
},
// Substitute user name references @TimvdLippe
{
regex: /\@(\w+)/g,
callback: function (match, username) {
return toLink(username, match);
}
}
];
return function (err, text) {
for (var i = 0, l = replacers.length; i < l; i++) {
var replacer = replacers[i];
text = text.replace(replacer.regex, replacer.callback);
}
return text;
};
}
};
})();
</script>
<!--
Element that shows GitHub flavored markdown.
-->
<dom-module id="marked-github-element">
<template>
<style>
:host {
display: block;
@apply --commits;
line-height: 1.4em;
}
img {
max-width: 100%;
}
pre {
background-color: #f7f7f7;
padding: 16px;
overflow: auto;
}
a {
text-decoration: none;
color: var(--anchor-color);
}
a:hover {
text-decoration: underline;
}
.markdown-html, p {
word-wrap: break-word;
@apply --commits;
}
marked-element {
width: 100%;
}
</style>
<marked-element markdown="[[markdown]]" callback="[[createMarkdownSubstituter(project)]]">
<div class="markdown-html"></div>
</marked-element>
</template>
<script>
/*global SubstituteRenderedGitHubMarkdownBehavior*/
Polymer({
is: 'marked-github-element',
behaviors: [SubstituteRenderedGitHubMarkdownBehavior],
properties: {
/**
* The markdown to be transformed into rendered HTML.
* @type {String}
*/
markdown: String,
project: Object
}
});
</script>
</dom-module>