@riebeckite/plugin-source-files 0.0.0-stage → 0.0.32
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 +59 -2
- package/README_ja.md +60 -0
- package/dist/client.d.ts +1 -0
- package/dist/client.js +53 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +218 -0
- package/dist/src/source.d.ts +7 -0
- package/package.json +62 -3
- package/style.css +45 -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
CHANGED
|
@@ -1,3 +1,60 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @riebeckite/plugin-source-files
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<!-- Generated from docs/docs/plugins/source-files.md. Edit the canonical documentation in docs/docs/plugins and run `pnpm docs:sync`. -->
|
|
4
|
+
|
|
5
|
+
Publish selected text source files as first-class, syntax-highlighted pages.
|
|
6
|
+
|
|
7
|
+
[日本語](./README_ja.md)
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
`sourceFiles()` requires explicit include patterns. A file is published only when a routable note links or embeds it, so draft and scheduled notes cannot expose a source page.
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { defineConfig } from "@riebeckite/core";
|
|
15
|
+
import { obsidianMarkdown } from "@riebeckite/plugin-obsidian-markdown";
|
|
16
|
+
import { codeEnhance } from "@riebeckite/plugin-code-enhance";
|
|
17
|
+
import { sourceFiles } from "@riebeckite/plugin-source-files";
|
|
18
|
+
|
|
19
|
+
export default defineConfig({
|
|
20
|
+
plugins: [
|
|
21
|
+
obsidianMarkdown(),
|
|
22
|
+
codeEnhance({ lineNumbers: true }),
|
|
23
|
+
sourceFiles({
|
|
24
|
+
include: ["src/**/*.ts", "scripts/*.ts"],
|
|
25
|
+
exclude: ["src/generated/**"],
|
|
26
|
+
}),
|
|
27
|
+
],
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Links and embeds
|
|
32
|
+
|
|
33
|
+
- `[src/main.ts](https://github.com/Rerurate514/riebeckite/blob/main/docs/docs/src/main.ts.md)` links to `/source/src/main.ts`.
|
|
34
|
+
- `` renders the source in the existing code-block path.
|
|
35
|
+
- `#L10` and `#L10-L20` select lines in an embed. With Code Enhance enabled, those source-page fragments scroll to and highlight the matching line or range.
|
|
36
|
+
|
|
37
|
+
Each source page includes its vault-relative path, detected language, size, a Copy action, and a Raw link to the normal attachment URL.
|
|
38
|
+
|
|
39
|
+
## Safety and publication
|
|
40
|
+
|
|
41
|
+
Only files matching `include` and not matching `exclude` are eligible. The plugin rejects unsafe logical paths, binary content, invalid UTF-8, files over `maxBytes`, and common secret file names and extensions (`.env`, keys, certificates, and credentials). It uses the configured `ContentSource`; it never reads a filesystem path directly or exposes an absolute path.
|
|
42
|
+
|
|
43
|
+
## Options
|
|
44
|
+
|
|
45
|
+
| Option | Type | Default | Description |
|
|
46
|
+
| --- | --- | --- | --- |
|
|
47
|
+
| `include` | `readonly string[]` | required | Vault-relative `*` / `**` patterns allowed for publication |
|
|
48
|
+
| `exclude` | `readonly string[]` | `[]` | Matching paths are never published |
|
|
49
|
+
| `maxBytes` | `number` | `1048576` | Maximum accepted UTF-8 file size |
|
|
50
|
+
|
|
51
|
+
## Exports
|
|
52
|
+
|
|
53
|
+
- `sourceFiles(options)` / `sourceFilesPlugin`
|
|
54
|
+
- `sourceFilePath(path)`
|
|
55
|
+
- `SourceFilesOptions`
|
|
56
|
+
|
|
57
|
+
## See also
|
|
58
|
+
|
|
59
|
+
- [`@riebeckite/plugin-obsidian-markdown`](../obsidian-markdown/README.md)
|
|
60
|
+
- [`@riebeckite/plugin-code-enhance`](../code-enhance/README.md)
|
package/README_ja.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# @riebeckite/plugin-source-files
|
|
2
|
+
|
|
3
|
+
<!-- Generated from docs/docs/plugins/source-files.ja.md. Edit the canonical documentation in docs/docs/plugins and run `pnpm docs:sync`. -->
|
|
4
|
+
|
|
5
|
+
指定したテキスト形式のソースファイルを、構文ハイライト対応の独立ページとして公開します。
|
|
6
|
+
|
|
7
|
+
[English](./README.md)
|
|
8
|
+
|
|
9
|
+
## 設定
|
|
10
|
+
|
|
11
|
+
`sourceFiles()` では公開対象の `include` パターンを必ず指定します。ルーティング可能なノートからリンクまたは埋め込みされたファイルだけが公開されるため、下書きや公開予定のノートからソースページが露出することはありません。
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { defineConfig } from "@riebeckite/core";
|
|
15
|
+
import { obsidianMarkdown } from "@riebeckite/plugin-obsidian-markdown";
|
|
16
|
+
import { codeEnhance } from "@riebeckite/plugin-code-enhance";
|
|
17
|
+
import { sourceFiles } from "@riebeckite/plugin-source-files";
|
|
18
|
+
|
|
19
|
+
export default defineConfig({
|
|
20
|
+
plugins: [
|
|
21
|
+
obsidianMarkdown(),
|
|
22
|
+
codeEnhance({ lineNumbers: true }),
|
|
23
|
+
sourceFiles({
|
|
24
|
+
include: ["src/**/*.ts", "scripts/*.ts"],
|
|
25
|
+
exclude: ["src/generated/**"],
|
|
26
|
+
}),
|
|
27
|
+
],
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## リンクと埋め込み
|
|
32
|
+
|
|
33
|
+
- `[src/main.ts](https://github.com/Rerurate514/riebeckite/blob/main/docs/docs/src/main.ts.ja.md)` は `/source/src/main.ts` のページへリンクします。
|
|
34
|
+
- `` は既存のコードブロック表示経路でソースを表示します。
|
|
35
|
+
- `#L10` と `#L10-L20` は埋め込みの行範囲を選びます。Code Enhance を有効にすると、ソースページでも該当の行または範囲へスクロールし、強調表示します。
|
|
36
|
+
|
|
37
|
+
ソースページには Vault 相対パス、検出した言語、サイズ、Copy 操作、通常の添付ファイル URL を指す Raw リンクを表示します。
|
|
38
|
+
|
|
39
|
+
## 安全性と公開境界
|
|
40
|
+
|
|
41
|
+
`include` に一致し `exclude` に一致しないファイルだけが対象です。不正な論理パス、バイナリ、UTF-8 として読めない内容、`maxBytes` を超えるファイル、代表的な秘密ファイル名・拡張子(`.env`、鍵、証明書、credentials)は拒否します。設定済みの `ContentSource` だけを利用し、ファイルシステムのパスを直接読んだり絶対パスを表示したりしません。
|
|
42
|
+
|
|
43
|
+
## オプション
|
|
44
|
+
|
|
45
|
+
| オプション | 型 | 既定値 | 内容 |
|
|
46
|
+
| --- | --- | --- | --- |
|
|
47
|
+
| `include` | `readonly string[]` | 必須 | 公開する Vault 相対 `*` / `**` パターン |
|
|
48
|
+
| `exclude` | `readonly string[]` | `[]` | 一致したパスを公開対象から除外 |
|
|
49
|
+
| `maxBytes` | `number` | `1048576` | 受け入れる UTF-8 ファイルの最大バイト数 |
|
|
50
|
+
|
|
51
|
+
## 公開 API
|
|
52
|
+
|
|
53
|
+
- `sourceFiles(options)` / `sourceFilesPlugin`
|
|
54
|
+
- `sourceFilePath(path)`
|
|
55
|
+
- `SourceFilesOptions`
|
|
56
|
+
|
|
57
|
+
## 関連資料
|
|
58
|
+
|
|
59
|
+
- [`@riebeckite/plugin-obsidian-markdown`](../obsidian-markdown/README_ja.md)
|
|
60
|
+
- [`@riebeckite/plugin-code-enhance`](../code-enhance/README_ja.md)
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function initSourceFiles(): () => void;
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// client.ts
|
|
2
|
+
function initSourceFiles() {
|
|
3
|
+
const onClick = async (event) => {
|
|
4
|
+
const target = event.target;
|
|
5
|
+
if (!(target instanceof Element)) return;
|
|
6
|
+
const button = target.closest("[data-source-file-copy]");
|
|
7
|
+
if (!button) return;
|
|
8
|
+
const code = document.querySelector(
|
|
9
|
+
".rr-source-file .rr-code__code, .rr-source-file pre code"
|
|
10
|
+
);
|
|
11
|
+
if (!code?.textContent) return;
|
|
12
|
+
await navigator.clipboard?.writeText(code.textContent);
|
|
13
|
+
button.dataset.sourceFileCopied = "true";
|
|
14
|
+
};
|
|
15
|
+
const updateLineTarget = () => {
|
|
16
|
+
const match = decodeURIComponent(window.location.hash.slice(1)).match(
|
|
17
|
+
/^L(\d+)(?:-L(\d+))?$/i
|
|
18
|
+
);
|
|
19
|
+
for (const line of document.querySelectorAll(
|
|
20
|
+
".rr-source-file [data-source-file] .rr-code__line"
|
|
21
|
+
)) {
|
|
22
|
+
line.classList.remove("rr-source-file__line--target");
|
|
23
|
+
line.removeAttribute("aria-current");
|
|
24
|
+
}
|
|
25
|
+
if (!match) return;
|
|
26
|
+
const start = Number(match[1]);
|
|
27
|
+
const end = Number(match[2] ?? match[1]);
|
|
28
|
+
if (start < 1 || end < start) return;
|
|
29
|
+
const target = document.getElementById(`L${start}`);
|
|
30
|
+
if (!target?.closest(".rr-source-file")) return;
|
|
31
|
+
for (let number = start; number <= end; number += 1) {
|
|
32
|
+
const line = document.getElementById(`L${number}`);
|
|
33
|
+
if (line?.closest(".rr-source-file")) {
|
|
34
|
+
line.classList.add("rr-source-file__line--target");
|
|
35
|
+
line.setAttribute("aria-current", "location");
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
target.scrollIntoView({ block: "center" });
|
|
39
|
+
target.focus({ preventScroll: true });
|
|
40
|
+
};
|
|
41
|
+
document.addEventListener("click", onClick);
|
|
42
|
+
window.addEventListener("hashchange", updateLineTarget);
|
|
43
|
+
document.addEventListener("riebeckite:navigation", updateLineTarget);
|
|
44
|
+
updateLineTarget();
|
|
45
|
+
return () => {
|
|
46
|
+
document.removeEventListener("click", onClick);
|
|
47
|
+
window.removeEventListener("hashchange", updateLineTarget);
|
|
48
|
+
document.removeEventListener("riebeckite:navigation", updateLineTarget);
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
export {
|
|
52
|
+
initSourceFiles
|
|
53
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { sourceFilePath } from "./src/source.js";
|
|
2
|
+
export type SourceFilesOptions = {
|
|
3
|
+
include: readonly string[];
|
|
4
|
+
exclude?: readonly string[];
|
|
5
|
+
maxBytes?: number;
|
|
6
|
+
};
|
|
7
|
+
export declare function sourceFiles(options: SourceFilesOptions): import("@riebeckite/core").RiebeckitePlugin<SourceFilesOptions>;
|
|
8
|
+
export declare const sourceFilesPlugin: typeof sourceFiles;
|
|
9
|
+
export { sourceFilePath };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// index.ts
|
|
2
|
+
import {
|
|
3
|
+
createClientEntry,
|
|
4
|
+
createStyleAsset,
|
|
5
|
+
definePlugin
|
|
6
|
+
} from "@riebeckite/core";
|
|
7
|
+
|
|
8
|
+
// src/source.ts
|
|
9
|
+
import {
|
|
10
|
+
attachmentUrl,
|
|
11
|
+
escapeHtml,
|
|
12
|
+
escapeHtmlAttribute,
|
|
13
|
+
getExtension,
|
|
14
|
+
readContentSourceEntry
|
|
15
|
+
} from "@riebeckite/core";
|
|
16
|
+
var DEFAULT_MAX_BYTES = 1024 * 1024;
|
|
17
|
+
var SECRET_NAMES = /* @__PURE__ */ new Set(["id_rsa", "credentials"]);
|
|
18
|
+
var SECRET_EXTENSIONS = /* @__PURE__ */ new Set(["key", "pem", "p12", "pfx"]);
|
|
19
|
+
async function renderSourceEmbed(context, options) {
|
|
20
|
+
if (context.kind !== "attachment" || !isSelectedPath(context.path, options)) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
const source = await readSource(context.contentSource, context.path, options);
|
|
24
|
+
if (!source) return null;
|
|
25
|
+
const label = context.label || fileName(context.path);
|
|
26
|
+
const href = `${sourceFilePath(context.path)}${context.fragment ? `#${encodeURIComponent(context.fragment)}` : ""}`;
|
|
27
|
+
if (!context.embed) {
|
|
28
|
+
return `<a class="rr-obsidian-wikilink rr-source-files__link" href="${escapeHtmlAttribute(href)}">${escapeHtml(label)}</a>`;
|
|
29
|
+
}
|
|
30
|
+
const code = selectLines(source.text, context.fragment);
|
|
31
|
+
return `<section class="rr-source-file-embed" data-source-file="${escapeHtmlAttribute(context.path)}"><a class="rr-source-file-embed__title" href="${escapeHtmlAttribute(href)}">${escapeHtml(label)}</a>${renderCode(code, context.path)}</section>`;
|
|
32
|
+
}
|
|
33
|
+
async function listSourceFiles(manifest, contentSource, options) {
|
|
34
|
+
const linked = /* @__PURE__ */ new Set();
|
|
35
|
+
for (const entry of manifest.publicEntries) {
|
|
36
|
+
for (const link of entry.links) {
|
|
37
|
+
if (link.kind === "attachment" && link.slug && isSelectedPath(link.slug, options)) {
|
|
38
|
+
linked.add(link.slug);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const readable = await Promise.all(
|
|
43
|
+
[...linked].sort().map(
|
|
44
|
+
async (path) => await readSource(contentSource, path, options) ? path : null
|
|
45
|
+
)
|
|
46
|
+
);
|
|
47
|
+
return readable.flatMap((path) => path ? [sourceFilePath(path)] : []);
|
|
48
|
+
}
|
|
49
|
+
async function resolveSourcePage(context, options) {
|
|
50
|
+
const sourcePath = resolveSourcePath(
|
|
51
|
+
context.pathname,
|
|
52
|
+
context.manifest,
|
|
53
|
+
options
|
|
54
|
+
);
|
|
55
|
+
if (!sourcePath) return null;
|
|
56
|
+
const source = await readSource(context.contentSource, sourcePath, options);
|
|
57
|
+
if (!source) return null;
|
|
58
|
+
const title = fileName(sourcePath);
|
|
59
|
+
const markdown = `\`\`\`${languageFor(sourcePath)} title="source-file:${title.replaceAll('"', "'")}"
|
|
60
|
+
${source.text}
|
|
61
|
+
\`\`\``;
|
|
62
|
+
const code = await context.renderMarkdown(markdown);
|
|
63
|
+
return {
|
|
64
|
+
type: "source-file",
|
|
65
|
+
pathname: context.pathname,
|
|
66
|
+
title,
|
|
67
|
+
body: `<main class="rr-source-file" data-source-file="${escapeHtmlAttribute(sourcePath)}"><header class="rr-source-file__header"><h1>${escapeHtml(title)}</h1><dl class="rr-source-file__metadata"><div><dt>Path</dt><dd>${escapeHtml(sourcePath)}</dd></div><div><dt>Language</dt><dd>${escapeHtml(languageFor(sourcePath))}</dd></div><div><dt>Size</dt><dd>${escapeHtml(formatBytes(source.bytes))}</dd></div></dl><div class="rr-source-file__actions"><button type="button" data-source-file-copy>Copy</button><a href="${escapeHtmlAttribute(attachmentUrl(sourcePath))}">Raw</a></div></header>${code}</main>`
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function sourceFilePath(path) {
|
|
71
|
+
return `/source/${path.split("/").map(encodeURIComponent).join("/")}`;
|
|
72
|
+
}
|
|
73
|
+
function resolveSourcePath(pathname, manifest, options) {
|
|
74
|
+
for (const entry of manifest.publicEntries) {
|
|
75
|
+
for (const link of entry.links) {
|
|
76
|
+
if (link.kind === "attachment" && link.slug && sourceFilePath(link.slug) === pathname && isSelectedPath(link.slug, options)) {
|
|
77
|
+
return link.slug;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
async function readSource(contentSource, path, options) {
|
|
84
|
+
if (!contentSource || !isSelectedPath(path, options)) return null;
|
|
85
|
+
try {
|
|
86
|
+
const content = await readContentSourceEntry(contentSource, path);
|
|
87
|
+
if (content === null) return null;
|
|
88
|
+
const bytes = typeof content === "string" ? new TextEncoder().encode(content) : content;
|
|
89
|
+
if (bytes.byteLength > (options.maxBytes ?? DEFAULT_MAX_BYTES) || bytes.includes(0))
|
|
90
|
+
return null;
|
|
91
|
+
const text = typeof content === "string" ? content : new TextDecoder("utf-8", { fatal: true }).decode(content);
|
|
92
|
+
return { text, bytes: bytes.byteLength };
|
|
93
|
+
} catch {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function isSelectedPath(path, options) {
|
|
98
|
+
if (!isSafePath(path) || isSecretPath(path)) return false;
|
|
99
|
+
return matchesAny(path, options.include) && !matchesAny(path, options.exclude ?? []);
|
|
100
|
+
}
|
|
101
|
+
function isSafePath(path) {
|
|
102
|
+
return !path.startsWith("/") && !path.includes("\\") && !path.includes("\0") && path.split("/").every((part) => part !== "" && part !== "." && part !== "..");
|
|
103
|
+
}
|
|
104
|
+
function isSecretPath(path) {
|
|
105
|
+
const name = fileName(path).toLowerCase();
|
|
106
|
+
return name === ".env" || name.startsWith(".env.") || SECRET_NAMES.has(name) || SECRET_EXTENSIONS.has(getExtension(path));
|
|
107
|
+
}
|
|
108
|
+
function matchesAny(path, patterns) {
|
|
109
|
+
return patterns.some(
|
|
110
|
+
(pattern) => new RegExp(
|
|
111
|
+
`^${pattern.split("**").map((part) => part.split("*").map(escapeRegex).join("[^/]*")).join(".*")}$`,
|
|
112
|
+
"i"
|
|
113
|
+
).test(path)
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
function escapeRegex(value) {
|
|
117
|
+
return value.replace(/[|\\{}()[\]^$+?.]/g, "\\$&");
|
|
118
|
+
}
|
|
119
|
+
function renderCode(text, path) {
|
|
120
|
+
return `<pre><code class="language-${escapeHtmlAttribute(languageFor(path))}">${escapeHtml(text)}</code></pre>`;
|
|
121
|
+
}
|
|
122
|
+
function selectLines(text, fragment) {
|
|
123
|
+
const match = fragment?.match(/^L(\d+)(?:-L(\d+))?$/i);
|
|
124
|
+
if (!match) return text;
|
|
125
|
+
const start = Number(match[1]);
|
|
126
|
+
const end = Number(match[2] ?? match[1]);
|
|
127
|
+
if (start < 1 || end < start) return text;
|
|
128
|
+
return text.split("\n").slice(start - 1, end).join("\n");
|
|
129
|
+
}
|
|
130
|
+
function languageFor(path) {
|
|
131
|
+
const extension = getExtension(path);
|
|
132
|
+
return extension === "" ? "text" : extension;
|
|
133
|
+
}
|
|
134
|
+
function fileName(path) {
|
|
135
|
+
return path.split("/").at(-1) ?? path;
|
|
136
|
+
}
|
|
137
|
+
function formatBytes(bytes) {
|
|
138
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
139
|
+
return `${(bytes / 1024).toFixed(1)} KB`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// index.ts
|
|
143
|
+
var PLUGIN_NAME = "source-files";
|
|
144
|
+
function sourceFiles(options) {
|
|
145
|
+
return definePlugin({
|
|
146
|
+
name: PLUGIN_NAME,
|
|
147
|
+
order: -30,
|
|
148
|
+
processedContentCache: {
|
|
149
|
+
version: "source-files-v1",
|
|
150
|
+
dependencyMode: "tracked"
|
|
151
|
+
},
|
|
152
|
+
options,
|
|
153
|
+
validateOptions,
|
|
154
|
+
renderers: [
|
|
155
|
+
{
|
|
156
|
+
name: "source-file",
|
|
157
|
+
render: (context) => renderSourceEmbed(context, options)
|
|
158
|
+
}
|
|
159
|
+
],
|
|
160
|
+
pageTypes: [
|
|
161
|
+
{
|
|
162
|
+
id: "source-file",
|
|
163
|
+
paths: (context) => listSourceFiles(context.manifest, context.contentSource, options),
|
|
164
|
+
outputDependencies: (context) => {
|
|
165
|
+
const path = resolveSourcePath(
|
|
166
|
+
context.pathname,
|
|
167
|
+
context.manifest,
|
|
168
|
+
options
|
|
169
|
+
);
|
|
170
|
+
return path ? [{ type: "file", path }] : [];
|
|
171
|
+
},
|
|
172
|
+
resolve: (context) => resolveSourcePage(context, options)
|
|
173
|
+
}
|
|
174
|
+
],
|
|
175
|
+
assets: [
|
|
176
|
+
createStyleAsset(
|
|
177
|
+
PLUGIN_NAME,
|
|
178
|
+
"@riebeckite/plugin-source-files/style.css"
|
|
179
|
+
)
|
|
180
|
+
],
|
|
181
|
+
clientEntries: [
|
|
182
|
+
createClientEntry(
|
|
183
|
+
PLUGIN_NAME,
|
|
184
|
+
"@riebeckite/plugin-source-files/client",
|
|
185
|
+
"initSourceFiles"
|
|
186
|
+
)
|
|
187
|
+
]
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
var sourceFilesPlugin = sourceFiles;
|
|
191
|
+
function validateOptions(options) {
|
|
192
|
+
if (!options || !Array.isArray(options.include) || options.include.length === 0) {
|
|
193
|
+
return [
|
|
194
|
+
{ path: "include", message: "Expected at least one include pattern." }
|
|
195
|
+
];
|
|
196
|
+
}
|
|
197
|
+
const issues = [];
|
|
198
|
+
for (const [key, value] of Object.entries({
|
|
199
|
+
include: options.include,
|
|
200
|
+
exclude: options.exclude
|
|
201
|
+
})) {
|
|
202
|
+
if (value !== void 0 && (!Array.isArray(value) || value.some((item) => typeof item !== "string" || item.trim() === ""))) {
|
|
203
|
+
issues.push({
|
|
204
|
+
path: key,
|
|
205
|
+
message: "Expected an array of non-empty strings."
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (options.maxBytes !== void 0 && (!Number.isInteger(options.maxBytes) || options.maxBytes < 1)) {
|
|
210
|
+
issues.push({ path: "maxBytes", message: "Expected a positive integer." });
|
|
211
|
+
}
|
|
212
|
+
return issues;
|
|
213
|
+
}
|
|
214
|
+
export {
|
|
215
|
+
sourceFilePath,
|
|
216
|
+
sourceFiles,
|
|
217
|
+
sourceFilesPlugin
|
|
218
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type ContentManifest, type ContentSource, type PluginPage, type PluginPageContext, type PluginRenderContext } from "@riebeckite/core";
|
|
2
|
+
import type { SourceFilesOptions } from "../index.js";
|
|
3
|
+
export declare function renderSourceEmbed(context: PluginRenderContext, options: SourceFilesOptions): Promise<string | null>;
|
|
4
|
+
export declare function listSourceFiles(manifest: ContentManifest, contentSource: ContentSource | undefined, options: SourceFilesOptions): Promise<readonly string[]>;
|
|
5
|
+
export declare function resolveSourcePage(context: PluginPageContext, options: SourceFilesOptions): Promise<PluginPage | null>;
|
|
6
|
+
export declare function sourceFilePath(path: string): string;
|
|
7
|
+
export declare function resolveSourcePath(pathname: string, manifest: ContentManifest, options: SourceFilesOptions): string | null;
|
package/package.json
CHANGED
|
@@ -1,6 +1,65 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@riebeckite/plugin-source-files",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.32",
|
|
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.32"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"tsx": "^4.22.4"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"LICENSE",
|
|
34
|
+
"README.md",
|
|
35
|
+
"README_ja.md",
|
|
36
|
+
"dist",
|
|
37
|
+
"style.css"
|
|
38
|
+
],
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "https://github.com/Rerurate514/riebeckite.git",
|
|
42
|
+
"directory": "packages/plugins/source-files"
|
|
43
|
+
},
|
|
44
|
+
"homepage": "https://github.com/Rerurate514/riebeckite#readme",
|
|
45
|
+
"bugs": {
|
|
46
|
+
"url": "https://github.com/Rerurate514/riebeckite/issues"
|
|
47
|
+
},
|
|
48
|
+
"description": "Publish selected vault source files as syntax-highlighted pages and wikilink embeds.",
|
|
49
|
+
"keywords": [
|
|
50
|
+
"riebeckite",
|
|
51
|
+
"plugin",
|
|
52
|
+
"source-files",
|
|
53
|
+
"obsidian"
|
|
54
|
+
],
|
|
55
|
+
"engines": {
|
|
56
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
57
|
+
},
|
|
58
|
+
"sideEffects": [
|
|
59
|
+
"./style.css"
|
|
60
|
+
],
|
|
61
|
+
"scripts": {
|
|
62
|
+
"build": "node ../../../scripts/build_package.mjs",
|
|
63
|
+
"test": "node --import tsx --test \"test/*.test.ts\""
|
|
64
|
+
}
|
|
6
65
|
}
|
package/style.css
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
.rr-source-file__header {
|
|
2
|
+
display: grid;
|
|
3
|
+
gap: 1rem;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
.rr-source-file__metadata {
|
|
7
|
+
display: flex;
|
|
8
|
+
flex-wrap: wrap;
|
|
9
|
+
gap: 0.5rem 1.5rem;
|
|
10
|
+
margin: 0;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
.rr-source-file__metadata div {
|
|
14
|
+
display: flex;
|
|
15
|
+
gap: 0.35rem;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
.rr-source-file__metadata dt {
|
|
19
|
+
font-weight: 600;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
.rr-source-file__metadata dd {
|
|
23
|
+
margin: 0;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
.rr-source-file__actions {
|
|
27
|
+
display: flex;
|
|
28
|
+
gap: 0.75rem;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
.rr-source-file-embed__title {
|
|
32
|
+
display: inline-block;
|
|
33
|
+
margin-bottom: 0.5rem;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
.rr-source-file .rr-source-file__line--target {
|
|
37
|
+
background: color-mix(in srgb, currentColor 14%, transparent);
|
|
38
|
+
outline: 1px solid color-mix(in srgb, currentColor 28%, transparent);
|
|
39
|
+
outline-offset: -1px;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
.rr-source-file .rr-source-file__line--target:focus-visible {
|
|
43
|
+
outline: 2px solid currentColor;
|
|
44
|
+
outline-offset: -2px;
|
|
45
|
+
}
|