Files
silverbullet/Library/Malys/mdPrettier.md
2026-02-05 12:53:43 +00:00

1.5 KiB

author, description, name, tags, share.uri, share.hash, share.mode
author description name tags share.uri share.hash share.mode
malys Beautify md file. Library/Malys/mdPrettier meta/library https://github.com/malys/silverbullet-libraries/blob/main/src/mdPrettier.md f2473a28 pull

Md Prettier

Beautify markdown.

Code

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

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

    printWidth = 160,
    proseWrap = "preserve",

    -- These DO NOT affect markdown tables
    useTabs = true,
    tabWidth = 4,
  })
end

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

command.define {
  name = "Beautify: markdown",
  run = formatDocument,
}

Important Notes:

  • Placement: prettier-ignore must be the very first line within the code fence. Any leading whitespace will cause it to be ignored.
  • Scope: prettier-ignore applies to the entire code fence it's placed in.
  • Alternative: If you want to disable formatting for a specific section of code within a code fence, you can use prettier-ignore on a single line:

Changelog

  • 2026-01-25:
    • feat: add option and ignore code

Community

Silverbullet forum