Action

Draft Library Summary

Posted by agiletortoise, Last update 5 months ago

Query all drafts and create a summary of your draft library.

Display total count, flagged count, and count of all assigned tags in your drafts library, displaying the results as an HTML table.

Steps

  • script

    // build audit data about drafts
    
    // create tally objects
    let tagCounts = { };
    let inboxCounts = { };
    let archiveCounts = { };
    let ttl = 0;
    let flagged = 0;
    
    function add(d) {
    	for (let tag of d.tags) {
    		if (tagCounts.hasOwnProperty(tag)) {
    			tagCounts[tag] = tagCounts[tag] + 1;
    		}
    		else {
    			tagCounts[tag] = 1;
    		}
    		if (!d.isArchived) {
    			if (inboxCounts.hasOwnProperty(tag)) {
    				inboxCounts[tag] = inboxCounts[tag] + 1;
    			}
    			else {
    				inboxCounts[tag] = 1;
    			}
    		}
    		else { // archive
    			if (archiveCounts.hasOwnProperty(tag)) {
    				archiveCounts[tag] = archiveCounts[tag] + 1;
    			}
    			else {
    				archiveCounts[tag] = 1;
    			}
    		}
    	}
    	if (d.isFlagged) {
    		flagged += 1;
    	}
    	ttl += 1;
    }
    
    // loop over all tagged drafts and count tags
    const all = Draft.query("", "all", []);
    for (let d of all) {
    	add(d);
    }
    
    // build html to display
    let html = [];
    html.push("<h2>Draft Library Summary</h2>");
    html.push(`<p>Total Drafts: <strong>${ttl}</strong>, Flagged: <strong>${flagged}</strong></p>`);
    
    if (Object.keys(tagCounts).length > 0) {
    	html.push("<div style=\"margin:auto;text-align:center;\"><table style=\"margin:auto;\">");
    	html.push("<tr><th>Tag</th><th>Inbox</th><th>Archive</th><th>All</th></tr>");
    	
    	for (let k of Object.keys(tagCounts).sort()) {
    		html.push(`<tr><td>${k}</td>`);
    		if (inboxCounts.hasOwnProperty(k)) {
    			html.push(`<td style='text-align:right;'>${inboxCounts[k]}</td>`);
    		}
    		else {
    			html.push("<td style='text-align:right;'>0</td>");
    		}
    		if (archiveCounts.hasOwnProperty(k)) {
    			html.push(`<td style='text-align:right;'>${archiveCounts[k]}</td>`);
    		}
    		else {
    			html.push("<td style='text-align:right;'>0</td>");
    		}
    		html.push(`<td style='text-align:right;'>${tagCounts[k]}</td></tr>`);
    	}
    	html.push("</table></div>");
    }
    else {
    	html.push("<p>No tags assigned</p>");
    }
    
    draft.setTemplateTag("html", html.join("\n"));
  • htmlpreview

    <!DOCTYPE html>
    <html dir="auto">
    
    <head>
      <title>[[title]]</title>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <style>
        @charset "utf-8";
    
        :root {
          --main-bg-color: white;
          --main-color: black;
          --alternate-bg-color: #efefef;
          --alternate-color: #222222;
          --main-border-color: #BBBBBB;
            --link-color: #627EC9;
        }
    
        @media (prefers-color-scheme: dark) {
          :root {
            --main-bg-color: #222222;
            --main-color: #eeeeee;
            --alternate-bg-color: #444444;
            --alternate-color: #cccccc;
            --main-border-color: #AAAAAA;
              --link-color: #627EC9;
          }
        }
    
        html {
          font-size: 100%;
          font-family: -apple-system, BlinkMacSystemFont, "helvetica neue", helvetica, roboto, noto, "segoe ui", arial, sans-serif;
          line-height: 1.4;
        }
    
        body {
          margin: 0;
          padding: 1em;
          background-color: var(--main-bg-color);
          color: var(--main-color);
          text-align:center;
        }
    
        h2 { padding: 0em; margin: 0em; }
    
        @media (max-device-width: 480px) {}
    
        @media (min-device-width: 481px) {
          body {
            margin: auto;
            max-width: 600px;
          }
        }
    
        blockquote {
          font-style: italic;
          margin: 1.5em 2em;
          padding: 1em;
          background-color: var(--alternate-bg-color);
          color: var(--alternate-color);
        }
    
        a {
          color: var(--link-color);
        }
        pre {
          display: block;
          overflow: scroll;
          width: 100%;
          background-color: var(--alternate-bg-color);
          padding: .5em 1em;
          margin: 1em 0;
        }
    
        code {
          background-color: var(--alternate-bg-color);
          color: var(--alternate-color);
          font-family: Menlo, Courier, sans-serif;
          padding: 2px 3px;
        }
    
        table {
          margin: 1em 1em;
          border: 1px solid var(--main-border-color);
          border-collapse: collapse;
          font-size: .85em;
        }
    
        th {
          padding: .25em .5em;
          background: var(--alternate-bg-color);
          border: 1px solid var(--main-border-color);
        }
    
        td {
          padding: .25em .5em;
          border: 1px solid var(--main-border-color);
        }
    
        img {
          max-width: 90%;
        }
      </style>
    </head>
    
    <body>
      %%[[html]]%%
    </body>
    
    </html>
    

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.