Browse Source

Markdown preview

master
Dylan Baker 5 years ago
parent
commit
c884da06f3

+ 23
- 3
assets/js/index.js View File

1
-document.querySelector('[data-generate-button]').addEventListener('click',
2
-  function (e) {
1
+document
2
+  .querySelector('[data-generate-button]')
3
+  .addEventListener('click', function(e) {
3
     var r = confirm('Are you sure? This will overwrite any existing content.');
4
     var r = confirm('Are you sure? This will overwrite any existing content.');
4
     if (!r) {
5
     if (!r) {
5
       e.preventDefault();
6
       e.preventDefault();
6
     }
7
     }
8
+  });
9
+
10
+var iframe = document.querySelector('iframe');
11
+var contentTextArea = document.querySelector('#content[name=content]');
12
+if (iframe && contentTextArea) {
13
+  var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
14
+  var styleNode = document.createElement('style');
15
+  styleNode.innerText = 'body { margin: 0; }';
16
+  iframeDocument.head.appendChild(styleNode);
17
+  var preview = iframeDocument.querySelector('body');
18
+  if (preview) {
19
+    var timeout;
20
+    preview.innerHTML = marked(contentTextArea.value);
21
+    contentTextArea.addEventListener('input', function(e) {
22
+      clearTimeout(timeout);
23
+      timeout = setTimeout(function() {
24
+        preview.innerHTML = marked(e.target.value);
25
+      }, 500);
26
+    });
7
   }
27
   }
8
-);
28
+}

+ 4
- 0
assets/scss/style.scss View File

41
   min-height: 100vh;
41
   min-height: 100vh;
42
 }
42
 }
43
 
43
 
44
+iframe {
45
+  border: none;
46
+}
47
+
44
 .heading {
48
 .heading {
45
   border-bottom: 1px solid $black;
49
   border-bottom: 1px solid $black;
46
   font-size: $heading-font-size;
50
   font-size: $heading-font-size;

+ 2
- 1
yird/app.py View File

24
 
24
 
25
 
25
 
26
 @app.context_processor
26
 @app.context_processor
27
-def inject_site_name():
27
+def inject_settings():
28
     return {
28
     return {
29
+        "show_markdown_preview": yird_settings.MARKDOWN_PREVIEW,
29
         "site_name": yird_settings.SITE_NAME
30
         "site_name": yird_settings.SITE_NAME
30
     }
31
     }
31
 
32
 

+ 1
- 0
yird/settings.py View File

12
             settings_file = json.loads(f.read())
12
             settings_file = json.loads(f.read())
13
             self.PUBLIC_PATH = settings_file['PUBLIC_PATH']
13
             self.PUBLIC_PATH = settings_file['PUBLIC_PATH']
14
             self.SITE_NAME = settings_file['SITE_NAME']
14
             self.SITE_NAME = settings_file['SITE_NAME']
15
+            self.MARKDOWN_PREVIEW = settings_file['MARKDOWN_PREVIEW']

+ 1
- 0
yird/templates/layout.html.j2 View File

51
       {% block content %}{% endblock %}
51
       {% block content %}{% endblock %}
52
     </main>
52
     </main>
53
   </div>
53
   </div>
54
+  <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
54
   <script src="{{ url_for('static', filename=asset_url_for('app_js.js')) }}"></script>
55
   <script src="{{ url_for('static', filename=asset_url_for('app_js.js')) }}"></script>
55
 </body>
56
 </body>
56
 </html>
57
 </html>

+ 8
- 0
yird/templates/posts/form.html.j2 View File

24
     <div class="form__field form__field--submit">
24
     <div class="form__field form__field--submit">
25
       <input type="submit" value="Submit" class="form__submit button">
25
       <input type="submit" value="Submit" class="form__submit button">
26
     </div>
26
     </div>
27
+
28
+    {% if show_markdown_preview %}
29
+      <h3 class="heading">
30
+        Preview
31
+      </h3>
32
+      <iframe src="about:blank" width="100%">
33
+      </iframe>
34
+    {% endif %}
27
   </form>
35
   </form>
28
 {% endblock %}
36
 {% endblock %}

Loading…
Cancel
Save