Skip to content

Instantly share code, notes, and snippets.

@wbhouston
Last active April 29, 2025 18:42
Show Gist options
  • Select an option

  • Save wbhouston/75e67fcd19f66a128f2f6efb7d684def to your computer and use it in GitHub Desktop.

Select an option

Save wbhouston/75e67fcd19f66a128f2f6efb7d684def to your computer and use it in GitHub Desktop.

#meta

util = util or {}

function printTree(tree, level)
  -- recursive
  local indentation = string.rep(" ", level * 2)
  local response = "\n" .. indentation
  local typesToSkipChildren = {
    "EmphasisMark",
    "CodeMark",
    "LuaDirectiveMark",
    "TaskMark",
    "WikiLinkMark",
  }
  
  if tree.type then
    response = response .. tree.type .. " (" .. tree.from ..  " to " .. tree.to .. ")"
    if not table.includes(typesToSkipChildren, tree.type) then
      response = response .. ": "
    end
  else
    response = response .. "Leaf: "
  end
  
  if tree.text then
    local text = string.gsub(tree.text, "\n", "<newline>")
    response = response ..  text
  end
  
  if tree.children and not table.includes(typesToSkipChildren, tree.type) then
    for child in tree.children do
      response = response .. printTree(child, level + 1)
    end
  end
  
  return response
end

util.prettyPrintMarkdown = function(page)
  parsedPage = markdown.parseMarkdown(space.readPage(page))
  return printTree(parsedPage)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment