@riebeckite/plugin-shortcodes 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 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,110 @@
1
+ # @riebeckite/plugin-shortcodes
2
+
3
+ A generic shortcode system for Markdown, built on `remark-directive`.
4
+
5
+ [日本語](./README_ja.md)
6
+
7
+ ## Overview
8
+
9
+ `shortcodes()` turns `remark-directive` syntax into HTML at Markdown time.
10
+ It supports leaf shortcodes (`::name[label]{key=value}`) and container
11
+ shortcodes (`:::name[label]{attrs}` … `:::`), and ships an extensible registry
12
+ so projects can add their own renderers on top of the built-ins.
13
+
14
+ Output is rendered into `mdast` `html` nodes and wrapped in a `span` (leaf) or
15
+ `div` (container) carrying `rb-shortcode rb-shortcode--<name>`.
16
+
17
+ ## Usage
18
+
19
+ ```ts
20
+ import { defineConfig } from "@riebeckite/core";
21
+ import { shortcodes } from "@riebeckite/plugin-shortcodes";
22
+
23
+ export default defineConfig({
24
+ // ...
25
+ plugins: [shortcodes()],
26
+ });
27
+ ```
28
+
29
+ Styles ship in `style.css`.
30
+
31
+ ## Syntax
32
+
33
+ ```
34
+ ::kbd[Ctrl+S]
35
+
36
+ ::youtube[id=dQw4w9WgXcQ]
37
+
38
+ :::note[Heads up]{type=warning}
39
+ Container bodies support **Markdown**.
40
+ :::
41
+ ```
42
+
43
+ - `::name[label]{key=value}` — leaf shortcode, rendered inline.
44
+ - `:::name[label]{key=value}` … `:::` — container shortcode; the body is
45
+ rendered by the normal Markdown pipeline.
46
+
47
+ Unknown shortcodes emit a `shortcodes-unknown` diagnostic and stay on the page
48
+ as escaped text. Malformed attributes emit `shortcodes-invalid` and are
49
+ ignored.
50
+
51
+ ## Built-in shortcodes
52
+
53
+ | Name | Kind | Attributes | Output |
54
+ | ---- | ---- | ---------- | ------ |
55
+ | `figure` | leaf / container | `src`/`url`/`image`, `alt`, `caption`, `width`, `height` | `<figure>` with image and caption |
56
+ | `youtube` | leaf | `id`/`video` or `url`/`src`, `title` | Privacy-friendly `youtube-nocookie.com` embed |
57
+ | `vimeo` | leaf | `id`/`video` or `url`/`src`, `title` | `player.vimeo.com` embed (`dnt=1`) |
58
+ | `gist` | leaf | `user` + `id`, or `url`, `file` | GitHub Gist embed with `<noscript>` link |
59
+ | `kbd` | leaf | label or `keys` | `<kbd>` elements split on `+` |
60
+ | `badge` | leaf | label or `text`, `variant`/`type`/`color`, `title` | Inline badge |
61
+ | `details` | container | label or `summary`, `open` | `<details>` disclosure |
62
+ | `spoiler` | container | label or `summary`, `open` | `details` alias with a different class |
63
+ | `note` | leaf / container | label or `title`, `type`/`variant` | Callout box |
64
+ | `callout` | leaf / container | label or `title`, `type`/`variant` | `note` alias with a different class |
65
+ | `link-card` | leaf | `url`/`href`, `title`, `description`, `image`/`icon` | Link preview card |
66
+ | `file` | leaf | `url`/`src`/`path`, `name`/`label`, `size` | Download link |
67
+
68
+ ## Options
69
+
70
+ | Option | Type | Default | Description |
71
+ | ------ | ---- | ------- | ----------- |
72
+ | `className` | `string` | `"rb-shortcode"` | Root CSS class of every wrapper |
73
+ | `language` | `string` | – | BCP-47 tag applied to wrappers as `lang` |
74
+ | `builtins` | `boolean` | `true` | Register the built-in renderers |
75
+ | `shortcodes` | `Record<string, ShortcodeRenderer>` | `{}` | Custom renderers, merged over the built-ins |
76
+
77
+ ## Custom renderers
78
+
79
+ ```ts
80
+ import { shortcodes } from "@riebeckite/plugin-shortcodes";
81
+
82
+ shortcodes({
83
+ shortcodes: {
84
+ mark: ({ label, attributes }) => `<mark>${label}</mark>`,
85
+ },
86
+ });
87
+ ```
88
+
89
+ A `ShortcodeRenderer` receives `{ name, label, attributes, childrenHtml,
90
+ context, container }` and returns a string. For container shortcodes the plugin
91
+ wraps the result in `<div class="rb-shortcode rb-shortcode--<name>">` and
92
+ replaces `childrenHtml` with the rendered body, so container renderers must echo
93
+ `input.childrenHtml` where the body belongs. Renderers are synchronous and must
94
+ escape any user input themselves — `escapeHtml` and `escapeHtmlAttribute` are
95
+ re-exported from `@riebeckite/core`.
96
+
97
+ ## Exports
98
+
99
+ - `shortcodes(options?)` / `shortcodesPlugin` — plugin factory
100
+ - `remarkShortcodes(options?)` — standalone remark transform
101
+ - `renderShortcode(request, options)` — render a single shortcode
102
+ - `resolveShortcodeOptions(options?)` — normalize options
103
+ - `builtinShortcodes`, `builtinShortcodeNames` — the built-in registry
104
+ - Types: `ShortcodeRenderer`, `ShortcodeOptions`, `ResolvedShortcodeOptions`,
105
+ `ShortcodeRenderInput`, `ShortcodeRenderRequest`, `ShortcodeAttributes`,
106
+ `RemarkShortcodesOptions`
107
+
108
+ ## See also
109
+
110
+ - [Plugin guide](../../docs/plugins_en.md)
package/README_ja.md ADDED
@@ -0,0 +1,111 @@
1
+ # @riebeckite/plugin-shortcodes
2
+
3
+ `remark-directive` を土台にした汎用ショートコード機能です。
4
+
5
+ [English](./README.md)
6
+
7
+ ## 概要
8
+
9
+ `shortcodes()` は `remark-directive` の記法を Markdown 処理時に HTML へ変換
10
+ します。リーフ型(`::name[label]{key=value}`)とコンテナ型
11
+ (`:::name[label]{attrs}` … `:::`)の両方に対応し、組み込みレンダラーに
12
+ 独自のレンダラーを重ねられる拡張可能なレジストリを備えます。
13
+
14
+ 出力は `mdast` の `html` ノードとして書き出され、リーフは `span`、コンテナは
15
+ `div` で `rb-shortcode rb-shortcode--<name>` を付けてラップされます。
16
+
17
+ ## 使い方
18
+
19
+ ```ts
20
+ import { defineConfig } from "@riebeckite/core";
21
+ import { shortcodes } from "@riebeckite/plugin-shortcodes";
22
+
23
+ export default defineConfig({
24
+ // ...
25
+ plugins: [shortcodes()],
26
+ });
27
+ ```
28
+
29
+ スタイルは `style.css` に同梱されています。
30
+
31
+ ## 記法
32
+
33
+ ```
34
+ ::kbd[Ctrl+S]
35
+
36
+ ::youtube[id=dQw4w9WgXcQ]
37
+
38
+ :::note[補足]{type=warning}
39
+ コンテナの本文では **Markdown** が使えます。
40
+ :::
41
+ ```
42
+
43
+ - `::name[label]{key=value}` — リーフ型。インラインで描画されます。
44
+ - `:::name[label]{key=value}` … `:::` — コンテナ型。本文は通常の Markdown
45
+ パイプラインで描画されます。
46
+
47
+ 未知のショートコードは `shortcodes-unknown` 診断を出し、エスケープした
48
+ テキストとしてページに残します。不正な属性は `shortcodes-invalid` を出して
49
+ 無視します。
50
+
51
+ ## 組み込みショートコード
52
+
53
+ | 名前 | 種類 | 属性 | 出力 |
54
+ | ---- | ---- | ---- | ---- |
55
+ | `figure` | リーフ / コンテナ | `src`/`url`/`image`, `alt`, `caption`, `width`, `height` | 画像とキャプションの `<figure>` |
56
+ | `youtube` | リーフ | `id`/`video` または `url`/`src`, `title` | `youtube-nocookie.com` のプライバシー配慮埋め込み |
57
+ | `vimeo` | リーフ | `id`/`video` または `url`/`src`, `title` | `player.vimeo.com` 埋め込み(`dnt=1`) |
58
+ | `gist` | リーフ | `user` + `id`、または `url`, `file` | `<noscript>` リンク付きの Gist 埋め込み |
59
+ | `kbd` | リーフ | ラベルまたは `keys` | `+` で分割した `<kbd>` |
60
+ | `badge` | リーフ | ラベルまたは `text`, `variant`/`type`/`color`, `title` | インラインバッジ |
61
+ | `details` | コンテナ | ラベルまたは `summary`, `open` | `<details>` 折りたたみ |
62
+ | `spoiler` | コンテナ | ラベルまたは `summary`, `open` | クラス違いの `details` エイリアス |
63
+ | `note` | リーフ / コンテナ | ラベルまたは `title`, `type`/`variant` | コールアウト |
64
+ | `callout` | リーフ / コンテナ | ラベルまたは `title`, `type`/`variant` | クラス違いの `note` エイリアス |
65
+ | `link-card` | リーフ | `url`/`href`, `title`, `description`, `image`/`icon` | リンクプレビューカード |
66
+ | `file` | リーフ | `url`/`src`/`path`, `name`/`label`, `size` | ダウンロードリンク |
67
+
68
+ ## オプション
69
+
70
+ | オプション | 型 | 既定値 | 説明 |
71
+ | ---------- | -- | ------ | ---- |
72
+ | `className` | `string` | `"rb-shortcode"` | すべてのラッパーのルート CSS クラス |
73
+ | `language` | `string` | – | ラッパーの `lang` に付与する BCP-47 タグ |
74
+ | `builtins` | `boolean` | `true` | 組み込みレンダラーを登録する |
75
+ | `shortcodes` | `Record<string, ShortcodeRenderer>` | `{}` | 組み込みに重ねる独自レンダラー |
76
+
77
+ ## 独自レンダラー
78
+
79
+ ```ts
80
+ import { shortcodes } from "@riebeckite/plugin-shortcodes";
81
+
82
+ shortcodes({
83
+ shortcodes: {
84
+ mark: ({ label, attributes }) => `<mark>${label}</mark>`,
85
+ },
86
+ });
87
+ ```
88
+
89
+ `ShortcodeRenderer` は `{ name, label, attributes, childrenHtml, context,
90
+ container }` を受け取り、文字列を返します。コンテナ型では結果が
91
+ `<div class="rb-shortcode rb-shortcode--<name>">` でラップされ、
92
+ `childrenHtml` の位置に本文が差し込まれます。コンテナ用レンダラーは本文を
93
+ 置きたい位置で `input.childrenHtml` をそのまま出力してください。レンダラーは
94
+ 同期関数で、ユーザー入力のエスケープは各自で行う必要があります
95
+ (`@riebeckite/core` の `escapeHtml` / `escapeHtmlAttribute` を再エクスポート
96
+ しています)。
97
+
98
+ ## エクスポート
99
+
100
+ - `shortcodes(options?)` / `shortcodesPlugin` — プラグインファクトリ
101
+ - `remarkShortcodes(options?)` — 単体で使える remark トランスフォーム
102
+ - `renderShortcode(request, options)` — 単一ショートコードの描画
103
+ - `resolveShortcodeOptions(options?)` — オプションの正規化
104
+ - `builtinShortcodes`, `builtinShortcodeNames` — 組み込みレジストリ
105
+ - 型: `ShortcodeRenderer`, `ShortcodeOptions`, `ResolvedShortcodeOptions`,
106
+ `ShortcodeRenderInput`, `ShortcodeRenderRequest`, `ShortcodeAttributes`,
107
+ `RemarkShortcodesOptions`
108
+
109
+ ## 関連
110
+
111
+ - [プラグインガイド](../../docs/plugins_ja.md)
@@ -0,0 +1,10 @@
1
+ import { builtinShortcodeNames, builtinShortcodes } from "./src/builtins.js";
2
+ import { DEFAULT_SHORTCODE_CLASS_NAME, SHORTCODE_CHILDREN_MARKER, createShortcodeRenderContext, renderShortcode, resolveShortcodeOptions } from "./src/render.js";
3
+ import { DIAGNOSTIC_INVALID, DIAGNOSTIC_UNKNOWN, SHORTCODES_SOURCE, remarkShortcodes } from "./src/remark.js";
4
+ import type { ShortcodeOptions } from "./src/types.js";
5
+ export type { DirectiveNode, RemarkShortcodesOptions, ResolvedShortcodeOptions, ShortcodeAttributes, ShortcodeOptions, ShortcodeRenderInput, ShortcodeRenderer, ShortcodeRenderRequest, } from "./src/types.js";
6
+ export { builtinShortcodeNames, builtinShortcodes };
7
+ export { DEFAULT_SHORTCODE_CLASS_NAME, SHORTCODE_CHILDREN_MARKER, createShortcodeRenderContext, renderShortcode, resolveShortcodeOptions, };
8
+ export { DIAGNOSTIC_INVALID, DIAGNOSTIC_UNKNOWN, SHORTCODES_SOURCE, remarkShortcodes, };
9
+ export declare function shortcodes(options?: ShortcodeOptions): import("@riebeckite/core").RiebeckitePlugin<ShortcodeOptions>;
10
+ export declare const shortcodesPlugin: typeof shortcodes;
package/dist/index.js ADDED
@@ -0,0 +1,509 @@
1
+ // index.ts
2
+ import { createStyleAsset, definePlugin } from "@riebeckite/core";
3
+
4
+ // src/builtins.ts
5
+ import { escapeHtml, escapeHtmlAttribute } from "@riebeckite/core";
6
+ var YOUTUBE_ID_PATTERN = /(?:youtu\.be\/|v=|embed\/|shorts\/|live\/)([A-Za-z0-9_-]{6,})/;
7
+ var VIMEO_ID_PATTERN = /vimeo\.com\/(?:video\/)?(\d+)/;
8
+ var GIST_URL_PATTERN = /gist\.github\.com\/([^/]+)\/([0-9a-f]+)/i;
9
+ var KEY_TOKEN_PATTERN = /[^a-z0-9-]+/g;
10
+ function first(attributes, keys) {
11
+ for (const key of keys) {
12
+ const value = attributes[key];
13
+ if (value !== void 0 && value !== "") return value;
14
+ }
15
+ return "";
16
+ }
17
+ function token(value, fallback) {
18
+ const normalized = value.toLowerCase().replace(KEY_TOKEN_PATTERN, "-").replace(/^-+|-+$/g, "");
19
+ return normalized || fallback;
20
+ }
21
+ function renderFigure(input) {
22
+ const source = first(input.attributes, ["src", "url", "image", "path"]);
23
+ const caption = first(input.attributes, ["caption"]) || input.label;
24
+ const alt = first(input.attributes, ["alt"]) || caption;
25
+ const body = input.childrenHtml ?? "";
26
+ if (!source) {
27
+ return `<span class="rb-shortcode__figure-fallback">${escapeHtml(
28
+ caption || "figure"
29
+ )}</span>`;
30
+ }
31
+ const width = first(input.attributes, ["width"]);
32
+ const height = first(input.attributes, ["height"]);
33
+ const dimension = (width ? ` width="${escapeHtmlAttribute(width)}"` : "") + (height ? ` height="${escapeHtmlAttribute(height)}"` : "");
34
+ const captionHtml = caption ? `<figcaption class="rb-shortcode__caption">${escapeHtml(
35
+ caption
36
+ )}</figcaption>` : "";
37
+ return `<figure class="rb-shortcode__figure">
38
+ <img class="rb-shortcode__image" src="${escapeHtmlAttribute(
39
+ source
40
+ )}" alt="${escapeHtmlAttribute(alt)}"${dimension} loading="lazy" decoding="async">
41
+ ${body}
42
+ ${captionHtml}
43
+ </figure>`;
44
+ }
45
+ function renderYouTube(input) {
46
+ const id = first(input.attributes, ["id", "video"]) || (first(input.attributes, ["url", "src"]).match(YOUTUBE_ID_PATTERN)?.[1] ?? "");
47
+ if (!id) {
48
+ return `<span class="rb-shortcode__video-fallback">${escapeHtml(
49
+ input.label || "youtube"
50
+ )}</span>`;
51
+ }
52
+ const title = first(input.attributes, ["title"]) || input.label || "YouTube video";
53
+ return `<div class="rb-shortcode__video">
54
+ <iframe class="rb-shortcode__iframe" src="https://www.youtube-nocookie.com/embed/${escapeHtmlAttribute(
55
+ id
56
+ )}" title="${escapeHtmlAttribute(
57
+ title
58
+ )}" loading="lazy" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>
59
+ </div>`;
60
+ }
61
+ function renderVimeo(input) {
62
+ const id = first(input.attributes, ["id", "video"]) || (first(input.attributes, ["url", "src"]).match(VIMEO_ID_PATTERN)?.[1] ?? "");
63
+ if (!id) {
64
+ return `<span class="rb-shortcode__video-fallback">${escapeHtml(
65
+ input.label || "vimeo"
66
+ )}</span>`;
67
+ }
68
+ const title = first(input.attributes, ["title"]) || input.label || "Vimeo video";
69
+ return `<div class="rb-shortcode__video">
70
+ <iframe class="rb-shortcode__iframe" src="https://player.vimeo.com/video/${escapeHtmlAttribute(
71
+ id
72
+ )}?dnt=1" title="${escapeHtmlAttribute(
73
+ title
74
+ )}" loading="lazy" allow="fullscreen; picture-in-picture" allowfullscreen></iframe>
75
+ </div>`;
76
+ }
77
+ function renderGist(input) {
78
+ let user = first(input.attributes, ["user", "username"]);
79
+ let id = first(input.attributes, ["id", "gist"]);
80
+ if (!user || !id) {
81
+ const match = first(input.attributes, ["url", "src"]).match(
82
+ GIST_URL_PATTERN
83
+ );
84
+ if (match) {
85
+ user = match[1];
86
+ id = match[2];
87
+ }
88
+ }
89
+ if (!user || !id) {
90
+ return `<span class="rb-shortcode__gist-fallback">${escapeHtml(
91
+ input.label || "gist"
92
+ )}</span>`;
93
+ }
94
+ const label = input.label || "Gist";
95
+ const gistUrl = `https://gist.github.com/${escapeHtmlAttribute(
96
+ user
97
+ )}/${escapeHtmlAttribute(id)}`;
98
+ const file = first(input.attributes, ["file"]);
99
+ const scriptUrl = `${gistUrl}.js${file ? `?file=${escapeHtmlAttribute(file)}` : ""}`;
100
+ return `<div class="rb-shortcode__gist">
101
+ <script src="${scriptUrl}"></script>
102
+ <noscript><a class="rb-shortcode__gist-link" href="${gistUrl}">${escapeHtml(
103
+ label
104
+ )}</a></noscript>
105
+ </div>`;
106
+ }
107
+ function renderKbd(input) {
108
+ const keys = input.label || first(input.attributes, ["keys", "key", "text"]) || "";
109
+ const parts = keys.split("+").map((part) => part.trim());
110
+ return parts.filter((part) => part.length > 0).map(
111
+ (part) => `<kbd class="rb-shortcode__kbd">${escapeHtml(part)}</kbd>`
112
+ ).join('<span class="rb-shortcode__kbd-separator">+</span>');
113
+ }
114
+ function renderBadge(input) {
115
+ const label = input.label || first(input.attributes, ["text", "label"]) || "badge";
116
+ const variant = token(
117
+ first(input.attributes, ["variant", "type", "color"]),
118
+ "default"
119
+ );
120
+ const title = first(input.attributes, ["title"]);
121
+ const titleAttribute = title ? ` title="${escapeHtmlAttribute(title)}"` : "";
122
+ return `<span class="rb-shortcode__badge rb-shortcode__badge--${escapeHtmlAttribute(
123
+ variant
124
+ )}"${titleAttribute}>${escapeHtml(label)}</span>`;
125
+ }
126
+ function renderDetails(defaultSummary) {
127
+ return (input) => {
128
+ const summary = input.label || first(input.attributes, ["summary"]) || defaultSummary;
129
+ const open = first(input.attributes, ["open"]) !== "" && first(input.attributes, ["open"]) !== "false" ? " open" : "";
130
+ return `<details class="rb-shortcode__details"${open}>
131
+ <summary class="rb-shortcode__summary">${escapeHtml(summary)}</summary>
132
+ <div class="rb-shortcode__details-body">${input.childrenHtml ?? ""}</div>
133
+ </details>`;
134
+ };
135
+ }
136
+ function renderNote(defaultType) {
137
+ return (input) => {
138
+ const type = token(
139
+ first(input.attributes, ["type", "variant"]) || defaultType,
140
+ defaultType
141
+ );
142
+ const title = first(input.attributes, ["title"]) || (input.container ? input.label : "");
143
+ const heading = title ? `<p class="rb-shortcode__note-title">${escapeHtml(title)}</p>` : "";
144
+ const body = input.childrenHtml ?? (input.label && !input.container ? `<p class="rb-shortcode__note-text">${escapeHtml(input.label)}</p>` : "");
145
+ return `<div class="rb-shortcode__note rb-shortcode__note--${escapeHtmlAttribute(
146
+ type
147
+ )}">
148
+ ${heading}
149
+ <div class="rb-shortcode__note-body">${body}</div>
150
+ </div>`;
151
+ };
152
+ }
153
+ function renderLinkCard(input) {
154
+ const url = first(input.attributes, ["url", "href"]);
155
+ if (!url) {
156
+ return `<span class="rb-shortcode__link-card-fallback">${escapeHtml(
157
+ input.label || "link-card"
158
+ )}</span>`;
159
+ }
160
+ const title = first(input.attributes, ["title"]) || input.label || url;
161
+ const description = first(input.attributes, ["description"]);
162
+ const image = first(input.attributes, ["image", "icon"]);
163
+ const imageHtml = image ? `<img class="rb-shortcode__link-card-image" src="${escapeHtmlAttribute(
164
+ image
165
+ )}" alt="" loading="lazy" decoding="async">` : "";
166
+ const descriptionHtml = description ? `<span class="rb-shortcode__link-card-description">${escapeHtml(
167
+ description
168
+ )}</span>` : "";
169
+ return `<a class="rb-shortcode__link-card" href="${escapeHtmlAttribute(
170
+ url
171
+ )}" target="_blank" rel="noopener noreferrer">
172
+ ${imageHtml}
173
+ <span class="rb-shortcode__link-card-body">
174
+ <span class="rb-shortcode__link-card-title">${escapeHtml(title)}</span>
175
+ ${descriptionHtml}
176
+ <span class="rb-shortcode__link-card-url">${escapeHtml(url)}</span>
177
+ </span>
178
+ </a>`;
179
+ }
180
+ function renderFile(input) {
181
+ const url = first(input.attributes, ["url", "src", "path", "href"]);
182
+ const label = first(input.attributes, ["name", "label"]) || input.label || url;
183
+ if (!url) {
184
+ return `<span class="rb-shortcode__file-fallback">${escapeHtml(
185
+ label || "file"
186
+ )}</span>`;
187
+ }
188
+ const size = first(input.attributes, ["size", "bytes"]);
189
+ const sizeHtml = size ? `<span class="rb-shortcode__file-size">${escapeHtml(size)}</span>` : "";
190
+ return `<a class="rb-shortcode__file" href="${escapeHtmlAttribute(
191
+ url
192
+ )}" download>
193
+ <span class="rb-shortcode__file-name">${escapeHtml(label)}</span>
194
+ ${sizeHtml}
195
+ </a>`;
196
+ }
197
+ var builtinShortcodes = {
198
+ figure: renderFigure,
199
+ youtube: renderYouTube,
200
+ vimeo: renderVimeo,
201
+ gist: renderGist,
202
+ kbd: renderKbd,
203
+ badge: renderBadge,
204
+ details: renderDetails("Details"),
205
+ spoiler: renderDetails("Spoiler"),
206
+ note: renderNote("note"),
207
+ callout: renderNote("callout"),
208
+ "link-card": renderLinkCard,
209
+ file: renderFile
210
+ };
211
+ var builtinShortcodeNames = Object.keys(
212
+ builtinShortcodes
213
+ );
214
+
215
+ // src/render.ts
216
+ import {
217
+ createUnavailableGeneratedOutputSink,
218
+ escapeHtmlAttribute as escapeHtmlAttribute2,
219
+ NoopLogger,
220
+ NoopTracer
221
+ } from "@riebeckite/core";
222
+ var DEFAULT_SHORTCODE_CLASS_NAME = "rb-shortcode";
223
+ var SHORTCODE_CHILDREN_MARKER = "\0rb-shortcodes:children\0";
224
+ var UNAVAILABLE_CACHE = {
225
+ async get() {
226
+ return void 0;
227
+ },
228
+ async set() {
229
+ },
230
+ async delete() {
231
+ },
232
+ async clear() {
233
+ }
234
+ };
235
+ function resolveShortcodeOptions(options = {}) {
236
+ const builtins = options.builtins !== false;
237
+ const custom = options.shortcodes ?? {};
238
+ const shortcodes2 = builtins ? { ...builtinShortcodes, ...custom } : { ...custom };
239
+ const resolved = {
240
+ className: options.className ?? DEFAULT_SHORTCODE_CLASS_NAME,
241
+ builtins,
242
+ shortcodes: shortcodes2
243
+ };
244
+ if (options.language !== void 0) resolved.language = options.language;
245
+ return resolved;
246
+ }
247
+ function createShortcodeRenderContext(input) {
248
+ return {
249
+ contentIndex: input.contentIndex ?? /* @__PURE__ */ new Map(),
250
+ diagnostics: [],
251
+ cache: UNAVAILABLE_CACHE,
252
+ output: createUnavailableGeneratedOutputSink(),
253
+ logger: new NoopLogger(),
254
+ tracer: new NoopTracer(),
255
+ contentSource: input.contentSource,
256
+ kind: `shortcode:${input.name}`,
257
+ path: `shortcode:${input.name}`,
258
+ raw: input.raw ?? input.label ?? "",
259
+ label: input.label ?? "",
260
+ url: input.url ?? "",
261
+ embed: false
262
+ };
263
+ }
264
+ function renderShortcode(request, options) {
265
+ const renderer = options.shortcodes[request.name];
266
+ if (!renderer) return "";
267
+ const attributes = request.attributes ?? {};
268
+ const context = request.context ?? createShortcodeRenderContext({
269
+ name: request.name,
270
+ label: request.label,
271
+ raw: request.label,
272
+ url: firstUrl(attributes)
273
+ });
274
+ const container = request.container ?? false;
275
+ const input = {
276
+ name: request.name,
277
+ label: request.label ?? "",
278
+ attributes,
279
+ context,
280
+ container
281
+ };
282
+ if (request.childrenHtml !== void 0) {
283
+ input.childrenHtml = request.childrenHtml;
284
+ }
285
+ const body = renderer(input);
286
+ return wrapShortcode(body, request.name, container, options);
287
+ }
288
+ function wrapShortcode(body, name, container, options) {
289
+ const tag = container ? "div" : "span";
290
+ const classes = `${options.className} ${options.className}--${name}`;
291
+ const languageAttribute = options.language === void 0 ? "" : ` lang="${escapeHtmlAttribute2(options.language)}"`;
292
+ return `<${tag} class="${escapeHtmlAttribute2(
293
+ classes
294
+ )}"${languageAttribute}>${body}</${tag}>`;
295
+ }
296
+ function firstUrl(attributes) {
297
+ for (const key of ["url", "src", "href", "path"]) {
298
+ const value = attributes[key];
299
+ if (value !== void 0 && value !== "") return value;
300
+ }
301
+ return "";
302
+ }
303
+
304
+ // src/remark.ts
305
+ import { escapeHtml as escapeHtml2, escapeHtmlAttribute as escapeHtmlAttribute3 } from "@riebeckite/core";
306
+ import { visit } from "unist-util-visit";
307
+ var SHORTCODES_SOURCE = "@riebeckite/plugin-shortcodes";
308
+ var DIAGNOSTIC_UNKNOWN = "shortcodes-unknown";
309
+ var DIAGNOSTIC_INVALID = "shortcodes-invalid";
310
+ var ATTRIBUTE_NAME_PATTERN = /^[A-Za-z_][\w-]*$/;
311
+ function remarkShortcodes(options = {}) {
312
+ const resolved = resolveShortcodeOptions(options);
313
+ return (tree, file) => {
314
+ const targets = [];
315
+ visit(
316
+ tree,
317
+ ["containerDirective", "leafDirective"],
318
+ (node, index, parent) => {
319
+ if (parent === void 0 || index === void 0) return;
320
+ targets.push({ node, index, parent });
321
+ }
322
+ );
323
+ for (const target of targets.reverse()) {
324
+ transformDirective(target, resolved, options, file);
325
+ }
326
+ };
327
+ }
328
+ function transformDirective(target, resolved, options, file) {
329
+ const { node, index, parent } = target;
330
+ const name = node.name;
331
+ const container = node.type === "containerDirective";
332
+ const { label, children } = extractLabel(node);
333
+ const attributes = normalizeAttributes(node, file);
334
+ const childrenHtml = container ? SHORTCODE_CHILDREN_MARKER : void 0;
335
+ const html = renderShortcode(
336
+ {
337
+ name,
338
+ label,
339
+ attributes,
340
+ childrenHtml,
341
+ container,
342
+ context: createShortcodeRenderContext({
343
+ name,
344
+ label,
345
+ raw: label,
346
+ url: firstUrl2(attributes),
347
+ contentIndex: options.contentIndex,
348
+ contentSource: options.contentSource
349
+ })
350
+ },
351
+ resolved
352
+ );
353
+ if (!html) {
354
+ report(
355
+ file,
356
+ DIAGNOSTIC_UNKNOWN,
357
+ `Unknown shortcode "::${name}" was left as escaped text.`,
358
+ node
359
+ );
360
+ parent.children.splice(
361
+ index,
362
+ 1,
363
+ ...createFallback(resolved.className, name, label, attributes, container, children)
364
+ );
365
+ return;
366
+ }
367
+ if (!container) {
368
+ parent.children.splice(index, 1, createHtml(html));
369
+ return;
370
+ }
371
+ const [before, after] = splitOnChildren(html);
372
+ parent.children.splice(
373
+ index,
374
+ 1,
375
+ createHtml(before),
376
+ ...children,
377
+ createHtml(after)
378
+ );
379
+ }
380
+ function splitOnChildren(html) {
381
+ if (html.includes(SHORTCODE_CHILDREN_MARKER)) {
382
+ const [before, ...rest] = html.split(SHORTCODE_CHILDREN_MARKER);
383
+ return [before, rest.join(SHORTCODE_CHILDREN_MARKER)];
384
+ }
385
+ const closeIndex = html.lastIndexOf("</div>");
386
+ if (closeIndex < 0) return [html, ""];
387
+ return [html.slice(0, closeIndex), html.slice(closeIndex)];
388
+ }
389
+ function extractLabel(node) {
390
+ if (node.type === "containerDirective") {
391
+ const [firstChild, ...rest] = node.children;
392
+ if (isDirectiveLabel(firstChild)) {
393
+ return { label: textContent(firstChild), children: rest };
394
+ }
395
+ return { label: "", children: node.children };
396
+ }
397
+ return { label: textContent(node), children: [] };
398
+ }
399
+ function isDirectiveLabel(node) {
400
+ if (node === null || typeof node !== "object") return false;
401
+ const candidate = node;
402
+ return candidate.type === "paragraph" && candidate.data?.directiveLabel === true;
403
+ }
404
+ function textContent(node) {
405
+ if (node === null || typeof node !== "object") return "";
406
+ const value = node.value;
407
+ if (typeof value === "string") return value;
408
+ const children = node.children;
409
+ if (!Array.isArray(children)) return "";
410
+ return children.map((child) => textContent(child)).join("");
411
+ }
412
+ function normalizeAttributes(node, file) {
413
+ const raw = node.attributes ?? {};
414
+ const attributes = {};
415
+ for (const [key, value] of Object.entries(raw)) {
416
+ if (typeof value !== "string" || !ATTRIBUTE_NAME_PATTERN.test(key)) {
417
+ report(
418
+ file,
419
+ DIAGNOSTIC_INVALID,
420
+ `Ignoring malformed attribute "${key}" on shortcode "::${node.name}".`,
421
+ node
422
+ );
423
+ continue;
424
+ }
425
+ attributes[key] = value;
426
+ }
427
+ return attributes;
428
+ }
429
+ function createFallback(className, name, label, attributes, container, children) {
430
+ const classes = `${className} ${className}--unknown`;
431
+ const dataName = ` data-shortcode-name="${escapeHtmlAttribute3(name)}"`;
432
+ const directive = escapeHtml2(sourceText(name, label, attributes, container));
433
+ if (container) {
434
+ return [
435
+ createHtml(
436
+ `<div class="${escapeHtmlAttribute3(classes)}"${dataName}>${directive}`
437
+ ),
438
+ ...children,
439
+ createHtml("</div>")
440
+ ];
441
+ }
442
+ return [
443
+ createHtml(
444
+ `<span class="${escapeHtmlAttribute3(classes)}"${dataName}>${directive}</span>`
445
+ )
446
+ ];
447
+ }
448
+ function sourceText(name, label, attributes, container) {
449
+ const fence = container ? ":::" : "::";
450
+ const labelPart = label ? `[${label}]` : "";
451
+ return `${fence}${name}${labelPart}${attributesText(attributes)}`;
452
+ }
453
+ function attributesText(attributes) {
454
+ const entries = Object.entries(attributes);
455
+ if (entries.length === 0) return "";
456
+ const body = entries.map(([key, value]) => `${key}="${value}"`).join(" ");
457
+ return `{${body}}`;
458
+ }
459
+ function firstUrl2(attributes) {
460
+ for (const key of ["url", "src", "href", "path"]) {
461
+ const value = attributes[key];
462
+ if (value !== void 0 && value !== "") return value;
463
+ }
464
+ return "";
465
+ }
466
+ function report(file, ruleId, reason, node) {
467
+ const target = file;
468
+ target?.message(reason, {
469
+ place: node,
470
+ ruleId,
471
+ source: SHORTCODES_SOURCE
472
+ });
473
+ }
474
+ function createHtml(value) {
475
+ return { type: "html", value };
476
+ }
477
+
478
+ // index.ts
479
+ var PLUGIN_NAME = "shortcodes";
480
+ function shortcodes(options = {}) {
481
+ return definePlugin({
482
+ name: PLUGIN_NAME,
483
+ options,
484
+ extendMarkdownPipeline: (pipeline, context) => {
485
+ pipeline.use(remarkShortcodes, {
486
+ ...options,
487
+ contentIndex: context.contentIndex,
488
+ contentSource: context.contentSource
489
+ });
490
+ },
491
+ assets: [createStyleAsset(PLUGIN_NAME)]
492
+ });
493
+ }
494
+ var shortcodesPlugin = shortcodes;
495
+ export {
496
+ DEFAULT_SHORTCODE_CLASS_NAME,
497
+ DIAGNOSTIC_INVALID,
498
+ DIAGNOSTIC_UNKNOWN,
499
+ SHORTCODES_SOURCE,
500
+ SHORTCODE_CHILDREN_MARKER,
501
+ builtinShortcodeNames,
502
+ builtinShortcodes,
503
+ createShortcodeRenderContext,
504
+ remarkShortcodes,
505
+ renderShortcode,
506
+ resolveShortcodeOptions,
507
+ shortcodes,
508
+ shortcodesPlugin
509
+ };
@@ -0,0 +1,4 @@
1
+ import type { ShortcodeRenderInput } from "./types.js";
2
+ /** Built-in renderers keyed by directive name. */
3
+ export declare const builtinShortcodes: Record<string, (input: ShortcodeRenderInput) => string>;
4
+ export declare const builtinShortcodeNames: readonly string[];
@@ -0,0 +1,6 @@
1
+ import type { Root } from "mdast";
2
+ import type { RemarkShortcodesOptions } from "./types.js";
3
+ export declare const SHORTCODES_SOURCE = "@riebeckite/plugin-shortcodes";
4
+ export declare const DIAGNOSTIC_UNKNOWN = "shortcodes-unknown";
5
+ export declare const DIAGNOSTIC_INVALID = "shortcodes-invalid";
6
+ export declare function remarkShortcodes(options?: RemarkShortcodesOptions): (tree: Root, file: unknown) => void;
@@ -0,0 +1,19 @@
1
+ import type { ContentSource, PluginRenderContext } from "@riebeckite/core";
2
+ import type { ResolvedShortcodeOptions, ShortcodeOptions, ShortcodeRenderRequest } from "./types.js";
3
+ export declare const DEFAULT_SHORTCODE_CLASS_NAME = "rb-shortcode";
4
+ /**
5
+ * Sentinel echoed through {@link ShortcodeRenderInput.childrenHtml}. The remark
6
+ * transform splits the rendered wrapper on this marker and splices the real
7
+ * container body in its place.
8
+ */
9
+ export declare const SHORTCODE_CHILDREN_MARKER = "\0rb-shortcodes:children\0";
10
+ export declare function resolveShortcodeOptions(options?: ShortcodeOptions): ResolvedShortcodeOptions;
11
+ export declare function createShortcodeRenderContext(input: {
12
+ name: string;
13
+ label?: string;
14
+ raw?: string;
15
+ url?: string;
16
+ contentIndex?: Map<string, string>;
17
+ contentSource?: ContentSource;
18
+ }): PluginRenderContext;
19
+ export declare function renderShortcode(request: ShortcodeRenderRequest, options: ResolvedShortcodeOptions): string;
@@ -0,0 +1,64 @@
1
+ import type { ContentSource, PluginRenderContext } from "@riebeckite/core";
2
+ import type { Parent } from "mdast";
3
+ /**
4
+ * Attributes parsed from a directive's `{ ... }` block. Values are always
5
+ * strings; shorthand `#id` / `.class` syntax is expanded by `remark-directive`.
6
+ */
7
+ export type ShortcodeAttributes = Record<string, string>;
8
+ /**
9
+ * Input passed to a {@link ShortcodeRenderer}.
10
+ *
11
+ * `childrenHtml` is present for container shortcodes. It contains a sentinel
12
+ * that the renderer must place where the container body belongs; the plugin
13
+ * swaps the sentinel for the rendered children. Renderers that need the body
14
+ * must echo `input.childrenHtml` verbatim.
15
+ */
16
+ export type ShortcodeRenderInput = {
17
+ name: string;
18
+ label: string;
19
+ attributes: ShortcodeAttributes;
20
+ childrenHtml?: string;
21
+ context: PluginRenderContext;
22
+ container: boolean;
23
+ };
24
+ export type ShortcodeRenderer = (input: ShortcodeRenderInput) => string;
25
+ export type ShortcodeOptions = {
26
+ /** Root CSS class applied to every wrapper. Defaults to `rb-shortcode`. */
27
+ className?: string;
28
+ /** Optional BCP-47 language tag applied to wrappers as `lang`. */
29
+ language?: string;
30
+ /** Register the built-in shortcode renderers. Defaults to `true`. */
31
+ builtins?: boolean;
32
+ /** Custom renderers, merged on top of the built-ins by name. */
33
+ shortcodes?: Record<string, ShortcodeRenderer>;
34
+ };
35
+ export type ResolvedShortcodeOptions = {
36
+ className: string;
37
+ language?: string;
38
+ builtins: boolean;
39
+ shortcodes: Record<string, ShortcodeRenderer>;
40
+ };
41
+ /** Input accepted by {@link renderShortcode}. */
42
+ export type ShortcodeRenderRequest = {
43
+ name: string;
44
+ label?: string;
45
+ attributes?: ShortcodeAttributes;
46
+ childrenHtml?: string;
47
+ container?: boolean;
48
+ context?: PluginRenderContext;
49
+ };
50
+ /**
51
+ * Options for the standalone remark transform. It mirrors
52
+ * {@link ShortcodeOptions} and additionally forwards pipeline state so the
53
+ * render context handed to renderers can expose the content index.
54
+ */
55
+ export type RemarkShortcodesOptions = ShortcodeOptions & {
56
+ contentIndex?: Map<string, string>;
57
+ contentSource?: ContentSource;
58
+ };
59
+ export type DirectiveName = "leafDirective" | "containerDirective";
60
+ export type DirectiveNode = Parent & {
61
+ type: DirectiveName;
62
+ name: string;
63
+ attributes?: Record<string, unknown>;
64
+ };
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@riebeckite/plugin-shortcodes",
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
+ "./style.css": "./style.css"
16
+ },
17
+ "dependencies": {
18
+ "@types/mdast": "^4.0.4",
19
+ "unist-util-visit": "^5.1.0",
20
+ "@riebeckite/core": "0.0.1"
21
+ },
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "files": [
26
+ "LICENSE",
27
+ "README.md",
28
+ "README_ja.md",
29
+ "dist",
30
+ "style.css"
31
+ ],
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "https://github.com/Rerurate514/riebeckite.git",
35
+ "directory": "packages/plugins/shortcodes"
36
+ },
37
+ "homepage": "https://github.com/Rerurate514/riebeckite#readme",
38
+ "bugs": {
39
+ "url": "https://github.com/Rerurate514/riebeckite/issues"
40
+ },
41
+ "description": "Remark directive shortcodes for Riebeckite Markdown.",
42
+ "keywords": [
43
+ "riebeckite",
44
+ "plugin",
45
+ "shortcodes",
46
+ "remark"
47
+ ],
48
+ "engines": {
49
+ "node": "^20.19.0 || >=22.12.0"
50
+ },
51
+ "sideEffects": [
52
+ "./style.css"
53
+ ],
54
+ "scripts": {
55
+ "build": "node ../../../scripts/build_package.mjs"
56
+ }
57
+ }
package/style.css ADDED
@@ -0,0 +1,252 @@
1
+ .rb-shortcode {
2
+ --rb-shortcode-accent: var(--rb-color-accent, #686868);
3
+ --rb-shortcode-ink: var(--rb-color-ink, #1a1a1a);
4
+ --rb-shortcode-rule: color-mix(
5
+ in srgb,
6
+ var(--rb-shortcode-ink) 20%,
7
+ transparent
8
+ );
9
+ --rb-shortcode-surface: color-mix(
10
+ in srgb,
11
+ var(--rb-shortcode-ink) 5%,
12
+ transparent
13
+ );
14
+ }
15
+
16
+ div.rb-shortcode {
17
+ display: block;
18
+ margin-block: 1.25rem;
19
+ }
20
+
21
+ /* figure ------------------------------------------------------------------ */
22
+
23
+ .rb-shortcode__figure {
24
+ display: flex;
25
+ flex-direction: column;
26
+ gap: 0.5rem;
27
+ margin: 0;
28
+ }
29
+
30
+ .rb-shortcode__image {
31
+ display: block;
32
+ max-width: 100%;
33
+ height: auto;
34
+ border: 1px solid var(--rb-shortcode-rule);
35
+ }
36
+
37
+ .rb-shortcode__caption {
38
+ color: var(--rb-shortcode-accent);
39
+ font-size: 0.875em;
40
+ line-height: 1.7;
41
+ }
42
+
43
+ .rb-shortcode__figure-fallback,
44
+ .rb-shortcode__video-fallback,
45
+ .rb-shortcode__gist-fallback,
46
+ .rb-shortcode__link-card-fallback,
47
+ .rb-shortcode__file-fallback {
48
+ color: var(--rb-shortcode-accent);
49
+ font-style: italic;
50
+ }
51
+
52
+ /* video ------------------------------------------------------------------- */
53
+
54
+ .rb-shortcode__video {
55
+ position: relative;
56
+ width: 100%;
57
+ aspect-ratio: 16 / 9;
58
+ overflow: hidden;
59
+ border: 1px solid var(--rb-shortcode-rule);
60
+ background: var(--rb-shortcode-surface);
61
+ }
62
+
63
+ .rb-shortcode__iframe {
64
+ position: absolute;
65
+ inset: 0;
66
+ width: 100%;
67
+ height: 100%;
68
+ border: 0;
69
+ }
70
+
71
+ /* gist -------------------------------------------------------------------- */
72
+
73
+ .rb-shortcode__gist {
74
+ overflow: hidden;
75
+ border: 1px solid var(--rb-shortcode-rule);
76
+ }
77
+
78
+ /* keyboard ---------------------------------------------------------------- */
79
+
80
+ .rb-shortcode__kbd {
81
+ display: inline-block;
82
+ padding: 0.1em 0.4em;
83
+ border: 1px solid var(--rb-shortcode-rule);
84
+ border-bottom-width: 2px;
85
+ border-radius: 4px;
86
+ background: var(--rb-shortcode-surface);
87
+ font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
88
+ font-size: 0.85em;
89
+ line-height: 1.4;
90
+ }
91
+
92
+ .rb-shortcode__kbd-separator {
93
+ margin-inline: 0.15em;
94
+ color: var(--rb-shortcode-accent);
95
+ }
96
+
97
+ /* badge ------------------------------------------------------------------- */
98
+
99
+ .rb-shortcode__badge {
100
+ display: inline-block;
101
+ padding: 0.1em 0.5em;
102
+ border: 1px solid currentcolor;
103
+ border-radius: 999px;
104
+ color: var(--rb-shortcode-accent);
105
+ font-size: 0.75em;
106
+ line-height: 1.6;
107
+ vertical-align: middle;
108
+ }
109
+
110
+ .rb-shortcode__badge--info {
111
+ color: #0b6bcb;
112
+ }
113
+
114
+ .rb-shortcode__badge--success {
115
+ color: #1a7f37;
116
+ }
117
+
118
+ .rb-shortcode__badge--warning {
119
+ color: #9a6700;
120
+ }
121
+
122
+ .rb-shortcode__badge--danger {
123
+ color: #cf222e;
124
+ }
125
+
126
+ /* details / spoiler ------------------------------------------------------- */
127
+
128
+ .rb-shortcode__details {
129
+ border: 1px solid var(--rb-shortcode-rule);
130
+ }
131
+
132
+ .rb-shortcode__summary {
133
+ padding: 0.5rem 0.75rem;
134
+ cursor: pointer;
135
+ font-weight: 600;
136
+ }
137
+
138
+ .rb-shortcode__details-body {
139
+ padding: 0.25rem 0.75rem 0.75rem;
140
+ }
141
+
142
+ /* note / callout ---------------------------------------------------------- */
143
+
144
+ .rb-shortcode__note {
145
+ padding: 0.75rem 1rem;
146
+ border-left: 4px solid var(--rb-shortcode-accent);
147
+ background: var(--rb-shortcode-surface);
148
+ }
149
+
150
+ .rb-shortcode__note-title {
151
+ margin: 0 0 0.35rem;
152
+ font-weight: 700;
153
+ }
154
+
155
+ .rb-shortcode__note-body > :first-child,
156
+ .rb-shortcode__details-body > :first-child {
157
+ margin-top: 0;
158
+ }
159
+
160
+ .rb-shortcode__note-body > :last-child,
161
+ .rb-shortcode__details-body > :last-child {
162
+ margin-bottom: 0;
163
+ }
164
+
165
+ .rb-shortcode__note--tip {
166
+ border-left-color: #1a7f37;
167
+ }
168
+
169
+ .rb-shortcode__note--warning {
170
+ border-left-color: #9a6700;
171
+ }
172
+
173
+ .rb-shortcode__note--danger {
174
+ border-left-color: #cf222e;
175
+ }
176
+
177
+ /* link card --------------------------------------------------------------- */
178
+
179
+ .rb-shortcode__link-card {
180
+ display: flex;
181
+ gap: 0.75rem;
182
+ padding: 0.75rem;
183
+ border: 1px solid var(--rb-shortcode-rule);
184
+ color: inherit;
185
+ text-decoration: none;
186
+ }
187
+
188
+ .rb-shortcode__link-card:hover,
189
+ .rb-shortcode__link-card:focus-visible {
190
+ border-color: var(--rb-shortcode-accent);
191
+ }
192
+
193
+ .rb-shortcode__link-card-image {
194
+ width: 96px;
195
+ height: 96px;
196
+ flex: none;
197
+ object-fit: cover;
198
+ }
199
+
200
+ .rb-shortcode__link-card-body {
201
+ display: flex;
202
+ flex-direction: column;
203
+ gap: 0.25rem;
204
+ min-width: 0;
205
+ }
206
+
207
+ .rb-shortcode__link-card-title {
208
+ font-weight: 700;
209
+ }
210
+
211
+ .rb-shortcode__link-card-description,
212
+ .rb-shortcode__link-card-url {
213
+ color: var(--rb-shortcode-accent);
214
+ font-size: 0.875em;
215
+ line-height: 1.6;
216
+ }
217
+
218
+ .rb-shortcode__link-card-url {
219
+ overflow: hidden;
220
+ text-overflow: ellipsis;
221
+ white-space: nowrap;
222
+ }
223
+
224
+ /* file -------------------------------------------------------------------- */
225
+
226
+ .rb-shortcode__file {
227
+ display: inline-flex;
228
+ align-items: center;
229
+ gap: 0.5rem;
230
+ padding: 0.35rem 0.75rem;
231
+ border: 1px solid var(--rb-shortcode-rule);
232
+ color: inherit;
233
+ text-decoration: none;
234
+ }
235
+
236
+ .rb-shortcode__file:hover,
237
+ .rb-shortcode__file:focus-visible {
238
+ border-color: var(--rb-shortcode-accent);
239
+ }
240
+
241
+ .rb-shortcode__file-size {
242
+ color: var(--rb-shortcode-accent);
243
+ font-size: 0.8125em;
244
+ }
245
+
246
+ /* fallback ---------------------------------------------------------------- */
247
+
248
+ .rb-shortcode--unknown {
249
+ color: var(--rb-shortcode-accent);
250
+ font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
251
+ font-size: 0.875em;
252
+ }