]> BookStack Code Mirror - bookstack/commitdiff
Added checkbox sytax parsing to markdown lists
authorDan Brown <redacted>
Sat, 25 Feb 2017 13:16:26 +0000 (13:16 +0000)
committerDan Brown <redacted>
Sat, 25 Feb 2017 13:16:26 +0000 (13:16 +0000)
Closes #319

resources/assets/js/directives.js
resources/assets/sass/_text.scss

index ef8bcd85c6c5ee8e80111a6303e1e7b2a3984d6d..10458e753fac0809a8fe9678affa5f4ade0bbac9 100644 (file)
@@ -214,6 +214,19 @@ export default function (ngApp, events) {
         }
     }]);
 
+    let renderer = new markdown.Renderer();
+    // Custom markdown checkbox list item
+    // Attribution: https://github.com/chjj/marked/issues/107#issuecomment-44542001
+    renderer.listitem = function(text) {
+        if (/^\s*\[[x ]\]\s*/.test(text)) {
+            text = text
+                .replace(/^\s*\[ \]\s*/, '<input type="checkbox"/>')
+                .replace(/^\s*\[x\]\s*/, '<input type="checkbox" checked/>');
+            return `<li class="checkbox-item">${text}</li>`;
+        }
+        return `<li>${text}</li>`;
+    };
+
     /**
      * Markdown input
      * Handles the logic for just the editor input field.
@@ -231,13 +244,13 @@ export default function (ngApp, events) {
                 element = element.find('textarea').first();
                 let content = element.val();
                 scope.mdModel = content;
-                scope.mdChange(markdown(content));
+                scope.mdChange(markdown(content, {renderer: renderer}));
 
                 element.on('change input', (event) => {
                     content = element.val();
                     $timeout(() => {
                         scope.mdModel = content;
-                        scope.mdChange(markdown(content));
+                        scope.mdChange(markdown(content, {renderer: renderer}));
                     });
                 });
 
index 79d3c3a922c334d7ca0fcf345c87238335d05043..a74a81647bd1c0cf8d93c266ccf8872e56815059 100644 (file)
@@ -281,6 +281,14 @@ ol {
   overflow: hidden;
 }
 
+li.checkbox-item {
+  list-style: none;
+  margin-left: - ($-m * 1.3);
+  input[type="checkbox"] {
+    margin-right: $-xs;
+  }
+}
+
 /*
  * Generic text styling classes
  */