@narkdown/notion-parser
Version:
Parse notion response
216 lines (162 loc) • 6.03 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.url = exports.title = exports.select = exports.rich_text = exports.properties = exports.phone_number = exports.number = exports.multi_select = exports.last_edited_time = exports.getRows = exports.email = exports.date = exports.created_time = exports.checkbox = exports.applyLink = exports.applyAnnotations = exports.annotationParsers = exports.SUPPORTED_PROPERTIES = void 0;
var _utils = require("./utils");
const SUPPORTED_PROPERTIES = ['title', 'rich_text', 'number', 'select', 'multi_select', 'date', // 'files',
'checkbox', 'url', 'email', 'phone_number', // 'formula',
// 'relation',
// 'rollup',
// 'people',
// 'created_by',
'created_time', // 'last_edited_by',
'last_edited_time'];
exports.SUPPORTED_PROPERTIES = SUPPORTED_PROPERTIES;
const annotationParsers = {
bold: string_ => `**${string_}**`,
italic: string_ => `_${string_}_`,
strikethrough: string_ => `~~${string_}~~`,
underline: string_ => `<u>${string_}</u>`,
code: string_ => `\`${string_}\``,
color: string_ => `${string_}`
};
exports.annotationParsers = annotationParsers;
const applyAnnotations = (text, annotations) => {
let annotatedText = text;
const annotationKeys = (0, _utils.objectEntries)(annotations).filter(([, value]) => value).map(([key]) => key);
for (const key of annotationKeys) {
annotatedText = annotationParsers[key](annotatedText);
}
return annotatedText;
};
exports.applyAnnotations = applyAnnotations;
const applyLink = (text, href) => `[${text}](${href})`;
exports.applyLink = applyLink;
const title = value => value.title.map(({
plain_text,
annotations,
href
}) => href ? applyLink(applyAnnotations(plain_text, annotations), href) : applyAnnotations(plain_text, annotations)).join('');
exports.title = title;
const rich_text = value => value.rich_text.map(({
plain_text,
annotations,
href
}) => href ? applyLink(applyAnnotations(plain_text, annotations), href) : applyAnnotations(plain_text, annotations)).join('');
exports.rich_text = rich_text;
const number = ({
number
}) => number;
exports.number = number;
const select = ({
select
}) => {
var _select$name;
return (_select$name = select === null || select === void 0 ? void 0 : select.name) !== null && _select$name !== void 0 ? _select$name : '';
};
exports.select = select;
const multi_select = ({
multi_select
}) => {
var _multi_select$0$name, _multi_select$;
return (_multi_select$0$name = (_multi_select$ = multi_select[0]) === null || _multi_select$ === void 0 ? void 0 : _multi_select$.name) !== null && _multi_select$0$name !== void 0 ? _multi_select$0$name : '';
};
exports.multi_select = multi_select;
const date = options => ({
date
}) => date !== null && date !== void 0 && date.end ? `${(0, _utils.formatDate)(date.start, options)} ~ ${(0, _utils.formatDate)(date.end, options)}` : `${(0, _utils.formatDate)(date.start, options)}`; // Unsupported
// export const files = ({files}: PropertyValue<'files'>) => files;
exports.date = date;
const checkbox = ({
checkbox
}) => checkbox ? ':white_check_mark:' : ':x:';
exports.checkbox = checkbox;
const url = ({
url
}) => url;
exports.url = url;
const email = ({
email
}) => email;
exports.email = email;
const phone_number = ({
phone_number
}) => phone_number; // Unsupported
// export const formula = ({formula}: PropertyValue<'formula'>) => formula;
// export const relation = ({relation}: PropertyValue<'relation'>) => relation;
// export const rollup = ({rollup}: PropertyValue<'rollup'>) => rollup;
// export const people = ({people}: PropertyValue<'people'>) => people;
// export const created_by = ({created_by}: PropertyValue<'created_by'>) =>
// created_by;
exports.phone_number = phone_number;
const created_time = options => ({
created_time
}) => (0, _utils.formatDate)(created_time, options); // Unsupported
// export const last_edited_by = ({
// last_edited_by,
// }: PropertyValue<'last_edited_by'>) => last_edited_by;
exports.created_time = created_time;
const last_edited_time = options => ({
last_edited_time
}) => (0, _utils.formatDate)(last_edited_time, options);
exports.last_edited_time = last_edited_time;
const properties = options => value => {
switch (value.type) {
case 'title':
return title(value);
case 'rich_text':
return rich_text(value);
case 'number':
return number(value).toString();
case 'select':
return select(value);
case 'multi_select':
return multi_select(value);
case 'date':
return date(options === null || options === void 0 ? void 0 : options.date)(value);
// Unsupported
//
// case 'files':
// return files(value);
case 'checkbox':
return checkbox(value);
case 'url':
return url(value);
case 'email':
return email(value);
case 'phone_number':
return phone_number(value);
// Unsupported
//
// case 'formula':
// return formula(value);
// case 'relation':
// return relation(value);
// case 'rollup':
// return rollup(value);
// case 'people':
// return people(value);
// case 'created_by':
// return created_by(value);
case 'created_time':
return created_time(options === null || options === void 0 ? void 0 : options.date)(value);
// Unsupported
//
// Case 'last_edited_by':
// return last_edited_by(value);
case 'last_edited_time':
return last_edited_time(options === null || options === void 0 ? void 0 : options.date)(value);
default:
return null;
}
};
exports.properties = properties;
const getRows = options => results => results.map(({
properties
}) => properties).map(row => Object.fromEntries(Object.entries(row).map(([key, propertyValue]) => {
const value = properties(options)(propertyValue);
return [key, value ? (0, _utils.escape)(value) : value];
})));
exports.getRows = getRows;
//# sourceMappingURL=database.js.map
;