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,
}