Skip to content

Commit ada8381

Browse files
committed
Add custom link targets
1 parent b8e0773 commit ada8381

File tree

5 files changed

+52
-18
lines changed

5 files changed

+52
-18
lines changed

docs/README.md

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

1515
- [`blockMap`](#blockMap) – required
1616
- [`contentId`](#contentId) – default: `undefined`
17-
- [`embedAllow`](#embedAllow) – default: `fullscreen`
17+
- [`embedAllow`](#embedAllow) – default: `"fullscreen"`
1818
- [`fullPage`](#fullPage) – default: `false`
1919
- [`hideList`](#hideList) – default: `[]`
2020
- [`mapImageUrl`](#mapImageUrl) – default: `defaultMapImageUrl()`
2121
- [`mapPageUrl`](#mapPageUrl) – default: `defaultMapPageUrl()`
2222
- [`pageLinkOptions`](#pageLinkOptions) – default: `undefined`
23+
- [`pageLinkTarget`](#pageLinkTarget) – default: `"_self"`
24+
- [`prism`](#prism) – default: `false`
25+
- [`textLinkTarget`](#textLinkTarget) – default: `"_blank"`
2326

2427
### `blockMap`: Object
2528

@@ -80,12 +83,20 @@ pageLinkOptions: {
8083
}
8184
```
8285

86+
### `pageLinkTarget`: String
87+
88+
– 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`)
89+
8390
### `prism`: Boolean
8491

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

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

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

src/blocks/decorator.vue

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
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"
13+
:target="pageLinkTarget"
14+
:href="mapPageUrl(decoratorValue)"
715
>{{ pageLinkTitle }}</a
816
>
917
<span v-else-if="decorators.length === 0">{{ text }}</span>
@@ -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" />
@@ -69,9 +77,15 @@ export default {
6977
pageLinkTitle() {
7078
return (
7179
this.blockMap?.[this.decoratorValue]?.value?.properties
72-
?.title?.[0]?.[0] || "this"
80+
?.title?.[0]?.[0] || "link"
7381
);
7482
},
83+
target() {
84+
if (this.type === "page") {
85+
return this.pageLinkTarget;
86+
}
87+
return this.textLinkTarget;
88+
},
7589
},
7690
};
7791
</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)