notion-api-js
Advanced tools
Comparing version 1.4.0 to 1.5.0
{ | ||
"name": "notion-api-js", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "", | ||
"repository": "/Frexeptabel/notion-api", | ||
"main": "src/notion.js", | ||
@@ -6,0 +7,0 @@ "scripts": { |
161
README.md
# Unofficial Notion.so Wrapper (WIP) | ||
This Repository contains an unoffical port of the [Notion](https://notion.so) to Node.js. | ||
![npm](https://img.shields.io/npm/v/notion-api-js.svg) | ||
![npm bundle size](https://img.shields.io/bundlephobia/min/notion-api-js.svg) | ||
This Repository contains an unofficial port of the [Notion](https://notion.so) to Node.js. **Important**: It only works using Node.js in backend and not in a client-side environment. | ||
**IMPORTANT**: You need a token to use the Notion API. You can obtain them by reading you local cookie. You can find instructions for that | ||
# Documentation | ||
- [Installation](#Installation) | ||
- [Dependencies](#Dependencies) | ||
- [Usage](#Usage) | ||
- [Obtaining Credentials](#Obtaining-Credentials) | ||
- [Instance Methods](#Instance-Methods) | ||
- [Disclaimer](#Disclaimer) | ||
# Installation | ||
You can either use npm or yarn to install it: | ||
``` | ||
npm i --save notion-api-js | ||
``` | ||
``` | ||
yarn add notion-api-js | ||
``` | ||
# Usage | ||
## Creating an instance | ||
To create an instance, simply pass an Object with your notion `userId` and `and token_v2`. | ||
```js | ||
// ES Modules syntax | ||
import Notion from "notion-api-js"; | ||
// require syntax | ||
const Notion = require("notion-api.js"); | ||
const notion = new Notion({ | ||
token: "YOUR_TOKEN_V2", | ||
userId: "YOUR_USERID" | ||
}); | ||
``` | ||
You can also provide options for the HTML parsing. | ||
```js | ||
const notion = new Notion({ | ||
token: "YOUR_TOKEN_V2", | ||
userId: "YOUR_USERID", | ||
options: { | ||
colors: { | ||
orange: 'CSS COLOR HERE' | ||
}, | ||
pageUrl: 'ABSOLUTE PAGE URL (e.g. /posts/'), | ||
} | ||
}); | ||
``` | ||
# Obtaining Credentials | ||
Right now there is no official way how to access the Notion API, but there is a little work-around to get your credentials. | ||
## Prerequisites | ||
You need to have an Account on Notion.so and need to be logged in. | ||
## Getting your credentials | ||
Most of the modern web browsers support inspecting cookies visually using the browser's devtools. | ||
You can read how to do it in your browser here: | ||
- [Chrome](https://developers.google.com/web/tools/chrome-devtools/manage-data/cookies) | ||
- [Firefox](https://developer.mozilla.org/en-US/docs/Tools/Storage_Inspector) | ||
After you found the Notion.so cookie, look for `token_v2` and `userId`. Those are the necessary credentials for the `Notion` instance. Simply copy them when you create the instance | ||
# Instance Options | ||
The Options are optionally passed to the instance as a parameter. Those options contain information on how the HTML will be parsed and returned using the instance methods. | ||
### Colors (Object) | ||
Contains definitions for the colors. If this option is omitted the default html colors like orange, pink and blue are used. You can change this behaviour by passing an object containing colour definitions. Example: | ||
```js | ||
options: { | ||
colors: { | ||
red: 'tomato', | ||
blue: 'rgb(100, 149, 237)', | ||
purple: '#9933cc', | ||
} | ||
} | ||
``` | ||
Possible colors are: | ||
- red | ||
- brown | ||
- orange | ||
- yellow | ||
- teal | ||
- blue | ||
- purple | ||
- pink | ||
- red | ||
### PageUrl (String) | ||
The PageUrl is the string passed to the `<a>` tag and is used to build the href of it. The id is inserted after the passed string. | ||
By default it looks like this `/page?id=`, which results into `<a href="/page?id=SOME_ID">Hello World</a>` | ||
# Instance Methods | ||
- [getPages](<#getPages()>) | ||
- [getPageById]() | ||
- [getAllHTML]() | ||
## getPages() | ||
Gets all pages of the user by the userId passed to the Notion instance. All pages are parsed to HTML. | ||
**Example** | ||
```js | ||
notion.getPages().then(pages => { | ||
// Your Code here | ||
}); | ||
``` | ||
## getPageById(pageId) | ||
Gets a Notion page by the pageId and returns it parsed to HTML. | ||
**Parameters**: | ||
| **Parameter** | **Type** | **Opt/Required** | | ||
| ------------- | -------- | ---------------- | | ||
| pageId | string | Required | | ||
**Example** | ||
```js | ||
notion.getPageById("pageID").then(page => { | ||
// Your Code here | ||
}); | ||
``` | ||
## getAllHTML() | ||
Gets the HTML for all pages. | ||
```js | ||
notion.getAllHTML().then(html => { | ||
// Your Code here | ||
}); | ||
``` | ||
# Disclaimer | ||
It's really WIP right now but I would highly appreciate if you would like to contribute to the project. Just fork this repo and create a PR 😄 |
@@ -0,2 +1,5 @@ | ||
// Seperator for good-looking HTML ;) | ||
const SEPERATOR = `\n `; | ||
// HTML Tag types | ||
const types = { | ||
@@ -14,15 +17,35 @@ page: 'a', | ||
function formatToHtml(ObjectToParse) { | ||
/** | ||
* Method that parses a Notion-Object to HTML | ||
* @param {*} ObjectToParse The Notion-Object | ||
* @param {*} options Options for parsing | ||
*/ | ||
function formatToHtml(ObjectToParse, options) { | ||
let { type, properties, format, id } = ObjectToParse; | ||
// Get color | ||
const color = format && format.block_color; | ||
// Replace color with custom color if passed | ||
const customColor = | ||
color && options.colors && (options.colors[color.split('_')[0]] || color); | ||
// Set content | ||
const content = properties && properties.title; | ||
const style = color ? ` style="color:${color}"` : ''; | ||
// Only set Style if passed | ||
const property = | ||
customColor && color.includes('background') | ||
? `style="background-color:${customColor.split('_')[0]}"` | ||
: `style="color:${customColor}"`; | ||
// Use ternary operator to return empty string instead of undefined | ||
const style = color ? ` ${property}` : ''; | ||
// Set type to break if no content is existant | ||
if (!content && type !== 'divider') { | ||
type = 'break'; | ||
} | ||
// Create HTML Tags with content | ||
switch (types[type]) { | ||
case types.page: { | ||
return `<${types.page} href="/page?id=${id}"${style}>${content}</${ | ||
types.page | ||
}>`; | ||
return `<${types.page} href="${options.pageUrl || | ||
'/page?id='}${id}"${style}>${content}</${types.page}>`; | ||
} | ||
@@ -45,7 +68,12 @@ case types.divider: { | ||
function formatList(ObjectList) { | ||
/** | ||
* Formats a List of objects to HTML | ||
* @param {*} ObjectList List of Notion-Objects | ||
* @param {*} options Options for parsing | ||
*/ | ||
function formatList(ObjectList, options) { | ||
const items = []; | ||
for (let index = 0; index < ObjectList.length; index += 1) { | ||
const element = ObjectList[index]; | ||
let html = formatToHtml(element); | ||
let html = formatToHtml(element, options); | ||
@@ -72,4 +100,9 @@ if (element && element.type.includes('list')) { | ||
function toHTMLPage(ObjectList) { | ||
const elementsString = formatList(ObjectList).join(SEPERATOR); | ||
/** | ||
* Creates a HTML Page out of a List of Notion-Objects | ||
* @param {*} ObjectList List of Notion-Objects | ||
* @param {*} options Options for parsing | ||
*/ | ||
function toHTMLPage(ObjectList, options) { | ||
const elementsString = formatList(ObjectList, options).join(SEPERATOR); | ||
return elementsString | ||
@@ -76,0 +109,0 @@ ? `<div> |
@@ -5,3 +5,10 @@ const fetch = require('./lib/fetch'); | ||
class Notion { | ||
constructor({ userId, token }) { | ||
constructor({ | ||
userId, | ||
token, | ||
options = { | ||
colors: {}, | ||
pageUrl: '/page?id=', | ||
}, | ||
}) { | ||
this.creds = { | ||
@@ -11,2 +18,3 @@ userId, | ||
}; | ||
this.options = options; | ||
} | ||
@@ -33,3 +41,3 @@ | ||
}); | ||
return makeHTML(values); | ||
return makeHTML(values, this.options); | ||
}); | ||
@@ -42,3 +50,5 @@ } | ||
Promise.all(pageIds.map(id => this.getPageById(id))) | ||
.then(a => a.map(element => pages.push(makeHTML(element)))) | ||
.then(a => | ||
a.map(element => pages.push(makeHTML(element, this.options))) | ||
) | ||
.then(() => pages) | ||
@@ -45,0 +55,0 @@ ); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
9387
178
167