l7note
Version:
Access your notion notes quick
125 lines (112 loc) • 2.74 kB
text/typescript
import { Client } from '@notionhq/client';
import chalk, { Chalk } from 'chalk';
import { exit } from 'process';
import { displayNotes } from '../displayNotes.js';
import { getSettingValueByName } from '../helper/getSettingValueByName.js';
import { Note } from '../Note.js';
import { globalConfig } from '../setup.js';
const addTagText = (text: string, color: string) => {
switch (color) {
case 'red':
return `${chalk.bgHex('#ffe2dd').black(text)} `;
case 'pink':
return `${chalk.bgHex('#F5E0E9').black(text)} `;
case 'purple':
return `${chalk.bgHex('#e8deee').black(text)} `;
case 'blue':
return `${chalk.bgHex('#D3E5EF').black(text)} `;
case 'green':
return `${chalk.bgHex('#DBEDDB').black(text)} `;
case 'yellow':
return `${chalk.bgHex('#FDECC8').black(text)} `;
case 'orange':
return `${chalk.bgHex('#fadec9').black(text)} `;
case 'brown':
return `${chalk.bgHex('#EEE0DA').black(text)} `;
}
};
const noteList: Note[] = [];
const getList = async () => {
const notion = new Client({ auth: globalConfig.token });
const databaseId = globalConfig.dbId;
if (databaseId == undefined) {
return;
}
let filter: any = {};
if (globalConfig.optionalArgs.length > 0) {
filter = {
and: [
{
property: 'Done',
checkbox: {
equals: false,
},
},
{
or: [
{
property: 'Title',
text: {
contains: getSettingValueByName('-h'),
},
},
{
property: 'Text',
text: {
contains: getSettingValueByName('-h'),
},
},
{
property: 'Tags',
multi_select: {
contains: getSettingValueByName('-h'),
},
},
],
},
],
};
} else {
filter = {
and: [
{
property: 'Done',
checkbox: {
equals: false,
},
},
],
};
}
const response = await notion.databases.query({
database_id: databaseId,
filter: filter,
});
response.results.forEach((el: any) => {
const title = el.properties.Title.title
.map((texts: any) => texts.plain_text + ' ', '')
.join(' ');
const text = el.properties.Text.rich_text
.map((texts: any) => texts.plain_text + ' ', '')
.join(' ');
const tags = el.properties.Tags.multi_select
.map((tag: any) => addTagText(tag.name, tag.color), '')
.join(' ');
const note = new Note(
el.id,
el.url,
title,
text,
tags,
el.created_time,
el.last_edited_time
);
noteList.push(note);
});
if (noteList.length <= 0) {
console.log(chalk.blue('No results found'));
exit(1);
}
displayNotes(noteList);
};
export { getList };