Compile one-page admonitions

I record my exchanges with Claude AI according to a simple structure:

  • Question type admonition (for the question)
  • Full text of the response
  • Abstract type admonition (for synthesis)

Some exchanges can have 1500 lines.

The code below compiles the admonitions (Question and Abstract) of a given page. It inserts a link to the position of each admonition (like a summary) and adds a dividing line between each pair of Question-Abstract.

The space-lua function:


-- priority: 10  
function extractAdmonitionsFromPage(pageName)  
  local text = space.readPage(pageName)  
  local admonitions = {}  
    
  -- Divide text into lines  
  local lines = {}  
  for line in text:gmatch("[^\r\n]+") do  
    table.insert(lines, line)  
  end  
    
  -- Calculate the start positions of each line  
  local linePositions = {}  
  local currentPos = 0  
  for _, line in ipairs(lines) do  
    table.insert(linePositions, currentPos)  
    currentPos = currentPos + #line + 1 -- +1 for line break  
  end  
    
  local i = 1  
  while i <= #lines do  
    local line = lines[i]  
      
    -- Find blockquote lines with StrongEmphasis  
    local type = line:match("^> %*%*(%w+)%*%*")  
    if type then  
      local lowerType = type:lower()  
      if lowerType == "question" or lowerType == "abstract" then  
        local content = {}  
          
        -- Extract the rest of the first line (after the type)  
        local restOfLine = line:match("^> %*%*" .. type .. "%*%*(.+)")  
        if restOfLine then  
          -- Transform first line into wiki link  
          local firstLineWithLink = "[[" .. pageName .. "@" .. linePositions[i] .. "|" .. lowerType:upper():gsub('ABSTRACT', 'RÉSUMÉ') .. restOfLine .. "]]"  
          table.insert(content, firstLineWithLink)
        end  
          
        -- Collect the following lines  
        i = i + 1  
        while i <= #lines do  
          local nextLine = lines[i]  
            
          if not nextLine:match("^>") then  
            break  
          end  
            
          if nextLine:match("^> %*%*%w+%*%*") then  
            break  
          end  
            
          local contentLine = nextLine:gsub("^>%s*", "")  
          -- Filter empty lines (containing only spaces or nothing)  
          if contentLine:match("%S") then
            table.insert(content, contentLine)  
          end  
            
          i = i + 1  
        end  
          
        -- Add found admonition  
        if #content > 0 then
          -- Add a separator line after the abstracts
          if lowerType == "abstract" then
            table.insert(content, '\n---')
          end
          table.insert(admonitions, {   
            content = table.concat(content, "\n"):match("^%s*(.-)%s*$")  
          })
        end  
      else  
        i = i + 1  
      end  
    else  
      i = i + 1  
    end  
  end  
    
  return admonitions  
end

and the template:

${template.each(extractAdmonitionsFromPage("page_name"), template.new[==[ ${_.content}
]==])}