Add githubUrl to frontmatter (for .md plug developers)

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
}

would something like this would be useful?

to have a updateDate and a lastCommitDate in the forntmatter

and this query:

so you can simply check if is something updatable or if you have the latest?

@malys what do you think?

it’s easy to implement this in my Github Update Library, but at this point everything extra would be just overcomplicating a simple working thing.

i’ve already done this script and they work ok. but question is, is it necessary? i think it would be better tho think about an overall (more interactive) better library management, i’m open for ideas.

3 Likes

It’s perfectly fine, as long as it facilitates seamless interaction between the two ends — one for developing, sharing and maintaining scripts, and the other for acquiring, utilizing and feedbacking them. :slight_smile:

Whether consensus is reached or not is secondary — what truly matters is the inspiration it sparks and the follow-up efforts in infrastructural development.

PS, the date is inspired from (Script) Display a calendar for Journal entries - #10 by zeus-web, and the stars are inspired from Command in space-lua to set page attributes; about frontmatter operation: Stripping frontmatter - #2 by Mr.Red

1 Like