Action Group

Markdown Scripting

Last update about 1 year ago

UPDATES

about 1 year ago

Drop action that was specific to my vault

show all updates...

about 1 year ago

Drop action that was specific to my vault

about 1 year ago

Work in current timezone

about 1 year ago

Break unit tests into separate action

about 1 year ago

Rename and change icons

Markdown Helpers for Drafts

The Markdown Helpers is a curated set of utility functions designed to enhance your experience when working with Markdown files in the Drafts app. These functions are particularly beneficial for appending content to specific sections within a Markdown document, making them ideal for users who manage their notes in an Obsidian vault or any other Markdown-based system accessed through Drafts Bookmarks.

Exposed Helper Functions

The following functions are exposed via globalThis.markdownHelpers and can be easily imported into your own actions. All you need to do is “Include Action” Markdown Helpers in your own custom action, and then follow it up with a Script action. Within your script action, you can access the Markdown Helpers via globalThis.markdownHelpers:

identifyMarkdownSections(markdownContent)

Identifies and returns an array of Markdown section headers from the provided text.

  • markdownContent: A string containing Markdown-formatted text.

promptUserForSectionFrom(content)

Prompts the user to select a section header from the provided Markdown content.

  • content: The string content of a Markdown file.

appendContentToSectionOfFile(bookmark, filePath, targetSection, content)

Appends content to a specified section of a file located at the given file path within the specified bookmark.

  • bookmark: The name of the bookmark in Drafts.
  • filePath: The file path relative to the bookmark location.
  • targetSection: The title of the section header where content should be appended.
  • content: The content to be appended within the section.

getFilePathForDate(baseDir, date)

Constructs a file path for a given date within a specified base directory.

  • baseDir: The base directory path.
  • date: A JavaScript Date object (optional, defaults to current date).

readFile(bookmark, filePath)

Reads and returns the content of a file at the given path within the specified bookmark.

  • bookmark: The name of the bookmark in Drafts.
  • filePath: The file path relative to the bookmark location.

Usage Example

To use these helpers in your own Drafts actions, first ensure that the Markdown Helpers action is published and available. Then, you can access the helper functions as follows:

// Main script
(() => {
    const markdownHelpers = globalThis.markdownHelpers;
    if (!markdownHelpers) {
        context.fail("Missing markdownHelpers functions. Run Import Markdown Helpers action first.");
        return;
    }

    const bookmark = "MyObsidianVault"; // Define your bookmark name here
    const targetSection = '# Tasks';
    const contentToAppend = draft.content.trim();
    markdownHelpers.appendContentToSectionOfFile(bookmark, "Tasks.md", targetSection, `- [ ] ${contentToAppend}`);
})();

By incorporating these helpers into your Drafts actions, you can leverage the power of Markdown for organized note-taking and content management. Whether you’re using Drafts to export to an Obsidian vault or any other bookmarked location, these utilities will streamline the process of appending your drafts to the appropriate sections of your documents.

In addition, this action group includes some helper actions that illustrate how to use these helpers in your custom actions.

Created by Luke Zilioli https://www.lukezilioli.com


Included Actions

  • Sample Actions
  • Append to Daily [draft created date]
  • Append to Today
  • Append to Today as Task
  • Script Helpers
  • Markdown Helpers
  • Unit Tests
  • Unit Test Markdown Helpers
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.