|
1 | | -# create-svelte |
| 1 | +# Introduction |
2 | 2 |
|
3 | | -Everything you need to build a Svelte library, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/master/packages/create-svelte). |
| 3 | +This is a plug and play library for [`Sveltekit`](https://kit.svelte.dev/) projects to create blogs quickly in your website's subdirectory using [`Notion`](https://www.notion.so/) as a CMS. |
4 | 4 |
|
5 | | -Read more about creating a library [in the docs](https://kit.svelte.dev/docs/packaging). |
| 5 | +## Getting started |
6 | 6 |
|
7 | | -## Creating a project |
| 7 | +### Notion Setup |
| 8 | +1. Duplicate this [`Notion template`] into your workspace. |
| 9 | +2. Create an internal Notion connection from the settings [(Link)](https://www.notion.so/my-integrations), Notion's documentation on how to create an internal connection [(link)] (https://developers.notion.com/docs/create-a-notion-integration) |
| 10 | +3. Connect the newly created connection with the blogs template duplicated in step 1 by clicking on the 3 dots on top-right. Then click on **Add Connections** and search for the connection you created in the step 3. Done! |
| 11 | +4. New blogs will be added in the **Blogs** page of the template. |
8 | 12 |
|
9 | | -If you're seeing this, you've probably already done this step. Congrats! |
| 13 | +### Initialize the library in the root +layout.ts |
10 | 14 |
|
11 | 15 | ```bash |
12 | | -# create a new project in the current directory |
13 | | -npm create svelte@latest |
| 16 | + import { PUBLIC_NOTION_DATABASE_ID, PUBLIC_NOTION_TOKEN } from "$env/static/public"; |
| 17 | + import { initNotion } from "sveltekit-notion-blog"; |
14 | 18 |
|
15 | | -# create a new project in my-app |
16 | | -npm create svelte@latest my-app |
| 19 | + export const prerender = true; |
| 20 | + |
| 21 | + initNotion({ |
| 22 | + databaseId: PUBLIC_NOTION_DATABASE_ID, //from .env |
| 23 | + notionToken: PUBLIC_NOTION_TOKEN, //from .env |
| 24 | + }); |
17 | 25 | ``` |
18 | 26 |
|
19 | | -## Developing |
| 27 | +### Code Setup |
20 | 28 |
|
21 | | -Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server: |
| 29 | +1. Install the package |
22 | 30 |
|
23 | 31 | ```bash |
24 | | -npm run dev |
25 | | - |
26 | | -# or start the server and open the app in a new browser tab |
27 | | -npm run dev -- --open |
| 32 | + npm i sveltekit-notion-blog |
28 | 33 | ``` |
29 | 34 |
|
30 | | -Everything inside `src/lib` is part of your library, everything inside `src/routes` can be used as a showcase or preview app. |
31 | | - |
32 | | -## Building |
33 | | - |
34 | | -To build your library: |
| 35 | +2. In your existing project, create a subdirectory names **blog** and create ```+page.svelte``` and ```+page.server.ts``` files in this directory and call the _getAllPosts_ method in the ```+page.server.ts``` |
35 | 36 |
|
36 | 37 | ```bash |
37 | | -npm run package |
| 38 | + import type { PageServerLoad } from './$types'; |
| 39 | + import { getAllPosts } from "sveltekit-notion-blog"; |
| 40 | + export const load: PageServerLoad = () => getAllPosts(); |
38 | 41 | ``` |
39 | 42 |
|
40 | | -To create a production version of your showcase app: |
| 43 | +3. In the ```+page.svelte```, import the ```PostsList`` component |
41 | 44 |
|
42 | 45 | ```bash |
43 | | -npm run build |
| 46 | + <script lang="ts"> |
| 47 | + import type { PageData } from './$types'; |
| 48 | + import { PostsList } from "sveltekit-notion-blog"; |
| 49 | + export let data: PageData; |
| 50 | + </script> |
| 51 | +
|
| 52 | + <div class="max-w-4xl m-auto"> |
| 53 | + <PostsList {data} /> |
| 54 | + </div> |
44 | 55 | ``` |
45 | 56 |
|
46 | | -You can preview the production build with `npm run preview`. |
| 57 | +4. Create a new directory inside the blog directory named **[slug]** and create ```+page.svelte``` and ```+page.server.ts``` files in this directory. |
47 | 58 |
|
48 | | -> To deploy your app, you may need to install an [adapter](https://kit.svelte.dev/docs/adapters) for your target environment. |
| 59 | +5. In the ```+page.server.ts``` call the **getBlogPageBySlug** method |
49 | 60 |
|
50 | | -## Publishing |
| 61 | +```bash |
| 62 | + import type { ServerLoadEvent } from '@sveltejs/kit'; |
| 63 | + import { getBlogPageBySlug } from 'sveltekit-notion-blog'; |
51 | 64 |
|
52 | | -Go into the `package.json` and give your package the desired name through the `"name"` option. Also consider adding a `"license"` field and point it to a `LICENSE` file which you can create from a template (one popular option is the [MIT license](https://opensource.org/license/mit/)). |
| 65 | + export const load = (event: ServerLoadEvent) => getBlogPageBySlug(event); |
| 66 | +``` |
53 | 67 |
|
54 | | -To publish your library to [npm](https://www.npmjs.com): |
| 68 | +6. In the ```+page.svelte``` |
55 | 69 |
|
56 | 70 | ```bash |
57 | | -npm publish |
| 71 | +<script lang="ts"> |
| 72 | + import { BlogPost } from 'sveltekit-notion-blog'; |
| 73 | + import type { PageData } from './$types'; |
| 74 | + |
| 75 | + export let data: PageData; |
| 76 | +</script> |
| 77 | +
|
| 78 | +<BlogPost {data} /> |
58 | 79 | ``` |
0 commit comments