Blame view

bower_components/marked-element/marked-element.html 5.26 KB
73bcce88   luigser   COMPONENTS
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
  <!--
  @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>
        /* 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
        }
  
      },
  
      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;
      },
  
      get outputElement () {
        var child = Polymer.dom(this).queryDistributedElements('.markdown-html')[0];
  
        if (child)
          return child;
  
        this.toggleClass('hidden', false, this.$.content);
        return this.$.content;
      },
  
      /**
       * 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;
        }
  
        Polymer.dom(this._outputElement).innerHTML = marked(this.markdown, {
          highlight: this._highlight.bind(this),
        });
      },
  
      _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>