developed a lightweight plugin designed to assist .md plug developers (see this thread and this discussion, among others)
Function: If the frontmatter lacks a githubUrl, populate it with a direct GitHub link based on the current note’s path.
also included recommendation ratings, update dates, correct urls, whitespace handling, key existence checks.
command.define {
name = "Frontmatter: Add githubUrl",
key = "Ctrl-Alt-g",
run = function()
local text = editor.getText()
local fmExtract = index.extractFrontmatter(text) or {}
local fm = fmExtract.frontmatter or {}
if type(fm.recommend) == "string" and fm.recommend ~= "" then
-- preserve exsiting value
editor.flashNotification("\"recommend\" already Set", "info")
else
fm.recommend = "⭐⭐⭐⭐⭐"
editor.flashNotification("recommend updated: " .. fm.recommend, "info")
end
if type(fm.udpateDate) == "string" and fm.udpateDate == os.date("%Y-%m-%d") then
-- modify exsiting value
editor.flashNotification("\"udpateDate\" already Set", "info")
else
fm.udpateDate = os.date("%Y-%m-%d")
editor.flashNotification("udpateDate added: " .. fm.udpateDate, "info")
end
local body = index.extractFrontmatter(text, {
removeFrontMatterSection = true }).text or text
local function replace_space_with_percent20(s)
local parts = {}
for i = 1, #s do
local c = s:sub(i, i)
if c == " " then
parts[#parts+1] = "%20"
else
parts[#parts+1] = c
end
end
return table.concat(parts)
end
local url = "https://github.com/ChenZhu-Xie/xczphysics_SilverBullet/blob/main/" .. tostring(editor.getCurrentPath())
-- editor.flashNotification(url)
-- editor.flashNotification(string.gsub(url, " ", "%20"))
-- editor.flashNotification(string.gsub(url, " ", "%%20"))
githubUrl_original = "\"" .. fm.githubUrl .. "\""
-- editor.flashNotification(githubUrl_original)
-- fm.githubUrl = replace_space_with_percent20(url)
fm.githubUrl = "\"" .. replace_space_with_percent20(url) .. "\""
local lines = {}
for k, v in pairs(fm) do
if type(v) == "table" then
-- editor.flashNotification(k)
if #v > 0 then
table.insert(lines, k .. ":")
for _, val in ipairs(v) do
table.insert(lines, " - " .. tostring(val))
end
end
else
-- editor.flashNotification(string.format("%s: %s", k, tostring(v)))
table.insert(lines, k .. ": " .. tostring(v))
end
end
-- editor.flashNotification(lines)
local fmText = table.concat(lines, "\n")
local newText = string.format("---\n%s\n---%s", fmText, body)
editor.setText(newText, false)
if type(fm.githubUrl) == "string" and fm.githubUrl ~= "" then
-- modify exsiting value
if fm.githubUrl ~= githubUrl_original then
editor.flashNotification("githubUrl updated", "info")
else
editor.flashNotification("\"githubUrl\" already Set", "info")
end
else
editor.flashNotification("githubUrl added", "info")
end
end
}

