Giter Site home page Giter Site logo

notionblog / vue-notion-render Goto Github PK

View Code? Open in Web Editor NEW
27.0 1.0 5.0 2.29 MB

πŸ“œ Vue.js renderer for Notion pages

Home Page: https://vue.mynotion.blog

License: MIT License

JavaScript 43.58% Vue 56.42%
notion vuejs nuxtjs vue-notion-render notion-blog notion-api

vue-notion-render's Introduction

vue-notion-render

πŸ“œ Vue Renderer for Notion pages.

vue-notion-render

NPM

Npm

Features

Install

npm install vue-notion-render --save

Vue.js

in src/index.js file import the component and use Vue.component method.

    import Vue from "vue"
    import VueNotionRender from "vue-notion-render"
    Vue.component('VueNotionRender', VueNotionRender)

Nuxt.js

in order to use it with Nuxt.js create a Nuxt plugin.

  1. in /plugins directory create a file vue-notion-render.js

    import VueNotionRender from 'vue-notion-render'
    import Vue from 'vue'
    Vue.component('VueNotionRender', VueNotionRender)
  2. Register the plugin by adding the file in nuxt.config.js plugins array

        plugins: ['~/plugins/vue-notion-render.js']

Local Component

if you don't want to register the component globally you can use it locally in your component.

    <script>
         import VueNotionRender from "vue-notion-render"
         ...
         export default {
                 components: {
                     VueNotionRender,
                 },
         ...
     </script>

Usage

This package doesn't handle the communication with the API, therefore to learn more about the official API check out the official documentation.

Currently the Official API doesn't support all Notion blocks, a temporary solution is to use notion-api-worker a serverless wrapper for the private Notion API.

Using notion-api-worker

  • Use the /v1/page/:id to retrieve page blocks
  • Make sure to pass :unofficial="true" prop
<template>
  <vue-notion-render :unofficial="true" :data="data" />
</template>

<script>
...js
export default {
    ...
    data() {
        return {
        data: {}
        };
    },
    async created() {
        try {
            const data = await fetch("https://beta.notiontoblog.workers.dev/v1/page/f36a63918d1246909206813dcec928ea")
            this.data = await data.json()
        }catch(err){
            console.log(err)
        }
    }
};
</script>

Using The official API

https://developers.notion.com/reference/get-block-children

  • Retrieve block children and pass the results array to data
  • Make sure to pass :unofficial="false" prop
<template>
  <vue-notion-render :unofficial="false" :data="data" />
</template>

<script>
...
export default {
    ...
    data() {
        return {
            data: []
        };
    },
    async created() {
        // Retrieve block children and pass the results array to data
        // https://developers.notion.com/reference/get-block-children
    }
};
</script>

Supported Blocks

Block Type Supported Notion-API-Worker Official API Note
paragraph βœ… βœ… βœ…
Headings βœ… βœ… βœ…
To-do βœ… βœ… βœ…
Lists βœ… βœ… βœ…
Toggle βœ… βœ… βœ…
Quote βœ… βœ… ❌
Colors βœ… βœ… βœ…
Highlight βœ… βœ… βœ…
Divider βœ… βœ… ❌
Callout βœ… βœ… ❌
Emojis βœ… βœ… βœ…
Inline Equation βœ… βœ… βœ…
Block Equation βœ… βœ… ❌
Code βœ… βœ… ❌
Bookmark βœ… βœ… ❌
Embeds βœ… βœ… ❌ Spotify, Twitter, Maps, Figma, Pdf, Codepen, TypeForm, Replit, Youtube, Whimsical, Drive
Layout βœ… βœ… ❌
Video βœ… βœ… ❌ embed
Audio βœ… βœ… ❌ embed
File βœ… βœ… ❌ embed
Table ❌ ❌ ❌ soon
Board ❌ ❌ ❌ soon

Custom Blocks

vue-notion-render let you use customized components to render some blocks, to do that

1. Register Components globally

Vue.js in main.js file register the components

import Vue from "vue";
import Component1 from "../components/component1.vue";
import Component2 from "../components/component2.vue";

Vue.component("Component1", Component1);
Vue.component("Component2", Component2);

Nuxt.js

1.create a plugin in /plugins directory and name it components.js

import Vue from "vue";
import Component1 from "../components/component1.vue";
import Component2 from "../components/component2.vue";

Vue.component("Component1", Component1);
Vue.component("Component2", Component2);

2- Register the plugin by adding the file inΒ nuxt.config.jsΒ plugins array

plugins: ['~/plugins/components.js']

2. Create Customized block array

You need to create an array of objects that contains blockId and componentName like this

data() {
    return {
      customBlocks: [
        {
          blockId: "fe30b1ce-84a7-47b3-abe1-b3448b989115",
          componentName: "Component1"
        },
        {
          blockId: "305fed49-f7e8-412f-80a1-b32e2bbd5a01",
          componentName: "Component2"
        }
      ]
    };
  },

3. Pass the array to custom prop

You need to pass the customized block array to custom prop

<VueNotionRender :unofficial="true" :data="blocks" :custom="customBlocks" />

4. Accept block prop in your component

to have the block data you need to accept block prop in your component

<script>
export default {
  props: {
    block: Object,
  },
};
</script>

Credits

Created By @yudax

vue-notion-render's People

Contributors

yudax42 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

vue-notion-render's Issues

General way to override the spacing between blocks

Would be nice if there were some quick and easy way to do this so the component can flow more text like without being line after line. Maybe a prop spacing [block, prose'] etc... where normal text just flows and other block elements like list items remain normally as an idea, not sure how best to approach this but it would just be nice to at least control the block spacing.

Very useful library, thanks! Came in super handy as we use Notion as our wiki backend. πŸ™

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.