Post

Obsidian Dataview Snippet #1 Headers Review

Primeiro de diversos snippets que vou jogar por aqui,

Função

Formata uma tabela com os primeiros headers nas duas notas diárias em intervalos específicos, pode ser muito útil para usar no periodic-notes1 permitindo uma forma fácil de revisar um mês ou mais das notas em uma única nota, segue um print abaixo.

é isso

Como usar

  • instale a ative o plugin - https://github.com/blacksmithgu/obsidian-dataview
  • Nas configurações do plugin ative Enable JavaScript queries
  • Crie um template ou salve o snippet abaixo,
    • –ou simplesmente cole fsdsss
  • Defina na primeira variável o caminho das suas notas diárias.
  • Então o intervalo de datas que deseja buscar.
  • Por fim troque o js no rótulo do bloco por dataviewjs.
    • É isso.

SNIPPET

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

// ⚙️ Configurações
const folderPath = "CAMINHO-DA-SUA-PASTA"; //  Exemplo : "CALENDAR/DAILY"   

const dateRange = {
  start: new Date("2025-02-01"),
  end: new Date("2025-04-01")
};

const maxHeaders = 3;

// Filtra páginas por pasta e data
let tableRows = [];
let headerNames = []; 

const pages = dv.pages()
  .where(p => p.file.path.startsWith(folderPath))
  .where(p => {
    const d = new Date(p.file.name);
    return d >= dateRange.start && d <= dateRange.end;
  })
  .sort(p => p.file.name, 'asc');

for (const page of pages) {
  const lines = (await dv.io.load(page.file.path)).split('\n');
  const headers = [], sections = {}, headerRegex = /^# (.+)/;
  let current = null;

  for (const l of lines) {
    const m = l.match(headerRegex);
    if (m && headers.length < maxHeaders) {
      current = m[1].trim();
      headers.push(current);
      sections[current] = [];
    } else if (current && headers.includes(current)) {
      sections[current].push(l.trim());
    }
  }

  // Armazena os nomes reais dos headers da primeira nota com headers suficientes
  if (headerNames.length === 0 && headers.length > 0) {
    headerNames = [...headers];
  }

  const row = [page.file.link, ...headerNames.map(h => sections[h]?.join('\n') || "")];
  tableRows.push(row);
}

// Títulos reais das colunas
const columnTitles = ["🗓️", ...headerNames.map(h => `${h}`)];

dv.table(columnTitles, tableRows);


Possíveis erros

  • Caso use outro padrão nos nomes das notas diárias tal como DD-MM-YYYY terá que trocar pelo snippet abaixo.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// ⚙️ Configurações
const folderPath = "CAMINHO-DA-SUA-PASTA"; // Exemplo: "CALENDAR/DAILY"
const dateRange = {
  start: new Date("2025-03-01"),
  end: new Date("2025-04-01")
};
const maxHeaders = 3;

let tableRows = [];
let headerNames = [];

// Função para converter "DD-MM-YYYY" para Date
function parseBrazilianDate(dateStr) {
  const [day, month, year] = dateStr.split("-").map(Number);
  return new Date(year, month - 1, day);
}

// Carrega e filtra as páginas
const pages = dv.pages()
  .where(p => p.file.path.startsWith(folderPath))
  .where(p => {
    const fileDate = parseBrazilianDate(p.file.name.split(" ")[0]);
    return fileDate >= dateRange.start && fileDate <= dateRange.end;
  })
  .sort(p => parseBrazilianDate(p.file.name.split(" ")[0]), 'asc');

for (const page of pages) {
  const lines = (await dv.io.load(page.file.path)).split('\n');
  const headers = [], sections = {}, headerRegex = /^# (.+)/;
  let current = null;

  for (const l of lines) {
    const m = l.match(headerRegex);
    if (m && headers.length < maxHeaders) {
      current = m[1].trim();
      headers.push(current);
      sections[current] = [];
    } else if (current && headers.includes(current)) {
      sections[current].push(l.trim());
    }
  }

  if (headerNames.length === 0 && headers.length > 0) {
    headerNames = [...headers];
  }

  const row = [page.file.link, ...headerNames.map(h => sections[h]?.join('\n') || "")];
  tableRows.push(row);
}

const columnTitles = ["🗓️", ...headerNames.map(h => `${h}`)];
dv.table(columnTitles, tableRows);

This post is licensed under CC BY 4.0 by the author.