Action

Generate Markdown of Drafts

Posted by jochi, Last update over 1 year ago

Generate Markdown of Drafts – V 0.1

Create a new draft with metadata list of all the drafts in the selected workspace.

This action (created with the help of ChatGPT) generates a markdown list of all drafts in the selected workspace. It offers:
1. Display Options: “Card” or Table View
2. Sort Options: Modified (Descending or Ascending), Created (Descending or Ascending), or Alphabetically (A–Z)
3. Group Option: Group Flagged drafts at the top
4. Configurable Default Options: You can set your own default options for the category, output format, sort by, and group.

This was created entirely with ChatGPT

Steps

  • script

    // CONFIGURATION:
    // Set your default options here. The following options are available:
    // category: ["Inbox", "Flagged", "Archived", "All"]
    // format: ["Card", "Table"]
    // sort: ["Modified (Descending)", "Modified (Ascending)", "Created (Descending)", "Created (Ascending)", "Alphabetical (A–Z)"]
    // group: true or false
    let defaultCategory = ["Inbox"];
    let defaultFormat = ["Card"];
    let defaultSort = ["Modified (Descending)"];
    let defaultGroup = true;
    let defaultTitleLength = 100;
    
    // Define the function to handle prompts
    const createPrompt = () => {
        let prompt = Prompt.create();
        prompt.title = "Generate List of Drafts in Workspace";
        prompt.message = "Select category, the output format, and sorting options";
    
        prompt.addSelect("category", "Drafts Category:", ["Inbox", "Flagged", "Archived", "All"], defaultCategory, false);
        prompt.addSelect("format", "Output Format:", ["Card", "Table"], defaultFormat, false);
        prompt.addSelect("sort", "Sort By:", ["Modified (Descending)", "Modified (Ascending)", "Created (Descending)", "Created (Ascending)", "Alphabetical (A–Z)"], defaultSort, false);
        prompt.addSwitch("group", "Group Flagged Drafts at Top", defaultGroup);
        prompt.addButton("Create");
        return prompt;
    };
    
    // Define the function to sort drafts
    const sortDrafts = (sort, drafts) => {
        return drafts.sort((a, b) => {
            let comparison;
            switch (sort) {
                case "Modified (Descending)":
                    comparison = b.modifiedAt - a.modifiedAt;
                    break;
                case "Modified (Ascending)":
                    comparison = a.modifiedAt - b.modifiedAt;
                    break;
                case "Created (Descending)":
                    comparison = b.createdAt - a.createdAt;
                    break;
                case "Created (Ascending)":
                    comparison = a.createdAt - b.createdAt;
                    break;
                case "Alphabetical (A–Z)":
                    comparison = a.displayTitle.localeCompare(b.displayTitle);
                    break;
                default:
                    comparison = 0;
            }
            return comparison;
        });
    };
    
    // Define the function to group flagged drafts at top
    const groupFlagged = (group, drafts) => {
        if (group === "Yes") {
            return drafts.sort((a, b) => {
                return b.isFlagged - a.isFlagged;
            });
        }
        return drafts;
    };
    
    // Define the function to create content
    const createContent = (workspace, format, drafts, category, sort) => {
        let date = new Date();
        let dateString = date.toLocaleDateString() + " " + date.toLocaleTimeString();
        let content = "# " + workspace.name + " Drafts\n";
        content += "- **Generated:** " + dateString + "\n";
        content += "- **Category:** " + category + "\n";
        content += "- **Sort Order:** " + sort + "\n";
        content += "- **Drafts:** " + drafts.length.toLocaleString() + "\n\n";
    
        let tableHeader = "";
        let tableSeparator = "";
        if (format === "Table") {
            let sortArrow = sort.includes("Descending") ? " ↓" : " ↑";
            tableHeader = "|  | Title" + (sort.includes("Alphabetical") ? sortArrow : "") + " | Tags | Created" + (sort.includes("Created") ? sortArrow : "") + " | Modified" + (sort.includes("Modified") ? sortArrow : "");
            if (category === "All") {
                tableHeader += " | Status";
            }
            tableHeader += " |\n";
            tableSeparator = "|---|---|---|---|---";
            if (category === "All") {
                tableSeparator += "|---";
            }
            tableSeparator += "|\n";
            content += tableHeader + tableSeparator;
        }
    
    drafts.forEach((draft) => {
        const displayTitle = draft.displayTitle.slice(0, defaultTitleLength); // Truncate display title
        const flag = draft.isFlagged ? "🚩 " : ""; // Add flag emoji if draft is flagged
        const creationDate = draft.processTemplate("[[created]]");
        const modificationDate = draft.processTemplate("[[modified]]");
        const status = draft.isArchived ? "Archived" : "Inbox"; // Check if the draft is archived or in inbox
        const tags = draft.tags.length > 0 ? draft.tags.join(", ") : "*untagged*";
        if (format === "Table") {
            // Add draft in Table format
            content += `| ${flag}| ${displayTitle} | ${tags} | ${creationDate} | ${modificationDate} | ${status} |\n`;
        } else {
            // Add draft in Card format
            content += `### ${flag}${displayTitle}\n> ${tags}\n- Created: ${creationDate}\n- Modified: ${modificationDate}\n- Status: ${status}\n\n`;
        }
    
        });
    
        return content;
    };
    
    // Define the function to create new draft
    const createNewDraft = (content) => {
        let newDraft = Draft.create();
        newDraft.content = content;
        newDraft.addTag("to-print");
        newDraft.update();
        editor.load(newDraft);
    };
    
    // Define the main function to create the metadata draft
    const createMetadataDraft = () => {
        let prompt = createPrompt();
        prompt.show();
        if (prompt.buttonPressed !== "Create") {
            return;
        }
    
        let workspace = app.currentWorkspace;
        let category = prompt.fieldValues["category"][0];
        let drafts = workspace.query(category.toLowerCase());
        let sort = prompt.fieldValues["sort"][0];
        let group = prompt.fieldValues["group"] ? "Yes" : "No";
        let format = prompt.fieldValues["format"][0];
    
        drafts = sortDrafts(sort, drafts);
        drafts = groupFlagged(group, drafts);
        let content = createContent(workspace, format, drafts, category, sort);
        createNewDraft(content);
    };
    
    // Call the main function to create the metadata draft
    createMetadataDraft();
    

Options

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