Action

Denote Markdown YAML

Posted by SanctumMooch, Last update about 1 month ago

Creates a note with the title, tags and frontmatter (in markdown yaml ) of Denote’s file naming scheme. Denote is an excellent Emacs package created by Protesilaos Stavrou.

More details of the Denote package can be found here:

https://protesilaos.com/emacs/denote#h:4a6d92dd-19eb-4fcc-a7b5-05ce04da3a92

Steps

  • script

    // Get the current draft's title
    let title = draft.title;
    
    // Get the tags of the current draft
    let tags = draft.tags;
    
    // Surround each tag with quotation marks and join them with commas
    let s = tags.map(tag => `"${tag}"`).join(", ");
    
    draft.setTemplateTag("mytag", s);
    
    // Convert tags array to a string, joined by commas or any separator you prefer
    let tagString = tags.join("_");
    
    // Create a new title by appending the tags to the original title
    let newTitle = `--${title}__${tagString}`;
    
    let sluggedTitle = slugify(newTitle);
    
    let datedTitle = getDateStamp();
    
    // Set the new title to the draft
    draft.setTemplateTag("titleWithTags", newTitle);
    
    function slugify(text) {
        return text.toString().toLowerCase()
            .replace(/\s+/g, '-')           // Replace spaces with hyphens
            .replace(/[^\w\-]+/g, '')       // Remove all non-word characters
            .replace(/\-\-+/g, '-')         // Replace multiple hyphens with a single hyphen
            .replace(/^-+/, '')             // Trim leading hyphens
            .replace(/-+$/, '');            // Trim trailing hyphens
    }
    
    // Function to get current date and time as YYYYMMDDHHMMSS
    function getDateStamp() {
        let now = new Date();
        let year = now.getFullYear();
        let month = String(now.getMonth() + 1).padStart(2, '0');  // Months are 0-indexed
        let day = String(now.getDate()).padStart(2, '0');
        let hours = String(now.getHours()).padStart(2, '0');
        let minutes = String(now.getMinutes()).padStart(2, '0');
        let seconds = String(now.getSeconds()).padStart(2, '0');
        return `${year}${month}${day}T${hours}${minutes}${seconds}`;
    }
    
    let fileName = datedTitle+"--"+sluggedTitle+ ".md";
    let fileContent = draft.content;
    
    draft.setTemplateTag("filename", fileName);
    
  • file

    fileNameTemplate
    [[filename]]
    folderTemplate
    template
    ---
    title: "[[line|1]]"
    date:  [[date]]T[[date|%H:%M:%S%z]]
    tags:   [[[mytag]]]
    identifier: "[[date|%Y%m%dT%H%M%S]]"
    ---
    
    [[trimmed_body]]
    local
    true
    writeType
    create

Options

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