Browse Source

Markdown output no longer prints subheads of empty sections

Nathan Schneider 4 years ago
parent
commit
fe30f5dfb4
1 changed files with 20 additions and 4 deletions
  1. 20 4
      _layouts/rule.html

+ 20 - 4
_layouts/rule.html

@@ -117,17 +117,31 @@ layout: default
   // Produces Markdown rendition of Rule
   function textOutput() {
       var filename = 'CommunityRule.txt';
+      // First, add title, whether there is one or not
       var content = '# '+ document.getElementById('communityname').innerHTML + '\n\n';
       content = content.replace(/(<([^>]+)>)/ig,''); // strips stray tags
+      // Now, begin adding other elements
       var elements = document.getElementsByClassName('output');
       for (var i = 1; i < elements.length; i++) {
           var thisBit = elements[i].innerHTML;
           thisBit = thisBit.replace(/(<([^>]+)>)/ig,''); // strips stray tags
           if (thisBit != "") {
               if (elements[i].classList.contains("subhead")) {
-                  content += '## ';
+                  // Before printing subhead, make sure it's not empty
+                  var i2 = i + 1;
+                  while ((i2 < elements.length) &&
+                         (!(elements[i2].classList.contains("subhead")))) {
+                      if (elements[i2].innerHTML != "") {
+                          // in this case, it's not empty, so print and move on
+                          content += '## ';
+                          content += thisBit + '\n\n';
+                          break;
+                      } else { i2++; }
+                  } // won't print anything if a subhead has only empty children
+              } else {
+                  // Non-subhead elements can just go ahead and print
+                  content += thisBit + '\n\n';
               }
-              content += elements[i].innerHTML + '\n\n';
           }
       }
       content += document.getElementById('attributionMD').innerHTML;
@@ -219,6 +233,7 @@ layout: default
   }
   
   // END Publish tools
+
   
   // FINALLY, Page loading
   // First, grab the current URL
@@ -239,9 +254,9 @@ layout: default
           }
       }
   }  
+  
 </script>
 
-
 <article class="post">
 
   <header class="post-header">
@@ -249,7 +264,6 @@ layout: default
         {{ page.title }}
     </h1>
 
-
     <button class="pushButton" id="editToggle" onclick="toggleEditMode()">
       Preview</button>
 
@@ -439,3 +453,5 @@ Created with [CommunityRule](https://communityrule.info)
   </button>
   
 </article>
+
+