nVim/luasnip/markdown.lua
Robert Morrison f2161af221 feat(luasnip): add custom snippet
Add a custom snippet for markdown to insert a YAML metadata block
This is a highly targeted snippet as it works for my blog template only.

There is some work to do on neatening this snippet up
2026-04-10 02:58:26 +01:00

110 lines
2.5 KiB
Lua

-- Markdown snippets
-- We manually create these as the LSP doesn't understand
-- if they are loaded properly.
local ls = require('luasnip')
local lsf = require('luasnip.extras.fmt')
local s = ls.snippet
local t = ls.text_node
local i = ls.insert_node
local c = ls.choice_node
local sn = ls.snippet_node
local fmt = lsf.fmt
local fmta = lsf.fmta
local currentUser
if vim.fn.has('linux') == 1 then
--- Get the current users full name
---@return string @The users full name OR NAME
currentUser = function ()
local status, res = require('sherlock5512.scriptutils').shellexec("getent passwd $USER | cut -d: -f5 | cut -d, -f1")
if status.ok then
-- We know there will only be stdout if the command runs successfully
---@diagnostic disable-next-line: return-type-mismatch, need-check-nil
return res:match("^[\r\n]*(.-)[\r\n]*$")
end
return "NAME"
end
elseif vim.fn.has('mac') == 1 then
--- Get the current users full name
---@return string @The users full name OR NAME
currentUser = function ()
local status, res = require('sherlock5512.scriptutils').shellexec("id -F")
if status.ok then
-- We know there will only be stdout if the command runs successfully
---@diagnostic disable-next-line: return-type-mismatch, need-check-nil
return res:match("^[\r\n]*(.-)[\r\n]*$")
end
return "NAME"
end
else
--- Get the current users full name
---@return string @The users full name OR NAME
currentUser = function ()
return "NAME"
end
end
--- @module 'luasnip'
--- @type LuaSnip.Snippet[]
return {
s(
{ trig = '\\yaml', dscr = 'YAML frontmatter block'}, {
t {'---',''}, -- end with empty block to force a newline
t {'author: "' },
i(1, currentUser()),
t {'"',''},
t {'title: "'},
i(2),
t {'"', ''},
t {'date: '},
i(3),
t {'',''},
t {'description: "'},
i(4),
t {'"', ''},
c(5,
{
t '',
sn(nil,{
t {"keywords: [ "},
i(1),
t { ' ]', '' },
})
}
),
t {"lang: "},
i(6, 'en-gb'),
t {'', ''},
c(7, {
sn(nil, {
t {'math: true', ''},
i(1),
c(2, {
t {'mathjax: true', ''},
t {'mathjax: false', ''}
}),
}),
t {'math: false', ''},
}),
c(8, {
t {'needs-syntax-highlighting: true', ''},
t {'needs-syntax-highlighting: false', ''},
}),
c(9, {
t {'allow-comments: true', ''},
t {'allow-comments: false', ''},
}),
c(10, {
t '',
sn(nil, {
t {'style: "', ''},
i(1),
t {'','"', ''}
})
}),
t {'---',''}
})
}