├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── composer.json ├── config └── notion-renderer.php ├── pint.json └── src ├── Blocks ├── Block.php ├── BulletList.php ├── Divider.php ├── Heading1.php ├── Heading2.php ├── Heading3.php ├── Image.php ├── Listing.php ├── NumberedList.php ├── Paragraph.php ├── Quote.php └── Table.php ├── Exceptions └── NotionException.php ├── NotionAPIService ├── NotionBlocks.php ├── NotionDatabase.php └── NotionPage.php ├── NotionRendererServiceProvider.php └── Renderers └── NotionRenderer.php /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **Add tests or relavent screenshots or recordings!** 44 | 45 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 46 | 47 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 48 | 49 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 50 | 51 | **Happy coding**! 52 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Usama Rehan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Generate HTML from Notion Page 2 | 3 | This package converts all the blocks in a Notion page into HTML using Notion's API. 4 | For more details on Notion API, please refer this page: https://developers.notion.com/ 5 | 6 | Currently, the Notion API returns the content in a form of JSON. 7 | You can see example for JSON format here: https://codebeautify.org/jsonviewer/y222f395f for this page: https://tested-wheel-e55.notion.site/Example-Page-6927be844d994e51bdda5f0c903f6788 8 | 9 | Our renderer converts the JSON and converts it into HTML (converting to Markdown is work in progress), which you can use to render in your web application. 10 | 11 | --- 12 | ## Installation 13 | 14 | You can install the package via composer: 15 | 16 | ```bash 17 | composer require rehankanak/laravel-notion-renderer 18 | ``` 19 | --- 20 | ## Usage 21 | 22 | ```php 23 | $pageHTML = (new NotionRenderer('yourPageId'))->html(); 24 | ``` 25 | 26 | That's it 🎉 27 | 28 | Your Notion Page is now converted to HTML and can use displayed in your web application. 29 | 30 | Recommendation: We suggest to use https://tailwindcss.com/docs/typography-plugin to style the HTML (or you may use your own CSS for the styling). 31 | 32 | Note: The NotionRenderer class extensively uses Notion's Block API (https://developers.notion.com/reference/get-block-children) to get 33 | the content of the Notion Page. Also, your API key must be present at `.env` file, check the `config` file of the package for more details. 34 | 35 | ------ 36 | ## Supported Block List: 37 | 38 | Only the below blocks are currently Supported, but more blocks are coming soon. 39 | - Paragraph 40 | - Heading One 41 | - Heading Two 42 | - Heading Three 43 | - Quote 44 | - Numbered List 45 | - Bulleted List 46 | - Image 47 | - Table 48 | - Divider 49 | 50 | Full list of blocks can be found here: https://developers.notion.com/reference/block 51 | 52 | Note: Other blocks are under development 53 | 54 | ----- 55 | ## Service Classes (Optional to Use) 56 | 57 | The package contains the following service classes: 58 | 59 | ### NotionBlocks 60 | - To retrieve a Block object using the ID specified. (https://developers.notion.com/reference/get-block-children) 61 | #### Usage 62 | ```php 63 | $notionPageAsBlocks = (new NotionBlocks())->fetch('yourPageId') 64 | ``` 65 | ### NotionDatabase 66 | - To retrieve a list of Pages contained in the Notion database (https://developers.notion.com/reference/post-database-query) 67 | #### Usage 68 | ```php 69 | $notionDatabase = (new NotionDatabase())->fetch('yourDatabaseId') 70 | ``` 71 | ### NotionPage 72 | - To retrieve a Page object using the Notion ID specified. (https://developers.notion.com/reference/retrieve-a-page) 73 | #### Usage 74 | ```php 75 | $notionPage = (new NotionDatabase())->fetch('yourPageId') 76 | ``` 77 | --- 78 | ### Note 79 | For this to work, you need to create Integration and share the page with the Integration. 80 | For more details on obtaining and creating Integrations, please refer this page: https://www.notion.so/my-integrations 81 | 82 | ## Credits 83 | 84 | - [zusamarehan](https://github.com/zusamarehan) 85 | - [All Contributors](../../contributors) 86 | 87 | ## License 88 | 89 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 90 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rehankanak/laravel-notion-renderer", 3 | "description": "Laravel Package for Converting Notion Pages to Web Pages", 4 | "keywords": [ 5 | "rehankanak", 6 | "laravel-renderer", 7 | "notion", 8 | "notion-renderer", 9 | "notion-laravel-renderer" 10 | ], 11 | "homepage": "https://github.com/zusamarehan/laravel-notion-renderer", 12 | "license": "MIT", 13 | "authors": [ 14 | { 15 | "name": "KU Rehan", 16 | "email": "zrehan286@gmail.com", 17 | "homepage": "https://github.com/zusamarehan/laravel-notion-renderer", 18 | "role": "Developer" 19 | } 20 | ], 21 | "require": { 22 | "php": "^8.1" 23 | }, 24 | "require-dev": { 25 | "laravel/pint": "^1.0" 26 | }, 27 | "autoload": { 28 | "psr-4": { 29 | "RehanKanak\\LaravelNotionRenderer\\": "src" 30 | } 31 | }, 32 | "autoload-dev": { 33 | "psr-4": { 34 | "RehanKanak\\LaravelNotionRenderer\\": "src" 35 | } 36 | }, 37 | "scripts": { 38 | "format": "vendor/bin/pint" 39 | }, 40 | "config": { 41 | "sort-packages": true 42 | }, 43 | "extra": { 44 | "laravel": { 45 | "providers": [ 46 | "RehanKanak\\LaravelNotionRenderer\\NotionRendererServiceProvider" 47 | ] 48 | } 49 | }, 50 | "minimum-stability": "dev", 51 | "prefer-stable": true, 52 | "funding": [] 53 | } 54 | -------------------------------------------------------------------------------- /config/notion-renderer.php: -------------------------------------------------------------------------------- 1 | env('NOTION_API'), 15 | 16 | /* 17 | |-------------------------------------------------------------------------- 18 | | Notion API Version 19 | |-------------------------------------------------------------------------- 20 | | 21 | | This value allows you to set the Notion API version to be used 22 | | while making the API calls. You can find more details on versioning 23 | | of Notion here: https://developers.notion.com/reference/versioning 24 | */ 25 | 'NOTION_API_VERSION' => env('NOTION_API_VERSION', '2022-02-22'), 26 | ]; 27 | -------------------------------------------------------------------------------- /pint.json: -------------------------------------------------------------------------------- 1 | { 2 | "preset": "laravel" 3 | } -------------------------------------------------------------------------------- /src/Blocks/Block.php: -------------------------------------------------------------------------------- 1 | block = $block; 18 | $this->previousBlock = $previousBlock; 19 | $this->result = ''; 20 | 21 | if (in_array($block['type'], NotionRenderer::BLOCKS_WITHOUT_LIST) && ($this->previousBlock && $this->previousBlock['type'] === 'numbered_list_item')) { 22 | $this->result .= NumberedList::end(); 23 | } 24 | 25 | if (in_array($block['type'], NotionRenderer::BLOCKS_WITHOUT_LIST) && ($this->previousBlock && $this->previousBlock['type'] === 'bulleted_list_item')) { 26 | $this->result .= BulletList::end(); 27 | } 28 | 29 | if ($block['type'] === 'numbered_list_item' && ($this->previousBlock && $this->previousBlock['type'] !== 'numbered_list_item')) { 30 | if ($this->previousBlock['type'] === 'bulleted_list_item') { 31 | $this->result .= BulletList::end(); 32 | } 33 | 34 | $this->result .= NumberedList::start(); 35 | } 36 | 37 | if ($block['type'] === 'bulleted_list_item' && ($this->previousBlock && $this->previousBlock['type'] !== 'bulleted_list_item')) { 38 | if ($this->previousBlock['type'] === 'numbered_list_item') { 39 | $this->result .= NumberedList::end(); 40 | } 41 | 42 | $this->result .= BulletList::start(); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Blocks/BulletList.php: -------------------------------------------------------------------------------- 1 | '; 10 | } 11 | 12 | public static function end(): string 13 | { 14 | return ''; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/Blocks/Divider.php: -------------------------------------------------------------------------------- 1 | result .= '
'; 10 | 11 | return $this; 12 | } 13 | 14 | public function render(): string 15 | { 16 | $this->previousBlock = $this->block; 17 | 18 | return $this->result; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Blocks/Heading1.php: -------------------------------------------------------------------------------- 1 | result .= '

