const pages = dv.pages('"SiteMap/Definitions"')
  .where(p => p.file.path !== dv.current().file.path && p.Text != null && p.Text[0] != null)
  .sort(p => p.file.name, "asc");
 
const getKey = (p) => {
  const t = Array.isArray(p.Text) ? p.Text[0] : p.Text;
  if (t && typeof t === "object" && t.path) return t.path;
  return String(t ?? "__uncategorized__");
};
 
const getLabel = (p) => {
  const t = Array.isArray(p.Text) ? p.Text[0] : p.Text;
  if (t && typeof t === "object" && t.path)
    return t.display ?? t.path.split("/").pop();
  return String(t ?? "Uncategorized");
};
 
const groups = pages.groupBy(p => getKey(p));
 
for (let group of groups.sort(g => g.key, "asc")) {
  const label = getLabel(group.rows[0]);
  dv.header(2, label);
  for (let page of group.rows) {
    dv.paragraph(`**${page.file.link}**`);
    if (page.Definition) dv.paragraph(`> ${page.Definition}`);
  }
}