Skip to content

Commit 7f6ea4c

Browse files
authored
Merge pull request #55 from janniks/add-custom-targets
Add custom link targets
2 parents bd20544 + c778e90 commit 7f6ea4c

File tree

5 files changed

+54
-20
lines changed

5 files changed

+54
-20
lines changed

docs/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ The `NotionRenderer` component offers a few properties
1515

1616
- [`blockMap`](#blockMap) – required
1717
- [`contentId`](#contentId) – default: `undefined`
18-
- [`embedAllow`](#embedAllow) – default: `fullscreen`
18+
- [`embedAllow`](#embedAllow) – default: `"fullscreen"`
1919
- [`fullPage`](#fullPage) – default: `false`
2020
- [`hideList`](#hideList) – default: `[]`
2121
- [`mapImageUrl`](#mapImageUrl) – default: `defaultMapImageUrl()`
2222
- [`mapPageUrl`](#mapPageUrl) – default: `defaultMapPageUrl()`
2323
- [`pageLinkOptions`](#pageLinkOptions) – default: `undefined`
24+
- [`pageLinkTarget`](#pageLinkTarget) – default: `"_self"`
25+
- [`prism`](#prism) – default: `false`
26+
- [`textLinkTarget`](#textLinkTarget) – default: `"_blank"`
2427

2528
### `blockMap`: Object
2629

@@ -81,12 +84,20 @@ pageLinkOptions: {
8184
}
8285
```
8386

87+
### `pageLinkTarget`: String
88+
89+
– the [target attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-target) of links referencing other pages (skipped for pages with `pageLinkeOptions`)
90+
8491
### `prism`: Boolean
8592

8693
– whether or not syntax-highlighting using Prims.js should be activated.
8794

8895
> Check the `docs#syntax-highlighting` section below for more details.
8996
97+
### `textLinkTarget`: String
98+
99+
– the [target attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-target) of links
100+
90101
## Syntax-Highlighting
91102

92103
The following steps are required to add syntax-highlighting using Prism.js

src/blocks/decorator.vue

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
<template>
2+
<component
3+
v-if="isPageLink && hasPageLinkOptions"
4+
class="notion-page-link"
5+
v-bind="pageLinkProps(decoratorValue)"
6+
:is="pageLinkOptions.component"
7+
>
8+
{{ pageLinkTitle }}
9+
</component>
210
<a
3-
v-if="isPageLink"
11+
v-else-if="isPageLink"
412
class="notion-link"
5-
target="_blank"
6-
:href="decoratorValue"
7-
>{{ pageLinkTitle }}
8-
</a>
13+
:target="pageLinkTarget"
14+
:href="mapPageUrl(decoratorValue)"
15+
>{{ pageLinkTitle }}</a
16+
>
917
<span v-else-if="decorators.length === 0">{{ text }}</span>
1018
<span v-else-if="decoratorKey === 'h'" :class="'notion-' + decoratorValue"
1119
><NotionDecorator :content="nextContent" v-bind="pass" />
@@ -25,7 +33,7 @@
2533
<a
2634
v-else-if="decoratorKey === 'a'"
2735
class="notion-link"
28-
target="_blank"
36+
:target="target"
2937
:href="decoratorValue"
3038
>
3139
<NotionDecorator :content="nextContent" v-bind="pass" />
@@ -74,9 +82,15 @@ export default {
7482
pageLinkTitle() {
7583
return (
7684
this.blockMap?.[this.decoratorValue]?.value?.properties
77-
?.title?.[0]?.[0] || "this"
85+
?.title?.[0]?.[0] || "link"
7886
);
7987
},
88+
target() {
89+
if (this.type === "page") {
90+
return this.pageLinkTarget;
91+
}
92+
return this.textLinkTarget;
93+
},
8094
},
8195
};
8296
</script>

src/blocks/page.vue

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<component
3030
v-else-if="hasPageLinkOptions"
3131
class="notion-page-link"
32-
v-bind="pageLinkProps"
32+
v-bind="pageLinkProps(value.id)"
3333
:is="pageLinkOptions.component"
3434
>
3535
<div class="notion-page-icon">
@@ -39,7 +39,12 @@
3939
<NotionTextRenderer :text="title" v-bind="pass" />
4040
</div>
4141
</component>
42-
<a v-else class="notion-page-link" :href="mapPageUrl(value.id)">
42+
<a
43+
v-else
44+
class="notion-page-link"
45+
:target="pageLinkTarget"
46+
:href="mapPageUrl(value.id)"
47+
>
4348
<div class="notion-page-icon">
4449
<NotionPageIcon v-bind="pass" />
4550
</div>
@@ -66,12 +71,6 @@ export default {
6671
(1 - (this.format.page_cover_position || 0.5)) * 100;
6772
return { objectPosition: `center ${coverPosition}%` };
6873
},
69-
hasPageLinkOptions() {
70-
return this.pageLinkOptions?.component && this.pageLinkOptions?.href;
71-
},
72-
pageLinkProps() {
73-
return { [this.pageLinkOptions.href]: this.mapPageUrl(this.value.id) };
74-
},
7574
},
7675
};
7776
</script>

src/blocks/text.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<template>
2-
<p v-if="properties" :class="['notion-text', blockColorClass]">
2+
<p v-if="properties" :class="['notion-text', blockColorClass()]">
33
<NotionTextRenderer :text="title" v-bind="pass" />
44
</p>
55
<div v-else class="notion-blank">&nbsp;</div>
66
</template>
77

88
<script>
9-
import Blockable, { blockComputed } from "@/lib/blockable";
9+
import Blockable from "@/lib/blockable";
1010
import NotionTextRenderer from "@/blocks/helpers/text-renderer";
1111
1212
export default {

src/lib/blockable.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ export const blockProps = {
1010
mapImageUrl: Function,
1111
mapPageUrl: Function,
1212
pageLinkOptions: Object,
13+
pageLinkTarget: { type: String, default: "_self" },
1314
prism: { type: Boolean, default: false },
15+
textLinkTarget: { type: String, default: "_blank" },
1416
todo: { type: Boolean, default: false },
1517
};
1618

@@ -77,6 +79,9 @@ export const blockComputed = {
7779
visible() {
7880
return !this.hideList.includes(this.type);
7981
},
82+
hasPageLinkOptions() {
83+
return this.pageLinkOptions?.component && this.pageLinkOptions?.href;
84+
},
8085
};
8186

8287
export default {
@@ -86,14 +91,19 @@ export default {
8691
getTextContent,
8792
isType(t) {
8893
if (Array.isArray(t)) {
89-
return t.includes(this.type) && this.visible;
94+
return this.visible && t.includes(this.type);
9095
}
9196

92-
return this.type === t && this.visible;
97+
return this.visible && this.type === t;
9398
},
9499
blockColorClass(suffix = "") {
95100
const blockColor = this.format?.block_color;
96101
return blockColor ? `notion-${blockColor}${suffix}` : undefined;
97102
},
103+
pageLinkProps(id) {
104+
return {
105+
[this.pageLinkOptions.href]: this.mapPageUrl(id),
106+
};
107+
},
98108
},
99109
};

0 commit comments

Comments
 (0)