It’s after midnight, so I guess it’s tomorrow, and I built a script to list all fields from frontmatter.
The list all frontmatter fields script
```dataviewjs
// Store an alias for long function name
const listProp = Object.getOwnPropertyNames
// Get frontmatter from every page having one
const allMatter =
dv.pages()
.where(p => listProp(p.file.frontmatter).length > 0)
.file.frontmatter
// Loop all of the frontmatter, and count all fields
let fieldList = {}
for (let frontmatter of allMatter) {
// For each frontmatter section, list and count each field
for (let field of listProp(frontmatter)) {
// console.log(field)
if (field in fieldList) {
fieldList[field] += 1
} else {
fieldList[field] = 1
}
}
}
// Build an ordinary array out of the dictionary
let fieldCounts = []
for (let key in fieldList) {
fieldCounts.push([key, fieldList[key]])
}
// Sort the array for display purposes
fieldCounts = fieldCounts.sort((a, b) => a[1] - b[1])
// console.log(fieldCounts)
// Present the final table of all fields found in
// frontmatter, with the most frequently used fields
// at the end of the table
dv.table(["name", "count"],
dv.array(fieldCounts)
.map(m => [m[0], m[1]])
)
```
This script does the following:
- Store all frontmatter section from all notes having one into
allMatter
- Loop through
allMatter
and split into the separate fields, and store the count of these fields intofieldList
(a dictionary object) - Build another array,
fieldCount
, based uponfieldList
, and sort this according to the frequency of the field - Output
fieldCount
into avdv.table()
, with the least frequent field names on the top, and the most used on the bottom
Review of fields table
Then one can review the table, and see if there are some fields one would want to eliminate or consolidate. To help localise the various fields, the following snippet would most likely be useful:
```dataview
table file.frontmatter
where file.frontmatter and createddate
```
In the example above I’m listing the frontmatter with originating file for the field createddate
, this of course you replace with whatever field you’re looking. If the field name has a space in it, replace with a dash/minus, aka Creation date
becomes creation-date
. The key name doesn’t depend on case.
Extra nugget: I’m so far not able to split up/flatten the file.frontmatter
list of various fields in a DQL query. Are some of you up for that task?