Browse Source

Markdown preview

master
Dylan Baker 5 years ago
parent
commit
c884da06f3

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

@@ -1,8 +1,28 @@
1
-document.querySelector('[data-generate-button]').addEventListener('click',
2
-  function (e) {
1
+document
2
+  .querySelector('[data-generate-button]')
3
+  .addEventListener('click', function(e) {
3 4
     var r = confirm('Are you sure? This will overwrite any existing content.');
4 5
     if (!r) {
5 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,6 +41,10 @@ body {
41 41
   min-height: 100vh;
42 42
 }
43 43
 
44
+iframe {
45
+  border: none;
46
+}
47
+
44 48
 .heading {
45 49
   border-bottom: 1px solid $black;
46 50
   font-size: $heading-font-size;

+ 2
- 1
yird/app.py View File

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

+ 1
- 0
yird/settings.py View File

@@ -12,3 +12,4 @@ class Settings:
12 12
             settings_file = json.loads(f.read())
13 13
             self.PUBLIC_PATH = settings_file['PUBLIC_PATH']
14 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,6 +51,7 @@
51 51
       {% block content %}{% endblock %}
52 52
     </main>
53 53
   </div>
54
+  <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
54 55
   <script src="{{ url_for('static', filename=asset_url_for('app_js.js')) }}"></script>
55 56
 </body>
56 57
 </html>

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

@@ -24,5 +24,13 @@
24 24
     <div class="form__field form__field--submit">
25 25
       <input type="submit" value="Submit" class="form__submit button">
26 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 35
   </form>
28 36
 {% endblock %}

Loading…
Cancel
Save