forked from PolymerElements/marked-element
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarked-element.html
261 lines (224 loc) · 7.36 KB
/
marked-element.html
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="marked-import.html">
<!--
Element wrapper for the [marked](https://github.com/chjj/marked) library.
`<marked-element>` accepts Markdown source, and renders it to a child
element with the class `markdown-html`. This child element can be styled
as you would a normal DOM element. If you do not provide a child element
with the `markdown-html` class, the Markdown source will still be rendered,
but to a shadow DOM child that cannot be styled.
The Markdown source can be specified either via the `markdown` attribute:
<marked-element markdown="`Markdown` is _awesome_!">
<div class="markdown-html"></div>
</marked-element>
Or, you can provide it via a `<script type="text/markdown">` element child:
<marked-element>
<div class="markdown-html"></div>
<script type="text/markdown">
Check out my markdown!
We can even embed elements without fear of the HTML parser mucking up their
textual representation:
```html
<awesome-sauce>
<div>Oops, I'm about to forget to close this div.
</awesome-sauce>
```
</script>
</marked-element>
Note that the `<script type="text/markdown">` approach is _static_. Changes to
the script content will _not_ update the rendered markdown!
### Styling
If you are using a child with the `markdown-html` class, you can style it
as you would a regular DOM element:
.markdown-html p {
color: red;
}
.markdown-html td:first-child {
padding-left: 24px;
}
@element marked-element
@group Molecules
@hero hero.svg
@demo demo/index.html
-->
<dom-module id="marked-element">
<template>
<style>
:host {
display: block;
}
a {
text-decoration: none;
color: #0099cc !important;
}
/* Thanks IE 10. */
.hidden {
display: none !important;
}
</style>
<content select=".markdown-html"></content>
<div id="content" class="hidden"></div>
</template>
</dom-module>
<script>
'use strict';
Polymer({
is: 'marked-element',
properties: {
/**
* The markdown source that should be rendered by this element.
*/
markdown: {
observer: 'render',
type: String,
value: null
},
/**
* Conform to obscure parts of markdown.pl as much as possible. Don't fix any of the original markdown bugs or poor behavior.
*/
pedantic: {
observer: 'render',
type: Boolean,
value: false
},
/**
* Function used to customize a renderer based on the [API specified in the Marked
* library](https://github.com/chjj/marked#overriding-renderer-methods).
* It takes one argument: a marked renderer object, which is mutated by the function.
*/
renderer: {
observer: 'render',
type: Function,
value: null
},
/**
* Sanitize the output. Ignore any HTML that has been input.
*/
sanitize: {
observer: 'render',
type: Boolean,
value: false
},
/**
* Use "smart" typographic punctuation for things like quotes and dashes.
*/
smartypants: {
observer: 'render',
type: Boolean,
value: false
},
/**
* Callback function invoked by Marked after HTML has been rendered.
* It must take two arguments: err and text and must return the resulting text.
*/
callback: {
observer: 'render',
type: Function,
value: null
}
},
ready: function() {
if (!this.markdown) {
// Use the Markdown from the first `<script>` descendant whose MIME type starts with
// "text/markdown". Script elements beyond the first are ignored.
var markdownElement = Polymer.dom(this).querySelector('[type^="text/markdown"]');
if (markdownElement != null) {
this.markdown = this._unindent(markdownElement.textContent);
}
}
},
/**
* Renders `markdown` to HTML when the element is attached.
*
* This serves a dual purpose:
*
* * Prevents unnecessary work (no need to render when not visible).
*
* * `attached` fires top-down, so we can give ancestors a chance to
* register listeners for the `syntax-highlight` event _before_ we render
* any markdown.
*
*/
attached: function() {
this._attached = true;
this._outputElement = this.outputElement;
this.render();
},
detached: function() {
this._attached = false;
},
/**
* Unindents the markdown source that will be rendered.
*/
unindent: function(text) {
return this._unindent(text);
},
get outputElement () {
var child = Polymer.dom(this).queryDistributedElements('.markdown-html')[0];
if (child)
return child;
this.toggleClass('hidden', false, this.$.content);
return this.$.content;
},
/**
* The `marked-render-complete` event is fired once Markdown to HTML
* conversion has finished, and the DOM has been populated via the resulting
* HTML.
*
* @event marked-render-complete
*/
/**
* Renders `markdown` into this element's DOM.
*
* This is automatically called whenever the `markdown` property is changed.
*
* The only case where you should be calling this is if you are providing
* markdown via `<script type="text/markdown">` after this element has been
* constructed (or updating that markdown).
*/
render: function() {
if (!this._attached) return;
if (!this.markdown) {
Polymer.dom(this._outputElement).innerHTML = '';
return;
}
var renderer = new marked.Renderer();
if (this.renderer) {
this.renderer(renderer);
}
var opts = {
renderer: renderer,
highlight: this._highlight.bind(this),
sanitize: this.sanitize,
pedantic: this.pedantic,
smartypants: this.smartypants
};
Polymer.dom(this._outputElement).innerHTML = marked(this.markdown, opts, this.callback);
this.fire('marked-render-complete');
},
_highlight: function(code, lang) {
var event = this.fire('syntax-highlight', {code: code, lang: lang});
return event.detail.code || code;
},
_unindent: function(text) {
if (!text) return text;
var lines = text.replace(/\t/g, ' ').split('\n');
var indent = lines.reduce(function(prev, line) {
if (/^\s*$/.test(line)) return prev; // Completely ignore blank lines.
var lineIndent = line.match(/^(\s*)/)[0].length;
if (prev === null) return lineIndent;
return lineIndent < prev ? lineIndent : prev;
}, null);
return lines.map(function(l) { return l.substr(indent); }).join('\n');
}
});
</script>