@riebeckite/plugin-hover-preview 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +79 -0
- package/README_ja.md +62 -0
- package/dist/client.d.ts +1 -0
- package/dist/client.js +173 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +369 -0
- package/dist/src/init.d.ts +1 -0
- package/dist/src/options.d.ts +6 -0
- package/dist/src/preview-index.d.ts +14 -0
- package/dist/src/render.d.ts +6 -0
- package/dist/src/runtime.d.ts +7 -0
- package/dist/src/types.d.ts +44 -0
- package/package.json +61 -0
- package/style.css +72 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright Rerurate_514
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# @riebeckite/plugin-hover-preview
|
|
2
|
+
|
|
3
|
+
Quartz/Obsidian-Publish style popover previews for internal links. Hovering or
|
|
4
|
+
focusing a note link shows its title and a short excerpt without leaving the
|
|
5
|
+
page.
|
|
6
|
+
|
|
7
|
+
[日本語](./README_ja.md)
|
|
8
|
+
|
|
9
|
+
## Overview
|
|
10
|
+
|
|
11
|
+
At build time `hoverPreviewPlugin()` builds a preview index from the content
|
|
12
|
+
manifest (`permalink` → `{ title, excerpt, slug }`) and injects it once into
|
|
13
|
+
every page that contains internal links as an inert
|
|
14
|
+
`<script type="application/json" data-rb-hover-preview>` block. The client
|
|
15
|
+
entry `initHoverPreview` reads that payload and attaches hover, focus, and touch
|
|
16
|
+
handlers to the matching links.
|
|
17
|
+
|
|
18
|
+
The excerpt is plain text extracted from the rendered HTML: tags are stripped,
|
|
19
|
+
whitespace is collapsed, and the result is truncated to `excerptLength`
|
|
20
|
+
characters. Pages without internal links are left untouched, and the payload is
|
|
21
|
+
bounded by `maxEntries` when set.
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { defineConfig } from "@riebeckite/core";
|
|
27
|
+
import { hoverPreviewPlugin } from "@riebeckite/plugin-hover-preview";
|
|
28
|
+
|
|
29
|
+
export default defineConfig({
|
|
30
|
+
// ...
|
|
31
|
+
plugins: [hoverPreviewPlugin()],
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
`hoverPreviewPlugin()` registers the style asset, the client entry, and the
|
|
36
|
+
build-time payload injection. `hoverPreview` is an alias of the same factory.
|
|
37
|
+
|
|
38
|
+
The client entry takes no arguments. Behavior is carried by data attributes on
|
|
39
|
+
the payload script, so the plugin works even when the client initializer is
|
|
40
|
+
called without options.
|
|
41
|
+
|
|
42
|
+
## Options
|
|
43
|
+
|
|
44
|
+
| Option | Default | Description |
|
|
45
|
+
| --------------- | -------------- | ------------------------------------------------------ |
|
|
46
|
+
| `delay` | `120` | Milliseconds before the popover appears. |
|
|
47
|
+
| `excerptLength` | `160` | Maximum excerpt length in characters. |
|
|
48
|
+
| `maxEntries` | unset | Upper bound on entries stored in the page payload. |
|
|
49
|
+
| `selector` | `a[href^="/"]` | Selector for internal links that receive a preview. |
|
|
50
|
+
| `className` | `rb-hover-preview` | Base class of the popover element. |
|
|
51
|
+
| `includeTitles` | `true` | Whether the popover shows the target entry title. |
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
hoverPreviewPlugin({
|
|
55
|
+
delay: 200,
|
|
56
|
+
excerptLength: 120,
|
|
57
|
+
maxEntries: 200,
|
|
58
|
+
selector: 'a[href^="/notes/"]',
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## API
|
|
63
|
+
|
|
64
|
+
- `hoverPreviewPlugin(options?)` — plugin factory
|
|
65
|
+
- `hoverPreview` — alias of `hoverPreviewPlugin`
|
|
66
|
+
- `resolveHoverPreviewOptions(options?)` — applies defaults and returns a
|
|
67
|
+
`ResolvedHoverPreviewOptions`
|
|
68
|
+
- `buildPreviewIndex(entries, options)` — builds a `HoverPreviewIndex`
|
|
69
|
+
(`permalink` → `{ title, excerpt, slug }`)
|
|
70
|
+
- `htmlToPlainText(html)` / `createExcerpt(html, length)` — excerpt helpers
|
|
71
|
+
- `initHoverPreview()` — browser initializer (also via
|
|
72
|
+
`@riebeckite/plugin-hover-preview/client`)
|
|
73
|
+
- Constants: `HOVER_PREVIEW_ATTRIBUTE`, `HOVER_PREVIEW_SCRIPT_ID`
|
|
74
|
+
- Types: `HoverPreviewOptions`, `ResolvedHoverPreviewOptions`,
|
|
75
|
+
`HoverPreviewEntry`, `HoverPreviewIndex`
|
|
76
|
+
|
|
77
|
+
## See also
|
|
78
|
+
|
|
79
|
+
- [Plugin guide](../../docs/plugins_en.md)
|
package/README_ja.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# @riebeckite/plugin-hover-preview
|
|
2
|
+
|
|
3
|
+
内部リンクにカーソルを合わせると、リンク先のタイトルと抜粋をポップオーバーで表示するプラグインです。Quartz や Obsidian Publish のプレビューに近い挙動で、ページを離れずにリンク先の内容を確認できます。
|
|
4
|
+
|
|
5
|
+
[English](./README.md)
|
|
6
|
+
|
|
7
|
+
## 概要
|
|
8
|
+
|
|
9
|
+
ビルド時に `hoverPreviewPlugin()` がコンテンツマニフェストからプレビュー索引(`permalink` → `{ title, excerpt, slug }`)を作り、内部リンクを含むページへ一度だけ `<script type="application/json" data-rb-hover-preview>` として埋め込みます。クライアント側の `initHoverPreview` がこのデータを読み、対象リンクにホバー・フォーカス・タップの処理を付けます。
|
|
10
|
+
|
|
11
|
+
抜粋は描画済み HTML からタグを除いたプレーンテキストで、空白をまとめたうえで `excerptLength` 文字に切り詰めます。内部リンクのないページには何も挿入しません。`maxEntries` を指定するとページごとのデータ量を抑えられます。
|
|
12
|
+
|
|
13
|
+
## 設定する
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { defineConfig } from "@riebeckite/core";
|
|
17
|
+
import { hoverPreviewPlugin } from "@riebeckite/plugin-hover-preview";
|
|
18
|
+
|
|
19
|
+
export default defineConfig({
|
|
20
|
+
// ...
|
|
21
|
+
plugins: [hoverPreviewPlugin()],
|
|
22
|
+
});
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
`hoverPreviewPlugin()` がスタイル、クライアントエントリ、ビルド時のデータ注入をまとめて登録します。`hoverPreview` は同じファクトリの別名です。
|
|
26
|
+
|
|
27
|
+
クライアントエントリは引数を取りません。表示に関する設定はデータ用スクリプトの data 属性に載るため、初期化処理をオプションなしで呼んでも動作します。
|
|
28
|
+
|
|
29
|
+
## オプション
|
|
30
|
+
|
|
31
|
+
| オプション | 既定値 | 説明 |
|
|
32
|
+
| --------------- | -------------- | ------------------------------------------ |
|
|
33
|
+
| `delay` | `120` | ポップオーバーが出るまでの待ち時間(ミリ秒) |
|
|
34
|
+
| `excerptLength` | `160` | 抜粋の最大文字数 |
|
|
35
|
+
| `maxEntries` | 未指定 | ページに埋め込む項目数の上限 |
|
|
36
|
+
| `selector` | `a[href^="/"]` | プレビュー対象にする内部リンクのセレクタ |
|
|
37
|
+
| `className` | `rb-hover-preview` | ポップオーバーの基準クラス名 |
|
|
38
|
+
| `includeTitles` | `true` | ポップオーバーにタイトルを表示するか |
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
hoverPreviewPlugin({
|
|
42
|
+
delay: 200,
|
|
43
|
+
excerptLength: 120,
|
|
44
|
+
maxEntries: 200,
|
|
45
|
+
selector: 'a[href^="/notes/"]',
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## 主なエクスポート
|
|
50
|
+
|
|
51
|
+
- `hoverPreviewPlugin(options?)`: プラグインを作成する
|
|
52
|
+
- `hoverPreview`: `hoverPreviewPlugin` の別名
|
|
53
|
+
- `resolveHoverPreviewOptions(options?)`: 既定値を適用して `ResolvedHoverPreviewOptions` を返す
|
|
54
|
+
- `buildPreviewIndex(entries, options)`: `HoverPreviewIndex`(`permalink` → `{ title, excerpt, slug }`)を作る
|
|
55
|
+
- `htmlToPlainText(html)` / `createExcerpt(html, length)`: 抜粋を作る補助関数
|
|
56
|
+
- `initHoverPreview()`: ブラウザ側の初期化処理。`@riebeckite/plugin-hover-preview/client` からも読み込める
|
|
57
|
+
- 定数: `HOVER_PREVIEW_ATTRIBUTE`, `HOVER_PREVIEW_SCRIPT_ID`
|
|
58
|
+
- 型: `HoverPreviewOptions`, `ResolvedHoverPreviewOptions`, `HoverPreviewEntry`, `HoverPreviewIndex`
|
|
59
|
+
|
|
60
|
+
## 関連資料
|
|
61
|
+
|
|
62
|
+
- [プラグインシステム](../../../docs/ja/plugin-system.md)
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { initHoverPreview } from "./src/init.js";
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// src/init.ts
|
|
2
|
+
var DEFAULT_SELECTOR = 'a[href^="/"]';
|
|
3
|
+
var DEFAULT_CLASS_NAME = "rb-hover-preview";
|
|
4
|
+
var DEFAULT_DELAY = 120;
|
|
5
|
+
var GAP = 8;
|
|
6
|
+
var MARGIN = 8;
|
|
7
|
+
function initHoverPreview() {
|
|
8
|
+
if (typeof document === "undefined") return () => {
|
|
9
|
+
};
|
|
10
|
+
const root = document.documentElement;
|
|
11
|
+
if (root.dataset.hoverPreviewInit === "true") return () => {
|
|
12
|
+
};
|
|
13
|
+
const payload = document.querySelector(
|
|
14
|
+
"script[data-rb-hover-preview]"
|
|
15
|
+
);
|
|
16
|
+
if (!payload) return () => {
|
|
17
|
+
};
|
|
18
|
+
const index = parsePayload(payload.textContent);
|
|
19
|
+
if (!index) return () => {
|
|
20
|
+
};
|
|
21
|
+
const config = {
|
|
22
|
+
selector: normalizeSelector(payload.dataset.selector),
|
|
23
|
+
className: normalizeClassName(payload.dataset.className),
|
|
24
|
+
includeTitles: payload.dataset.includeTitles !== "false",
|
|
25
|
+
delay: normalizeDelay(payload.dataset.delay)
|
|
26
|
+
};
|
|
27
|
+
const anchors = Array.from(
|
|
28
|
+
document.querySelectorAll(config.selector)
|
|
29
|
+
);
|
|
30
|
+
if (anchors.length === 0) return () => {
|
|
31
|
+
};
|
|
32
|
+
root.dataset.hoverPreviewInit = "true";
|
|
33
|
+
const popover = document.createElement("div");
|
|
34
|
+
popover.className = config.className;
|
|
35
|
+
popover.setAttribute("data-hover-preview", "");
|
|
36
|
+
popover.setAttribute("role", "tooltip");
|
|
37
|
+
popover.hidden = true;
|
|
38
|
+
document.body.appendChild(popover);
|
|
39
|
+
let showTimer = 0;
|
|
40
|
+
let activeAnchor = null;
|
|
41
|
+
const hide = () => {
|
|
42
|
+
window.clearTimeout(showTimer);
|
|
43
|
+
showTimer = 0;
|
|
44
|
+
activeAnchor = null;
|
|
45
|
+
popover.hidden = true;
|
|
46
|
+
};
|
|
47
|
+
const show = (anchor) => {
|
|
48
|
+
window.clearTimeout(showTimer);
|
|
49
|
+
showTimer = 0;
|
|
50
|
+
const entry = resolveEntry(index, anchor.getAttribute("href"));
|
|
51
|
+
if (!entry) return;
|
|
52
|
+
renderPopover(popover, entry, config);
|
|
53
|
+
popover.hidden = false;
|
|
54
|
+
positionPopover(popover, anchor);
|
|
55
|
+
activeAnchor = anchor;
|
|
56
|
+
};
|
|
57
|
+
const schedule = (anchor, delay) => {
|
|
58
|
+
window.clearTimeout(showTimer);
|
|
59
|
+
if (delay <= 0) {
|
|
60
|
+
show(anchor);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
showTimer = window.setTimeout(() => show(anchor), delay);
|
|
64
|
+
};
|
|
65
|
+
const listeners = [];
|
|
66
|
+
const listen = (target, type, handler, options) => {
|
|
67
|
+
target.addEventListener(type, handler, options);
|
|
68
|
+
listeners.push({ target, type, handler });
|
|
69
|
+
};
|
|
70
|
+
for (const anchor of anchors) {
|
|
71
|
+
listen(anchor, "mouseenter", () => schedule(anchor, config.delay));
|
|
72
|
+
listen(anchor, "mouseleave", hide);
|
|
73
|
+
listen(anchor, "focus", () => schedule(anchor, config.delay));
|
|
74
|
+
listen(anchor, "blur", hide);
|
|
75
|
+
listen(anchor, "touchstart", () => show(anchor), { passive: true });
|
|
76
|
+
}
|
|
77
|
+
listen(document, "scroll", hide, { passive: true, capture: true });
|
|
78
|
+
listen(document, "touchstart", (event) => {
|
|
79
|
+
const target = event.target;
|
|
80
|
+
if (activeAnchor && target instanceof Node && activeAnchor.contains(target)) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (target instanceof Node && popover.contains(target)) return;
|
|
84
|
+
hide();
|
|
85
|
+
});
|
|
86
|
+
listen(document, "keydown", (event) => {
|
|
87
|
+
if (event.key === "Escape") hide();
|
|
88
|
+
});
|
|
89
|
+
return () => {
|
|
90
|
+
hide();
|
|
91
|
+
for (const { target, type, handler } of listeners) {
|
|
92
|
+
target.removeEventListener(type, handler);
|
|
93
|
+
}
|
|
94
|
+
listeners.length = 0;
|
|
95
|
+
popover.remove();
|
|
96
|
+
delete root.dataset.hoverPreviewInit;
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function parsePayload(text) {
|
|
100
|
+
if (!text) return null;
|
|
101
|
+
try {
|
|
102
|
+
const parsed = JSON.parse(text);
|
|
103
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
return parsed;
|
|
107
|
+
} catch {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
function resolveEntry(index, href) {
|
|
112
|
+
if (!href) return null;
|
|
113
|
+
const withoutFragment = href.split("#")[0] ?? href;
|
|
114
|
+
const clean = withoutFragment.split("?")[0] ?? withoutFragment;
|
|
115
|
+
const candidates = /* @__PURE__ */ new Set([href, clean]);
|
|
116
|
+
if (clean.endsWith("/")) {
|
|
117
|
+
candidates.add(clean.slice(0, -1));
|
|
118
|
+
} else {
|
|
119
|
+
candidates.add(`${clean}/`);
|
|
120
|
+
}
|
|
121
|
+
try {
|
|
122
|
+
candidates.add(decodeURI(clean));
|
|
123
|
+
} catch {
|
|
124
|
+
}
|
|
125
|
+
for (const candidate of candidates) {
|
|
126
|
+
const entry = index[candidate];
|
|
127
|
+
if (entry) return entry;
|
|
128
|
+
}
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
function renderPopover(popover, entry, config) {
|
|
132
|
+
popover.replaceChildren();
|
|
133
|
+
if (config.includeTitles && entry.title) {
|
|
134
|
+
const title = document.createElement("p");
|
|
135
|
+
title.className = `${config.className}__title`;
|
|
136
|
+
title.textContent = entry.title;
|
|
137
|
+
popover.appendChild(title);
|
|
138
|
+
}
|
|
139
|
+
if (entry.excerpt) {
|
|
140
|
+
const excerpt = document.createElement("p");
|
|
141
|
+
excerpt.className = `${config.className}__excerpt`;
|
|
142
|
+
excerpt.textContent = entry.excerpt;
|
|
143
|
+
popover.appendChild(excerpt);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function positionPopover(popover, anchor) {
|
|
147
|
+
const anchorRect = anchor.getBoundingClientRect();
|
|
148
|
+
const popoverRect = popover.getBoundingClientRect();
|
|
149
|
+
let top = anchorRect.bottom + GAP;
|
|
150
|
+
if (top + popoverRect.height > window.innerHeight - MARGIN && anchorRect.top - GAP - popoverRect.height >= MARGIN) {
|
|
151
|
+
top = anchorRect.top - GAP - popoverRect.height;
|
|
152
|
+
}
|
|
153
|
+
const maxLeft = window.innerWidth - popoverRect.width - MARGIN;
|
|
154
|
+
const left = Math.min(
|
|
155
|
+
Math.max(anchorRect.left, MARGIN),
|
|
156
|
+
Math.max(maxLeft, MARGIN)
|
|
157
|
+
);
|
|
158
|
+
popover.style.top = `${Math.round(top)}px`;
|
|
159
|
+
popover.style.left = `${Math.round(left)}px`;
|
|
160
|
+
}
|
|
161
|
+
function normalizeSelector(value) {
|
|
162
|
+
return value && value.trim() !== "" ? value.trim() : DEFAULT_SELECTOR;
|
|
163
|
+
}
|
|
164
|
+
function normalizeClassName(value) {
|
|
165
|
+
return value && value.trim() !== "" ? value.trim() : DEFAULT_CLASS_NAME;
|
|
166
|
+
}
|
|
167
|
+
function normalizeDelay(value) {
|
|
168
|
+
const delay = Number(value ?? DEFAULT_DELAY);
|
|
169
|
+
return Number.isFinite(delay) && delay >= 0 ? delay : DEFAULT_DELAY;
|
|
170
|
+
}
|
|
171
|
+
export {
|
|
172
|
+
initHoverPreview
|
|
173
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { HoverPreviewOptions } from "./src/types.js";
|
|
2
|
+
export { initHoverPreview } from "./src/init.js";
|
|
3
|
+
export { DEFAULT_HOVER_PREVIEW_CLASS, DEFAULT_HOVER_PREVIEW_DELAY, DEFAULT_HOVER_PREVIEW_EXCERPT_LENGTH, DEFAULT_HOVER_PREVIEW_SELECTOR, resolveHoverPreviewOptions, } from "./src/options.js";
|
|
4
|
+
export { buildPreviewIndex, createExcerpt, htmlToPlainText, } from "./src/preview-index.js";
|
|
5
|
+
export { HOVER_PREVIEW_ATTRIBUTE, HOVER_PREVIEW_SCRIPT_ID, hasInternalLink, renderHoverPreviewPayload, } from "./src/render.js";
|
|
6
|
+
export type { HoverPreviewEntry, HoverPreviewIndex, HoverPreviewOptions, ResolvedHoverPreviewOptions, } from "./src/types.js";
|
|
7
|
+
export declare function hoverPreviewPlugin(options?: HoverPreviewOptions): import("@riebeckite/core").RiebeckitePlugin<HoverPreviewOptions>;
|
|
8
|
+
export declare const hoverPreview: typeof hoverPreviewPlugin;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
// index.ts
|
|
2
|
+
import {
|
|
3
|
+
createClientEntry,
|
|
4
|
+
createStyleAsset,
|
|
5
|
+
definePlugin,
|
|
6
|
+
isPublishable
|
|
7
|
+
} from "@riebeckite/core";
|
|
8
|
+
|
|
9
|
+
// src/options.ts
|
|
10
|
+
var DEFAULT_HOVER_PREVIEW_DELAY = 120;
|
|
11
|
+
var DEFAULT_HOVER_PREVIEW_EXCERPT_LENGTH = 160;
|
|
12
|
+
var DEFAULT_HOVER_PREVIEW_SELECTOR = 'a[href^="/"]';
|
|
13
|
+
var DEFAULT_HOVER_PREVIEW_CLASS = "rb-hover-preview";
|
|
14
|
+
function resolveHoverPreviewOptions(options = {}) {
|
|
15
|
+
return {
|
|
16
|
+
delay: normalizeNonNegative(options.delay, DEFAULT_HOVER_PREVIEW_DELAY),
|
|
17
|
+
excerptLength: normalizePositive(
|
|
18
|
+
options.excerptLength,
|
|
19
|
+
DEFAULT_HOVER_PREVIEW_EXCERPT_LENGTH
|
|
20
|
+
),
|
|
21
|
+
maxEntries: normalizeMaxEntries(options.maxEntries),
|
|
22
|
+
selector: normalizeString(options.selector, DEFAULT_HOVER_PREVIEW_SELECTOR),
|
|
23
|
+
className: normalizeString(options.className, DEFAULT_HOVER_PREVIEW_CLASS),
|
|
24
|
+
includeTitles: options.includeTitles ?? true
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function normalizeNonNegative(value, fallback) {
|
|
28
|
+
return value === void 0 || !Number.isFinite(value) || value < 0 ? fallback : value;
|
|
29
|
+
}
|
|
30
|
+
function normalizePositive(value, fallback) {
|
|
31
|
+
return value === void 0 || !Number.isFinite(value) || value <= 0 ? fallback : value;
|
|
32
|
+
}
|
|
33
|
+
function normalizeMaxEntries(value) {
|
|
34
|
+
return value === void 0 || !Number.isInteger(value) || value < 1 ? void 0 : value;
|
|
35
|
+
}
|
|
36
|
+
function normalizeString(value, fallback) {
|
|
37
|
+
return typeof value === "string" && value.trim() !== "" ? value.trim() : fallback;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// src/preview-index.ts
|
|
41
|
+
function buildPreviewIndex(entries, options) {
|
|
42
|
+
const pool = options.maxEntries === void 0 ? entries : entries.slice(0, options.maxEntries);
|
|
43
|
+
const index = {};
|
|
44
|
+
for (const entry of pool) {
|
|
45
|
+
index[entry.permalink] = {
|
|
46
|
+
title: entry.title,
|
|
47
|
+
excerpt: createExcerpt(entry.html, options.excerptLength),
|
|
48
|
+
slug: entry.slug
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return index;
|
|
52
|
+
}
|
|
53
|
+
function createExcerpt(html, length) {
|
|
54
|
+
const text = htmlToPlainText(html);
|
|
55
|
+
if (text.length <= length) return text;
|
|
56
|
+
const truncated = text.slice(0, Math.max(length - 1, 0)).trimEnd();
|
|
57
|
+
return `${truncated}…`;
|
|
58
|
+
}
|
|
59
|
+
function htmlToPlainText(html) {
|
|
60
|
+
return decodeHtmlEntities(
|
|
61
|
+
html.replace(/<script\b[^>]*>[\s\S]*?<\/script>/gi, " ").replace(/<style\b[^>]*>[\s\S]*?<\/style>/gi, " ").replace(/<[^>]+>/g, " ")
|
|
62
|
+
).replace(/\s+/g, " ").trim();
|
|
63
|
+
}
|
|
64
|
+
function decodeHtmlEntities(value) {
|
|
65
|
+
return value.replace(/ /g, " ").replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"').replace(/'/g, "'");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// src/render.ts
|
|
69
|
+
import { escapeHtmlAttribute } from "@riebeckite/core";
|
|
70
|
+
var HOVER_PREVIEW_SCRIPT_ID = "rb-hover-preview-data";
|
|
71
|
+
var HOVER_PREVIEW_ATTRIBUTE = "data-rb-hover-preview";
|
|
72
|
+
function renderHoverPreviewPayload(index, options) {
|
|
73
|
+
const attributes = [
|
|
74
|
+
'type="application/json"',
|
|
75
|
+
`id="${escapeHtmlAttribute(HOVER_PREVIEW_SCRIPT_ID)}"`,
|
|
76
|
+
HOVER_PREVIEW_ATTRIBUTE,
|
|
77
|
+
`data-selector="${escapeHtmlAttribute(options.selector)}"`,
|
|
78
|
+
`data-delay="${escapeHtmlAttribute(String(options.delay))}"`,
|
|
79
|
+
`data-class-name="${escapeHtmlAttribute(options.className)}"`,
|
|
80
|
+
`data-include-titles="${escapeHtmlAttribute(String(options.includeTitles))}"`
|
|
81
|
+
].join(" ");
|
|
82
|
+
return `<script ${attributes}>${escapeScriptJson(JSON.stringify(index))}</script>`;
|
|
83
|
+
}
|
|
84
|
+
function hasInternalLink(html) {
|
|
85
|
+
return /href\s*=\s*["']?\//.test(html);
|
|
86
|
+
}
|
|
87
|
+
function escapeScriptJson(value) {
|
|
88
|
+
return value.replace(/</g, "\\u003c").replace(/>/g, "\\u003e").replace(/&/g, "\\u0026");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// src/runtime.ts
|
|
92
|
+
function createHoverPreviewRuntime(options) {
|
|
93
|
+
const tracked = /* @__PURE__ */ new Map();
|
|
94
|
+
return {
|
|
95
|
+
track(slug, content) {
|
|
96
|
+
tracked.set(slug, content);
|
|
97
|
+
},
|
|
98
|
+
inject(manifest, shouldInclude) {
|
|
99
|
+
const entries = shouldInclude ? manifest.entries.filter(shouldInclude) : manifest.entries;
|
|
100
|
+
const index = buildPreviewIndex(entries, {
|
|
101
|
+
excerptLength: options.excerptLength,
|
|
102
|
+
...options.maxEntries === void 0 ? {} : { maxEntries: options.maxEntries }
|
|
103
|
+
});
|
|
104
|
+
if (Object.keys(index).length === 0) return;
|
|
105
|
+
const payload = renderHoverPreviewPayload(index, options);
|
|
106
|
+
for (const entry of entries) {
|
|
107
|
+
if (!hasInternalLink(entry.html)) continue;
|
|
108
|
+
if (entry.html.includes(HOVER_PREVIEW_ATTRIBUTE)) continue;
|
|
109
|
+
const html = `${entry.html}${payload}`;
|
|
110
|
+
entry.html = html;
|
|
111
|
+
const content = tracked.get(entry.slug);
|
|
112
|
+
if (content) content.html = html;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// src/init.ts
|
|
119
|
+
var DEFAULT_SELECTOR = 'a[href^="/"]';
|
|
120
|
+
var DEFAULT_CLASS_NAME = "rb-hover-preview";
|
|
121
|
+
var DEFAULT_DELAY = 120;
|
|
122
|
+
var GAP = 8;
|
|
123
|
+
var MARGIN = 8;
|
|
124
|
+
function initHoverPreview() {
|
|
125
|
+
if (typeof document === "undefined") return () => {
|
|
126
|
+
};
|
|
127
|
+
const root = document.documentElement;
|
|
128
|
+
if (root.dataset.hoverPreviewInit === "true") return () => {
|
|
129
|
+
};
|
|
130
|
+
const payload = document.querySelector(
|
|
131
|
+
"script[data-rb-hover-preview]"
|
|
132
|
+
);
|
|
133
|
+
if (!payload) return () => {
|
|
134
|
+
};
|
|
135
|
+
const index = parsePayload(payload.textContent);
|
|
136
|
+
if (!index) return () => {
|
|
137
|
+
};
|
|
138
|
+
const config = {
|
|
139
|
+
selector: normalizeSelector(payload.dataset.selector),
|
|
140
|
+
className: normalizeClassName(payload.dataset.className),
|
|
141
|
+
includeTitles: payload.dataset.includeTitles !== "false",
|
|
142
|
+
delay: normalizeDelay(payload.dataset.delay)
|
|
143
|
+
};
|
|
144
|
+
const anchors = Array.from(
|
|
145
|
+
document.querySelectorAll(config.selector)
|
|
146
|
+
);
|
|
147
|
+
if (anchors.length === 0) return () => {
|
|
148
|
+
};
|
|
149
|
+
root.dataset.hoverPreviewInit = "true";
|
|
150
|
+
const popover = document.createElement("div");
|
|
151
|
+
popover.className = config.className;
|
|
152
|
+
popover.setAttribute("data-hover-preview", "");
|
|
153
|
+
popover.setAttribute("role", "tooltip");
|
|
154
|
+
popover.hidden = true;
|
|
155
|
+
document.body.appendChild(popover);
|
|
156
|
+
let showTimer = 0;
|
|
157
|
+
let activeAnchor = null;
|
|
158
|
+
const hide = () => {
|
|
159
|
+
window.clearTimeout(showTimer);
|
|
160
|
+
showTimer = 0;
|
|
161
|
+
activeAnchor = null;
|
|
162
|
+
popover.hidden = true;
|
|
163
|
+
};
|
|
164
|
+
const show = (anchor) => {
|
|
165
|
+
window.clearTimeout(showTimer);
|
|
166
|
+
showTimer = 0;
|
|
167
|
+
const entry = resolveEntry(index, anchor.getAttribute("href"));
|
|
168
|
+
if (!entry) return;
|
|
169
|
+
renderPopover(popover, entry, config);
|
|
170
|
+
popover.hidden = false;
|
|
171
|
+
positionPopover(popover, anchor);
|
|
172
|
+
activeAnchor = anchor;
|
|
173
|
+
};
|
|
174
|
+
const schedule = (anchor, delay) => {
|
|
175
|
+
window.clearTimeout(showTimer);
|
|
176
|
+
if (delay <= 0) {
|
|
177
|
+
show(anchor);
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
showTimer = window.setTimeout(() => show(anchor), delay);
|
|
181
|
+
};
|
|
182
|
+
const listeners = [];
|
|
183
|
+
const listen = (target, type, handler, options) => {
|
|
184
|
+
target.addEventListener(type, handler, options);
|
|
185
|
+
listeners.push({ target, type, handler });
|
|
186
|
+
};
|
|
187
|
+
for (const anchor of anchors) {
|
|
188
|
+
listen(anchor, "mouseenter", () => schedule(anchor, config.delay));
|
|
189
|
+
listen(anchor, "mouseleave", hide);
|
|
190
|
+
listen(anchor, "focus", () => schedule(anchor, config.delay));
|
|
191
|
+
listen(anchor, "blur", hide);
|
|
192
|
+
listen(anchor, "touchstart", () => show(anchor), { passive: true });
|
|
193
|
+
}
|
|
194
|
+
listen(document, "scroll", hide, { passive: true, capture: true });
|
|
195
|
+
listen(document, "touchstart", (event) => {
|
|
196
|
+
const target = event.target;
|
|
197
|
+
if (activeAnchor && target instanceof Node && activeAnchor.contains(target)) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
if (target instanceof Node && popover.contains(target)) return;
|
|
201
|
+
hide();
|
|
202
|
+
});
|
|
203
|
+
listen(document, "keydown", (event) => {
|
|
204
|
+
if (event.key === "Escape") hide();
|
|
205
|
+
});
|
|
206
|
+
return () => {
|
|
207
|
+
hide();
|
|
208
|
+
for (const { target, type, handler } of listeners) {
|
|
209
|
+
target.removeEventListener(type, handler);
|
|
210
|
+
}
|
|
211
|
+
listeners.length = 0;
|
|
212
|
+
popover.remove();
|
|
213
|
+
delete root.dataset.hoverPreviewInit;
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
function parsePayload(text) {
|
|
217
|
+
if (!text) return null;
|
|
218
|
+
try {
|
|
219
|
+
const parsed = JSON.parse(text);
|
|
220
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
return parsed;
|
|
224
|
+
} catch {
|
|
225
|
+
return null;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
function resolveEntry(index, href) {
|
|
229
|
+
if (!href) return null;
|
|
230
|
+
const withoutFragment = href.split("#")[0] ?? href;
|
|
231
|
+
const clean = withoutFragment.split("?")[0] ?? withoutFragment;
|
|
232
|
+
const candidates = /* @__PURE__ */ new Set([href, clean]);
|
|
233
|
+
if (clean.endsWith("/")) {
|
|
234
|
+
candidates.add(clean.slice(0, -1));
|
|
235
|
+
} else {
|
|
236
|
+
candidates.add(`${clean}/`);
|
|
237
|
+
}
|
|
238
|
+
try {
|
|
239
|
+
candidates.add(decodeURI(clean));
|
|
240
|
+
} catch {
|
|
241
|
+
}
|
|
242
|
+
for (const candidate of candidates) {
|
|
243
|
+
const entry = index[candidate];
|
|
244
|
+
if (entry) return entry;
|
|
245
|
+
}
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
function renderPopover(popover, entry, config) {
|
|
249
|
+
popover.replaceChildren();
|
|
250
|
+
if (config.includeTitles && entry.title) {
|
|
251
|
+
const title = document.createElement("p");
|
|
252
|
+
title.className = `${config.className}__title`;
|
|
253
|
+
title.textContent = entry.title;
|
|
254
|
+
popover.appendChild(title);
|
|
255
|
+
}
|
|
256
|
+
if (entry.excerpt) {
|
|
257
|
+
const excerpt = document.createElement("p");
|
|
258
|
+
excerpt.className = `${config.className}__excerpt`;
|
|
259
|
+
excerpt.textContent = entry.excerpt;
|
|
260
|
+
popover.appendChild(excerpt);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
function positionPopover(popover, anchor) {
|
|
264
|
+
const anchorRect = anchor.getBoundingClientRect();
|
|
265
|
+
const popoverRect = popover.getBoundingClientRect();
|
|
266
|
+
let top = anchorRect.bottom + GAP;
|
|
267
|
+
if (top + popoverRect.height > window.innerHeight - MARGIN && anchorRect.top - GAP - popoverRect.height >= MARGIN) {
|
|
268
|
+
top = anchorRect.top - GAP - popoverRect.height;
|
|
269
|
+
}
|
|
270
|
+
const maxLeft = window.innerWidth - popoverRect.width - MARGIN;
|
|
271
|
+
const left = Math.min(
|
|
272
|
+
Math.max(anchorRect.left, MARGIN),
|
|
273
|
+
Math.max(maxLeft, MARGIN)
|
|
274
|
+
);
|
|
275
|
+
popover.style.top = `${Math.round(top)}px`;
|
|
276
|
+
popover.style.left = `${Math.round(left)}px`;
|
|
277
|
+
}
|
|
278
|
+
function normalizeSelector(value) {
|
|
279
|
+
return value && value.trim() !== "" ? value.trim() : DEFAULT_SELECTOR;
|
|
280
|
+
}
|
|
281
|
+
function normalizeClassName(value) {
|
|
282
|
+
return value && value.trim() !== "" ? value.trim() : DEFAULT_CLASS_NAME;
|
|
283
|
+
}
|
|
284
|
+
function normalizeDelay(value) {
|
|
285
|
+
const delay = Number(value ?? DEFAULT_DELAY);
|
|
286
|
+
return Number.isFinite(delay) && delay >= 0 ? delay : DEFAULT_DELAY;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// index.ts
|
|
290
|
+
function hoverPreviewPlugin(options = {}) {
|
|
291
|
+
const resolved = resolveHoverPreviewOptions(options);
|
|
292
|
+
const runtime = createHoverPreviewRuntime(resolved);
|
|
293
|
+
return definePlugin({
|
|
294
|
+
name: "hover-preview",
|
|
295
|
+
options,
|
|
296
|
+
validateOptions: validateHoverPreviewOptions,
|
|
297
|
+
onPostProcessed: (context) => {
|
|
298
|
+
runtime.track(context.slug, context.content);
|
|
299
|
+
},
|
|
300
|
+
onManifestCreated: (context) => {
|
|
301
|
+
const strategy = context.config?.content.filters.publishStrategy ?? "explicit";
|
|
302
|
+
runtime.inject(
|
|
303
|
+
context.manifest,
|
|
304
|
+
(entry) => isPublishable(strategy, entry.frontmatter)
|
|
305
|
+
);
|
|
306
|
+
},
|
|
307
|
+
assets: [createStyleAsset("hover-preview")],
|
|
308
|
+
clientEntries: [createClientEntry("hover-preview", "initHoverPreview")]
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
var hoverPreview = hoverPreviewPlugin;
|
|
312
|
+
function validateHoverPreviewOptions(options) {
|
|
313
|
+
if (!options) return [];
|
|
314
|
+
const issues = [];
|
|
315
|
+
if (options.delay !== void 0 && (!Number.isFinite(options.delay) || options.delay < 0)) {
|
|
316
|
+
issues.push({
|
|
317
|
+
path: "delay",
|
|
318
|
+
message: "Expected a non-negative finite number."
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
if (options.excerptLength !== void 0 && (!Number.isFinite(options.excerptLength) || options.excerptLength <= 0)) {
|
|
322
|
+
issues.push({
|
|
323
|
+
path: "excerptLength",
|
|
324
|
+
message: "Expected a positive finite number."
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
if (options.maxEntries !== void 0 && (!Number.isInteger(options.maxEntries) || options.maxEntries < 1)) {
|
|
328
|
+
issues.push({
|
|
329
|
+
path: "maxEntries",
|
|
330
|
+
message: "Expected a positive integer."
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
if (options.selector !== void 0 && (typeof options.selector !== "string" || options.selector.trim() === "")) {
|
|
334
|
+
issues.push({
|
|
335
|
+
path: "selector",
|
|
336
|
+
message: "Expected a non-empty string."
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
if (options.className !== void 0 && (typeof options.className !== "string" || options.className.trim() === "")) {
|
|
340
|
+
issues.push({
|
|
341
|
+
path: "className",
|
|
342
|
+
message: "Expected a non-empty string."
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
if (options.includeTitles !== void 0 && typeof options.includeTitles !== "boolean") {
|
|
346
|
+
issues.push({
|
|
347
|
+
path: "includeTitles",
|
|
348
|
+
message: "Expected a boolean."
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
return issues;
|
|
352
|
+
}
|
|
353
|
+
export {
|
|
354
|
+
DEFAULT_HOVER_PREVIEW_CLASS,
|
|
355
|
+
DEFAULT_HOVER_PREVIEW_DELAY,
|
|
356
|
+
DEFAULT_HOVER_PREVIEW_EXCERPT_LENGTH,
|
|
357
|
+
DEFAULT_HOVER_PREVIEW_SELECTOR,
|
|
358
|
+
HOVER_PREVIEW_ATTRIBUTE,
|
|
359
|
+
HOVER_PREVIEW_SCRIPT_ID,
|
|
360
|
+
buildPreviewIndex,
|
|
361
|
+
createExcerpt,
|
|
362
|
+
hasInternalLink,
|
|
363
|
+
hoverPreview,
|
|
364
|
+
hoverPreviewPlugin,
|
|
365
|
+
htmlToPlainText,
|
|
366
|
+
initHoverPreview,
|
|
367
|
+
renderHoverPreviewPayload,
|
|
368
|
+
resolveHoverPreviewOptions
|
|
369
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function initHoverPreview(): () => void;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { HoverPreviewOptions, ResolvedHoverPreviewOptions } from "./types.js";
|
|
2
|
+
export declare const DEFAULT_HOVER_PREVIEW_DELAY = 120;
|
|
3
|
+
export declare const DEFAULT_HOVER_PREVIEW_EXCERPT_LENGTH = 160;
|
|
4
|
+
export declare const DEFAULT_HOVER_PREVIEW_SELECTOR = "a[href^=\"/\"]";
|
|
5
|
+
export declare const DEFAULT_HOVER_PREVIEW_CLASS = "rb-hover-preview";
|
|
6
|
+
export declare function resolveHoverPreviewOptions(options?: HoverPreviewOptions): ResolvedHoverPreviewOptions;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ContentManifestEntry } from "@riebeckite/core";
|
|
2
|
+
import type { HoverPreviewIndex } from "./types.js";
|
|
3
|
+
export type PreviewIndexSource = {
|
|
4
|
+
permalink: string;
|
|
5
|
+
slug: string;
|
|
6
|
+
title: string;
|
|
7
|
+
html: string;
|
|
8
|
+
};
|
|
9
|
+
export declare function buildPreviewIndex(entries: readonly (ContentManifestEntry | PreviewIndexSource)[], options: {
|
|
10
|
+
excerptLength: number;
|
|
11
|
+
maxEntries?: number;
|
|
12
|
+
}): HoverPreviewIndex;
|
|
13
|
+
export declare function createExcerpt(html: string, length: number): string;
|
|
14
|
+
export declare function htmlToPlainText(html: string): string;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { HoverPreviewIndex, ResolvedHoverPreviewOptions } from "./types.js";
|
|
2
|
+
export declare const HOVER_PREVIEW_SCRIPT_ID = "rb-hover-preview-data";
|
|
3
|
+
export declare const HOVER_PREVIEW_ATTRIBUTE = "data-rb-hover-preview";
|
|
4
|
+
export declare function renderHoverPreviewPayload(index: HoverPreviewIndex, options: ResolvedHoverPreviewOptions): string;
|
|
5
|
+
export declare function hasInternalLink(html: string): boolean;
|
|
6
|
+
export declare function escapeScriptJson(value: string): string;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ContentManifest, ContentManifestEntry, PostContent } from "@riebeckite/core";
|
|
2
|
+
import type { ResolvedHoverPreviewOptions } from "./types.js";
|
|
3
|
+
export type HoverPreviewRuntime = {
|
|
4
|
+
track(slug: string, content: PostContent): void;
|
|
5
|
+
inject(manifest: ContentManifest, shouldInclude?: (entry: ContentManifestEntry) => boolean): void;
|
|
6
|
+
};
|
|
7
|
+
export declare function createHoverPreviewRuntime(options: ResolvedHoverPreviewOptions): HoverPreviewRuntime;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export type HoverPreviewOptions = {
|
|
2
|
+
/**
|
|
3
|
+
* Delay in milliseconds before a popover appears after the pointer enters a
|
|
4
|
+
* link. Defaults to `120`.
|
|
5
|
+
*/
|
|
6
|
+
delay?: number;
|
|
7
|
+
/**
|
|
8
|
+
* Maximum number of characters kept in a preview excerpt. Defaults to `160`.
|
|
9
|
+
*/
|
|
10
|
+
excerptLength?: number;
|
|
11
|
+
/**
|
|
12
|
+
* Maximum number of entries stored in the per-page payload. Unset keeps every
|
|
13
|
+
* entry.
|
|
14
|
+
*/
|
|
15
|
+
maxEntries?: number;
|
|
16
|
+
/**
|
|
17
|
+
* CSS selector used to find the internal links that receive a preview.
|
|
18
|
+
* Defaults to `a[href^="/"]`.
|
|
19
|
+
*/
|
|
20
|
+
selector?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Base class name applied to the popover element. Defaults to
|
|
23
|
+
* `rb-hover-preview`.
|
|
24
|
+
*/
|
|
25
|
+
className?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Whether the popover shows the target entry's title. Defaults to `true`.
|
|
28
|
+
*/
|
|
29
|
+
includeTitles?: boolean;
|
|
30
|
+
};
|
|
31
|
+
export type ResolvedHoverPreviewOptions = {
|
|
32
|
+
delay: number;
|
|
33
|
+
excerptLength: number;
|
|
34
|
+
maxEntries: number | undefined;
|
|
35
|
+
selector: string;
|
|
36
|
+
className: string;
|
|
37
|
+
includeTitles: boolean;
|
|
38
|
+
};
|
|
39
|
+
export type HoverPreviewEntry = {
|
|
40
|
+
title: string;
|
|
41
|
+
excerpt: string;
|
|
42
|
+
slug: string;
|
|
43
|
+
};
|
|
44
|
+
export type HoverPreviewIndex = Record<string, HoverPreviewEntry>;
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@riebeckite/plugin-hover-preview",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"source": "./index.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./client": {
|
|
16
|
+
"types": "./dist/client.d.ts",
|
|
17
|
+
"import": "./dist/client.js",
|
|
18
|
+
"source": "./client.ts",
|
|
19
|
+
"default": "./dist/client.js"
|
|
20
|
+
},
|
|
21
|
+
"./style.css": "./style.css"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@riebeckite/core": "0.0.1"
|
|
25
|
+
},
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"LICENSE",
|
|
31
|
+
"README.md",
|
|
32
|
+
"README_ja.md",
|
|
33
|
+
"dist",
|
|
34
|
+
"style.css"
|
|
35
|
+
],
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "https://github.com/Rerurate514/riebeckite.git",
|
|
39
|
+
"directory": "packages/plugins/hover-preview"
|
|
40
|
+
},
|
|
41
|
+
"homepage": "https://github.com/Rerurate514/riebeckite#readme",
|
|
42
|
+
"bugs": {
|
|
43
|
+
"url": "https://github.com/Rerurate514/riebeckite/issues"
|
|
44
|
+
},
|
|
45
|
+
"description": "Popover previews for internal Riebeckite links.",
|
|
46
|
+
"keywords": [
|
|
47
|
+
"riebeckite",
|
|
48
|
+
"plugin",
|
|
49
|
+
"link-preview",
|
|
50
|
+
"obsidian"
|
|
51
|
+
],
|
|
52
|
+
"engines": {
|
|
53
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
54
|
+
},
|
|
55
|
+
"sideEffects": [
|
|
56
|
+
"./style.css"
|
|
57
|
+
],
|
|
58
|
+
"scripts": {
|
|
59
|
+
"build": "node ../../../scripts/build_package.mjs"
|
|
60
|
+
}
|
|
61
|
+
}
|
package/style.css
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
.rb-hover-preview {
|
|
2
|
+
--rb-hover-preview-paper: var(--rb-color-paper, #ffffff);
|
|
3
|
+
--rb-hover-preview-ink: var(--rb-color-ink, #16130f);
|
|
4
|
+
--rb-hover-preview-muted: var(--rb-color-muted, #69645c);
|
|
5
|
+
--rb-hover-preview-rule: var(
|
|
6
|
+
--rb-color-border,
|
|
7
|
+
color-mix(in srgb, var(--rb-hover-preview-ink) 16%, transparent)
|
|
8
|
+
);
|
|
9
|
+
--rb-hover-preview-accent: var(--rb-color-accent, #b4531f);
|
|
10
|
+
|
|
11
|
+
position: fixed;
|
|
12
|
+
z-index: 70;
|
|
13
|
+
top: 0;
|
|
14
|
+
left: 0;
|
|
15
|
+
max-width: min(22rem, calc(100vw - 2rem));
|
|
16
|
+
padding: 0.625rem 0.75rem;
|
|
17
|
+
border: 1px solid var(--rb-hover-preview-rule);
|
|
18
|
+
border-left: 3px solid var(--rb-hover-preview-accent);
|
|
19
|
+
background: var(--rb-hover-preview-paper);
|
|
20
|
+
color: var(--rb-hover-preview-ink);
|
|
21
|
+
box-shadow: 0 14px 34px -18px
|
|
22
|
+
color-mix(in srgb, var(--rb-hover-preview-ink) 45%, transparent);
|
|
23
|
+
font-size: 0.875rem;
|
|
24
|
+
line-height: 1.5;
|
|
25
|
+
pointer-events: none;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
.rb-hover-preview[hidden] {
|
|
29
|
+
display: none;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
.rb-hover-preview__title {
|
|
33
|
+
margin: 0 0 0.25rem;
|
|
34
|
+
font-size: 0.875rem;
|
|
35
|
+
font-weight: 650;
|
|
36
|
+
line-height: 1.35;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
.rb-hover-preview__excerpt {
|
|
40
|
+
margin: 0;
|
|
41
|
+
color: var(--rb-hover-preview-muted);
|
|
42
|
+
font-size: 0.8125rem;
|
|
43
|
+
line-height: 1.5;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
html[data-theme="dark"] .rb-hover-preview,
|
|
47
|
+
html.dark .rb-hover-preview {
|
|
48
|
+
--rb-hover-preview-paper: var(--rb-color-ink, #14110d);
|
|
49
|
+
--rb-hover-preview-ink: var(--rb-color-paper, #f8f5ef);
|
|
50
|
+
--rb-hover-preview-muted: color-mix(
|
|
51
|
+
in srgb,
|
|
52
|
+
var(--rb-hover-preview-ink) 72%,
|
|
53
|
+
transparent
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
@media (prefers-reduced-motion: no-preference) {
|
|
58
|
+
.rb-hover-preview {
|
|
59
|
+
animation: rb-hover-preview-in 120ms ease-out;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
@keyframes rb-hover-preview-in {
|
|
63
|
+
from {
|
|
64
|
+
opacity: 0;
|
|
65
|
+
transform: translateY(-2px);
|
|
66
|
+
}
|
|
67
|
+
to {
|
|
68
|
+
opacity: 1;
|
|
69
|
+
transform: translateY(0);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|