Skip to content

Commit

Permalink
feat: add richText builder, fix exports
Browse files Browse the repository at this point in the history
  • Loading branch information
EndBug committed Jun 3, 2022
1 parent aa99ef4 commit 240d128
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
47 changes: 47 additions & 0 deletions src/builders.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,53 @@
import {Color} from 'notion-api-types/global';
import {Blocks, Block, RichText} from 'notion-api-types/requests';
import {Blocks as ResponseBlocks} from 'notion-api-types/responses';
import {LIMITS} from './constants';
import {chunkString} from './internal';

export interface RichTextOptions {
type?: 'text' | 'equation'; // 'mention' is not supported
annotations?: RichText['annotations'];
url?: string;
}
export function richText(
content: string,
options: RichTextOptions = {}
): RichText[] {
const annotations: RichText['annotations'] = {
bold: false,
strikethrough: false,
underline: false,
italic: false,
code: false,
color: 'default' as const,
...((options.annotations as RichText['annotations']) || {}),
};

if (options.type === 'equation') {
return chunkString(content, LIMITS.RICH_TEXT.EQUATION_EXPRESSION).map(
expression => ({
type: 'equation',
annotations,
equation: {
expression,
},
})
);
} else
return chunkString(content, LIMITS.RICH_TEXT.TEXT_CONTENT).map(str => ({
type: 'text',
annotations,
text: {
content: str,
link: options.url
? {
type: 'url',
url: options.url,
}
: undefined,
},
}));
}

export function paragraph(
text: RichText[],
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * as Blocks from './builders';
export * as Builders from './builders';
export * as Constants from './constants';
export * as Types from './types';
export * as Utils from './utils';
export * from './types';
export * from './utils';
6 changes: 6 additions & 0 deletions src/internal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Splits `str` into chunks with a maximum length of `len`.
*/
export function chunkString(str: string, len: number): string[] {
return str.match(new RegExp('.{1,' + len + '}', 'g')) || [];
}
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Code} from 'notion-api-types/responses/blocks';

export * from 'notion-api-types';
export type {Endpoints, NotionRequest, NotionResponse} from 'notion-api-types';
export type CodeLang = Code['code']['language'];

0 comments on commit 240d128

Please sign in to comment.