'; 10 | } 11 | 12 | private function end(): void 13 | { 14 | $this->result .= '

'; 15 | } 16 | 17 | public function process(): Heading1 18 | { 19 | Heading1::start(); 20 | 21 | foreach ($this->block['heading_1']['rich_text'] as $content) { 22 | if ($content['text']['link'] !== null) { 23 | $this->result .= " 28 |
".$this->caption.'
29 | '; 30 | } 31 | 32 | if ($this->block['image']['type'] === 'file') { 33 | $this->result .= 34 | '
35 | Image 36 |
".$this->caption.'
37 |
'; 38 | } 39 | 40 | return $this; 41 | } 42 | 43 | public function render(): string 44 | { 45 | $this->previousBlock = $this->block; 46 | 47 | return $this->result; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/Blocks/Listing.php: -------------------------------------------------------------------------------- 1 | result .= '
  • '; 10 | } 11 | 12 | public function process(): Listing 13 | { 14 | Listing::start(); 15 | 16 | foreach ($this->block[$this->block['type']]['rich_text'] as $content) { 17 | if ($content['text']['link'] !== null) { 18 | $this->result .= "request('GET', $url, [ 24 | 'headers' => [ 25 | 'Accept' => 'application/json', 26 | 'Content-Type' => 'application/json', 27 | 'Notion-Version' => config('notion-renderer.NOTION_API_VERSION'), 28 | 'Authorization' => 'Bearer '.config('notion-renderer.NOTION_API'), 29 | ], 30 | ]); 31 | 32 | if ($blocks->getStatusCode() == 200) { 33 | return [ 34 | 'success' => true, 35 | 'data' => json_decode($blocks->getBody(), true)['results'], 36 | ]; 37 | } 38 | 39 | return [ 40 | 'success' => false, 41 | 'details' => json_decode($blocks->getBody(), true), 42 | ]; 43 | } catch (GuzzleException $e) { 44 | throw new NotionException($e->getMessage(), $e->getCode()); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/NotionAPIService/NotionDatabase.php: -------------------------------------------------------------------------------- 1 | request('POST', $url, [ 22 | 'headers' => [ 23 | 'Accept' => 'application/json', 24 | 'Content-Type' => 'application/json', 25 | 'Notion-Version' => config('notion-renderer.NOTION_API_VERSION'), 26 | 'Authorization' => 'Bearer '.config('notion-renderer.NOTION_API'), 27 | ], 28 | ]); 29 | 30 | if ($database->getStatusCode() == 200) { 31 | return [ 32 | 'success' => true, 33 | 'data' => json_decode($database->getBody(), true), 34 | ]; 35 | } 36 | 37 | return [ 38 | 'success' => false, 39 | 'details' => json_decode($database->getBody(), true), 40 | ]; 41 | } catch (GuzzleException $e) { 42 | throw new NotionException($e->getMessage(), $e->getCode()); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/NotionAPIService/NotionPage.php: -------------------------------------------------------------------------------- 1 | request('GET', $url, [ 22 | 'headers' => [ 23 | 'Accept' => 'application/json', 24 | 'Content-Type' => 'application/json', 25 | 'Notion-Version' => config('notion-renderer.NOTION_API_VERSION'), 26 | 'Authorization' => 'Bearer '.config('notion-renderer.NOTION_API'), 27 | ], 28 | ]); 29 | 30 | if ($pages->getStatusCode() == 200) { 31 | return [ 32 | 'success' => true, 33 | 'data' => json_decode($pages->getBody(), true), 34 | ]; 35 | } 36 | 37 | return [ 38 | 'success' => false, 39 | 'details' => json_decode($pages->getBody(), true), 40 | ]; 41 | } catch (GuzzleException $e) { 42 | throw new NotionException($e->getMessage(), $e->getCode()); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/NotionRendererServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 12 | __DIR__.'/../config/notion-renderer.php' => config_path('notion-renderer.php'), 13 | ], 'config'); 14 | } 15 | 16 | public function register() 17 | { 18 | $this->mergeConfigFrom(__DIR__.'/../config/notion-renderer.php', 'notion-renderer'); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Renderers/NotionRenderer.php: -------------------------------------------------------------------------------- 1 | fetch($pageId); 54 | 55 | if (! $response['success']) { 56 | throw new NotionException($response['details']['message'], $response['details']['status']); 57 | } 58 | 59 | $this->blocks = $response['data']; 60 | $this->render(); 61 | } 62 | 63 | /** 64 | * @throws NotionException 65 | */ 66 | public function render(): NotionRenderer 67 | { 68 | foreach ($this->blocks as $block) { 69 | if (! in_array($block['type'], NotionRenderer::BLOCKS)) { 70 | continue; 71 | } 72 | 73 | if ($block['type'] === 'paragraph') { 74 | $this->results .= (new Paragraph($block, $this->previousBlock)) 75 | ->process() 76 | ->render(); 77 | } 78 | 79 | if ($block['type'] === 'heading_1') { 80 | $this->results .= (new Heading1($block, $this->previousBlock)) 81 | ->process() 82 | ->render(); 83 | } 84 | 85 | if ($block['type'] === 'heading_2') { 86 | $this->results .= (new Heading2($block, $this->previousBlock)) 87 | ->process() 88 | ->render(); 89 | } 90 | 91 | if ($block['type'] === 'heading_3') { 92 | $this->results .= (new Heading3($block, $this->previousBlock)) 93 | ->process() 94 | ->render(); 95 | } 96 | 97 | if ($block['type'] === 'quote') { 98 | $this->results .= (new Quote($block, $this->previousBlock)) 99 | ->process() 100 | ->render(); 101 | } 102 | 103 | if ($block['type'] === 'numbered_list_item') { 104 | $this->results .= (new Listing($block, $this->previousBlock)) 105 | ->process() 106 | ->render(); 107 | } 108 | 109 | if ($block['type'] === 'bulleted_list_item') { 110 | $this->results .= (new Listing($block, $this->previousBlock)) 111 | ->process() 112 | ->render(); 113 | } 114 | 115 | if ($block['type'] === 'image') { 116 | $this->results .= (new Image($block, $this->previousBlock)) 117 | ->caption() 118 | ->process() 119 | ->render(); 120 | } 121 | 122 | if ($block['type'] === 'table') { 123 | $this->results .= (new Table($block, $this->previousBlock)) 124 | ->children() 125 | ->process() 126 | ->render(); 127 | } 128 | 129 | if ($block['type'] === 'divider') { 130 | $this->results .= (new Divider($block, $this->previousBlock)) 131 | ->process() 132 | ->render(); 133 | } 134 | 135 | $this->previousBlock = $block; 136 | } 137 | 138 | return $this; 139 | } 140 | 141 | public function html() 142 | { 143 | return $this->results; 144 | } 145 | } 146 | --------------------------------------------------------------------------------