Plugin: Amplequery

name

Amplequery

description

Generate a list of notes that match certain criteria. Can filter by "fields" inside notes as well as tags.

instructions


After installing the plugin, type { and you will have 2 new options:
1. Amplequery: Field
2. Amplequery: Tag

"Filter by field" returns a list of notes that match a certain field value, where fields are defined in a table at the top of notes. For performance considerations, this plugin only searches inside notes with a certain tag (as you define in the settings screen). Can be improved once support for inserting tables is added.

"Filter by tag" returns a list of notes that match a certain tag query.

icon

table-search

setting

Tag to search in


linkTodo


linkChange log:

August 10th, 2023: Fix the tag query not inserting markdown


linkCode block

{
insertText: {
/**
// June 19th 2023: Commented out because this function will crash for larger notes;
// Will attempt to fix ASAP
"Empty": async function(app) {
try {
const noteHandles = await app.filterNotes({tag: "^auto-archived"});
console.log("Done");
const emptyNoteLinks = [];
let count = 0;
 
for (const noteHandle of noteHandles) {
const noteContent = await app.getNoteContent(noteHandle);
if (noteContent.trim() === "") {
emptyNoteLinks.push(`* [${noteHandle.name}](https://www.amplenote.com/notes/${noteHandle.uuid})`);
}
count += 1;
}
await app.context.replaceSelection(emptyNoteLinks.join("\n"));
return emptyNoteLinks.join("\n");
} catch (err) {
await app.alert(err);
}
},*/
 
"Field": async function(app) {
try {
let notes = await app.filterNotes({tag: app.settings["Tag to search in"]});
// Get all "fields" found in the selected notes
let attributes = await this._getAllAttributesFromNoteHandles(app, notes);
 
let field = await app.prompt(`Which field would you like to filter by?\n Options: ${Object.keys(attributes).join(', ')}`);
let value = await app.prompt(`What value would you like to filter on?`);
 
let result = [];
for (const key in attributes[field]) {
if (attributes[field][key] == value) {
result.push(key);
}
}
let noteUUID = await app.createNote("Query results");
await app.insertContent({uuid: noteUUID}, this._generateMDList(result));
}
catch(error) {
app.alert(String(error));
}
},
 
"Tag": async function(app) {
try {
let tagName = await app.prompt("What tag query to filter on?");
let notes = await app.filterNotes({tag: tagName});
 
const self = this;
let noteUUID = await app.createNote("Query results");
let result = this._generateMDList(notes.map(obj => self._createMDLinkFromNoteHandle(obj)));
await app.insertContent(
{uuid: noteUUID},
result
);
await app.context.replaceSelection(result);
// return result;
}
catch(error) {
app.alert(String(error));
}
}
},
 
async _getAllAttributesFromNoteHandles(app, notes) {
let attributes = {};
for (let i = 0; i < notes.length; i++) {
let note = notes[i];
let contents = await app.getNoteContent({uuid: note.uuid});
const _attributes = this._getDictFromTable(contents);
for (const key in _attributes) {
if (!attributes[key]) {
attributes[key] = {}; // Initialize an empty object if it doesn't exist
}
attributes[key][this._createMDLinkFromNoteHandle(note)] = _attributes[key];
}
}
return attributes;
},
 
_createMDLinkFromNoteHandle(noteHandle) {
return `[${noteHandle.name}](https://www.amplenote.com/notes/${noteHandle.uuid})`
},
 
_generateMDList(input) {
return input.map(item => `- ${item}`).join('\n');
},
 
_getDictFromTable(input) {
let result = {};
const lines = input.split("\n");
const table_lines = lines.filter(str => str.startsWith("|"));
for (let i = 2; i < table_lines.length; i++) {
const [_, key, value, ...rest] = table_lines[i].split("|");
result[key] = value;
}
return result;
},
 
_generateMarkdownTable(data) {
// Get all unique note names
const notes = Array.from(new Set(Object.values(data).flatMap(Object.keys)));
 
// Get all attribute names
const attributes = Object.keys(data);
 
// Generate table header
let table = '| ' + ['Note', ...attributes].join(' | ') + ' |\n';
table += '| ' + attributes.map(_ => '---').join(' | ') + ' |\n';
 
// Generate table body
notes.forEach(note => {
let row = '| ' + note + ' ';
attributes.forEach(attr => {
row += '| ' + (data[attr][note] || '-') + ' ';
});
row += '|\n';
table += row;
});
 
return table;
}
}