Skip to content

Commit 5551466

Browse files
authored
Add prism (#2)
* Add prism * Add delayed language loading * Check for language support * Add prism note * Update example * Fix link
1 parent 1bd9dfb commit 5551466

File tree

13 files changed

+176
-38
lines changed

13 files changed

+176
-38
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ This package doesn't handle the communication with the API. Check out [notion-ap
4747

4848
🎨 **Custom Styles** – Styles are easily adaptable. Optional styles included
4949

50+
🔮 **Syntax-Highlighting** – Beautiful themeable code highlighting using Prism.js
51+
5052
🌎 **SSR / Static Generation Support** – Functions to work with Nuxt and other frameworks
5153

5254
## Install
@@ -61,7 +63,7 @@ npm install vue-notion
6163

6264
> Check out a demo at [vue-notion.now.sh](https://vue-notion.now.sh/)
6365
64-
The full `NotionRenderer` specification and more information on the Notion API and integration with Nuxt can be found at [`docs/`](https://github.com/janniks/vue-notion/tree/main/docs).
66+
More information on the `NotionRenderer` specification, syntax-highlighting, the Notion API, and integration with Nuxt can be found at [`docs/`](https://github.com/janniks/vue-notion/tree/main/docs).
6567

6668
### Basic Example
6769

@@ -154,4 +156,4 @@ Please, feel free to [open an issue](https://github.com/janniks/vue-notion/issue
154156

155157
## License ⚖️
156158

157-
MIT © [Jannik Siebert](https://twitter.com/jnnksbrt)
159+
MIT © [Jannik Siebert](https://twitter.com/jnnksbrt)

dev/serve.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
<template>
22
<div id="app">
3-
<notion-renderer :blockMap="blockMap" fullPage todo />
3+
<NotionRenderer :blockMap="blockMap" fullPage prism todo />
44
</div>
55
</template>
66

77
<script>
88
import { NotionRenderer, getPageBlocks } from "@/entry";
99
10+
import "prismjs";
11+
import "prismjs/themes/prism.css";
12+
1013
export default {
1114
name: "ServeDev",
1215
components: {

docs/README.md

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,36 @@ The `NotionRenderer` component offers a few properties
1212
- [`mapPageUrl`](#mapPageUrl) – default: `defaultMapPageUrl()`
1313
- [`pageLinkOptions`](#pageLinkOptions) – default: `undefined`
1414

15-
### `blockMap`
15+
### `blockMap`: Object
1616

1717
– the blocks part of a Notion API response.
1818
A list of blocks by their id that may contain contents and properties.
1919

20-
### `contentId`
20+
### `contentId`: String
2121

2222
– the id of the block that should be rendered.
2323
If this is `undefined` the _first_ block is rendered.
2424
_Usually the first block contains the rest of the page._
2525

26-
### `fullPage`
26+
### `fullPage`: Boolean
2727

2828
– wether or not the page should contain the cover and header.
2929

30-
### `hideList`
30+
### `hideList`: [String]
3131

3232
– a list of Notion blocks (e.g. `"callout"`) that should not be rendered.
3333

34-
### `mapImageUrl`
34+
### `mapImageUrl`: Function
3535

36-
– a method that receives `(imageUrl: String, block: Object)` and returns a `url: String` that should be used during rendering.
37-
The default method resolves images as they are used in Notion.
36+
– a function that receives `(imageUrl: String, block: Object)` and returns a `url: String` that should be used during rendering.
37+
The default function resolves images as they are used in Notion.
3838

39-
> This method could be used to upload the Notion hosted images to an own provider and replace the image URLs with new ones.
39+
> This function could be used to upload the Notion hosted images to an own provider and replace the image URLs with new ones.
4040
41-
### `mapPageUrl`
41+
### `mapPageUrl`: Function
4242

43-
– a method that receives `(pageId: String)` and returns a `url: String` that should be used during rendering.
44-
The default method resolves pageIds relative to the current page root – i.e., `<pageId>` will become `/<pageId>`
43+
– a function that receives `(pageId: String)` and returns a `url: String` that should be used during rendering.
44+
The default function resolves pageIds relative to the current page root – i.e., `<pageId>` will become `/<pageId>`
4545

4646
e.g. if you're using vue-notion for blog posts at `/posts/<pageId>`
4747

@@ -52,7 +52,7 @@ mapPageUrl(pageId = "") {
5252
}
5353
```
5454

55-
### `pageLinkOptions`
55+
### `pageLinkOptions`: Object
5656

5757
– are used to override links to other Notion pages with custom Vue components.
5858
`pageLinkOptions` is an `Object` that requires a `component` and a `href` parameter.
@@ -66,6 +66,36 @@ pageLinkOptions: {
6666
}
6767
```
6868

69+
### `prism`: Boolean
70+
71+
– whether or not syntax-highlighting using Prims.js should be activated.
72+
73+
> Check the `docs#syntax-highlighting` section below for more details.
74+
75+
## Syntax-highlighting
76+
77+
The following steps are required to add syntax-highlighting using Prism.js
78+
79+
- Install `prismjs` to your project – `npm install prismjs`
80+
- Import the Prism.js css and a theme css somewhere in your application.
81+
82+
```js
83+
import "prismjs";
84+
import "prismjs/themes/prism.css";
85+
```
86+
87+
- Add the `prism` flag to the `NotionRenderer`
88+
89+
```diff
90+
-<NotionRenderer :blockMap="blockMap" />
91+
+<NotionRenderer :blockMap="blockMap" prism />
92+
```
93+
94+
> A list of available themes can be found at [prism/themes](https://github.com/PrismJS/prism/tree/master/themes) and more installable themes can be found at [prism-themes](https://github.com/PrismJS/prism-themes).
95+
96+
> ⚠️ To keep file size down, Prism.js only includes `markup`, `css`, `clike`, and `javascript` languages per default.
97+
> To add supported languages import the language component from Prism.js – e.g. `import 'prismjs/components/prism-rust'` for `rust`.
98+
6999
## Notion API
70100

71101
The official Notion API is currently in [private beta](https://www.notion.so/api-beta).

example/layouts/default.vue

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ html {
5959
margin: 0 auto;
6060
min-height: 100vh;
6161
max-width: 90%;
62-
padding: 4em;
62+
padding: 4em 0.5em;
6363
display: block;
6464
text-align: center;
6565
}
@@ -77,25 +77,25 @@ html {
7777
padding: 1em 0 1.5em 0;
7878
}
7979
80-
.tags {
80+
.posts {
8181
margin: 1em;
8282
text-align: left;
8383
}
8484
85-
h2 {
85+
.container h2 {
8686
margin-top: 1.5em;
8787
}
8888
89-
ul {
89+
.container ul {
9090
margin: 1em auto;
9191
list-style: none;
9292
}
9393
94-
p {
94+
.container p {
9595
margin: 0.75em 0;
9696
}
9797
98-
a {
98+
.container a {
9999
max-width: 75%;
100100
white-space: nowrap;
101101
overflow: hidden;

example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"dependencies": {
1212
"core-js": "^3.6.5",
1313
"nuxt": "^2.14.12",
14+
"prismjs": "^1.22.0",
1415
"vue-notion": "^0.2.14"
1516
},
1617
"devDependencies": {}

example/pages/_slug.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
:blockMap="blockMap"
44
:pageLinkOptions="pageLinkOptions"
55
fullPage
6+
prism
67
/>
78
</template>
89

910
<script>
1011
import { NotionRenderer, getPageBlocks, getPageTable } from "vue-notion";
1112
13+
import "prismjs";
14+
import "prismjs/themes/prism.css";
15+
1216
export default {
1317
components: { NotionRenderer },
1418
data() {

example/pages/index.vue

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@
4444
<div class="links">
4545
<p>Blog-style Examples (use Notion as a CMS for Nuxt)</p>
4646
<div class="links">
47-
<NuxtLink class="button--green" to="/tags">
48-
Posts sorted by database tags
49-
</NuxtLink>
47+
<NuxtLink class="button--green" to="/posts">All Blog Posts</NuxtLink>
5048
<NuxtLink class="button--green" to="/example">
5149
Post via slug <i>(e.g. /example)</i>
5250
</NuxtLink>

example/pages/tags.vue renamed to example/pages/posts.vue

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,28 @@
22
<div class="container">
33
<div>
44
<h1 class="title">vue-notion-example</h1>
5-
<div class="tags">
6-
<h2>Posts sorted by Tags</h2>
5+
<div class="posts">
6+
<h2>All Posts</h2>
7+
<ul>
8+
<li v-for="(post, k) in posts" :key="k">
9+
<b>{{ post.date }}</b>
10+
<NuxtLink v-if="post.slug" :to="post.slug" class="button--grey">
11+
<b>{{ post.title }}</b>
12+
{{ post.preview }}
13+
</NuxtLink>
14+
</li>
15+
</ul>
16+
</div>
17+
<div class="posts">
18+
<h2>All Tags</h2>
719
<ul>
820
<li v-for="(tag, k) in tags" :key="k">
921
<b>{{ tag }}</b>
1022
<ul>
11-
<li v-for="(page, k) in pagesByTag.get(tag)" :key="k">
12-
<NuxtLink v-if="page.slug" :to="page.slug" class="button--grey">
13-
<b>{{ page.title }}</b>
14-
{{ page.preview }}
23+
<li v-for="(post, k) in postsByTag.get(tag)" :key="k">
24+
<NuxtLink v-if="post.slug" :to="post.slug" class="button--grey">
25+
<b>{{ post.title }}</b>
26+
{{ post.preview }}
1527
</NuxtLink>
1628
</li>
1729
</ul>
@@ -29,8 +41,13 @@ export default {
2941
async asyncData({ params, error }) {
3042
const pageTable = await getPageTable("10327f9074b7433aad577ccd0020e971");
3143
44+
// sort published pages
45+
const posts = pageTable
46+
.filter((page) => page.published)
47+
.sort((a, b) => a.date - b.date);
48+
3249
// convert array of pages to a map of tags with page arrays
33-
const pagesByTag = pageTable
50+
const postsByTag = pageTable
3451
.filter((page) => page.published)
3552
.reduce((map, currentPage) => {
3653
currentPage.tags.forEach((tag) =>
@@ -41,7 +58,11 @@ export default {
4158
return map;
4259
}, new Map());
4360
44-
return { tags: Array.from(pagesByTag.keys()).sort(), pagesByTag };
61+
return {
62+
posts,
63+
postsByTag,
64+
tags: [...postsByTag.keys()].sort(),
65+
};
4566
},
4667
};
4768
</script>

package-lock.json

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)