Markdown Formatter for Silverbullet

Hey everyone :waving_hand:

I just released a new Markdown Formatter plugin for Silverbullet!

This plugin uses Prettier under the hood to format your notes consistently and cleanly.

:sparkles: Features

  • Formats your Markdown documents with Prettier’s opinionated style
  • Supports lists, headings, tables, and inline formatting
  • Can be triggered via a command or automatically on save (configurable)
  • Built with Deno and Prettier’s official Markdown parser

:wrench: Usage

Run the command: Editor: Format

Your current note will be reformatted instantly :bullseye:

If you want to format a portion of the document, select the text and run Editor: Format

:red_question_mark: I was curious to know if this can be achieved purely with space-lua?

6 Likes

:red_question_mark: I was curious to know if this can be achieved purely with space-lua?

local prettier = js.import("https://cdn.jsdelivr.net/npm/[email protected]/standalone/+esm")
local prettierMarkdown = js.import("https://cdn.jsdelivr.net/npm/[email protected]/plugins/markdown/+esm")

function formatText(text)
  return prettier.format(text, { parser = 'markdown', plugins =  { prettierMarkdown } })
end

function formatDocument()
  local text = editor.getText()
  local formattedText = formatText(text)
  editor.setText(formattedText)
end

function formatSelection()
  local selection = editor.getSelection()
  if selection.from == selection.to then
    return
  end
  local text = editor.getText()
  local selectedText = text.slice(selection.from, selection.to)
  local formattedText = formatText(selectedText)
  formattedText = text.substring(0, selection.from) + formattedText.slice(0, -1) + text.substring(selection.to)
  editor.setText(formattedText)
end

function formatSmart()
  local selection = editor.getSelection()
  if selection.from != selection.to then
    formatSelection()
  else
    formatDocument()
  end
end

command.define {
  name = "Editor: Format",
  run = formatSmart,
}
5 Likes

Thanks for this, what theme are you running? I like the look.

I took one from the community and modified it to reflect eva theme.

I’ll share it in some time.

Retouching some parameters

@zef it formats correctly md table.

1 Like