@lvl99dotcom/edaha-plugin-toc 0.0.0-stage → 0.4.0

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 [2026] [Matt Scheurich]
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,48 @@
1
- # Temporary Holding Version
1
+ # @lvl99dotcom/edaha-plugin-toc
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ An [edaha.md](https://github.com/lvl99/edaha.md#readme) embed plugin for tables of contents. It lists the article's headings (not its title, nor headings in other embed blocks) as nested ordered lists of links, built with the page, without JavaScript.
4
+
5
+ ```sh
6
+ npm add @lvl99dotcom/edaha-plugin-toc --save-dev
7
+ yarn add @lvl99dotcom/edaha-plugin-toc -D
8
+ pnpm add @lvl99dotcom/edaha-plugin-toc -D
9
+ ```
10
+
11
+ ```yaml
12
+ # edahaconfig.yml
13
+ plugins:
14
+ - "@lvl99dotcom/edaha-plugin-toc"
15
+ ```
16
+
17
+ <!-- prettier-ignore-start -->
18
+ ```md
19
+ <!-- embed:toc -->
20
+ <!--yaml
21
+ level2: number
22
+ level3: alpha
23
+ -->
24
+ ## Contents
25
+ <!-- /embed:toc -->
26
+ ```
27
+ <!-- prettier-ignore-end -->
28
+
29
+ Markdown in the block (optional) is shown above the table of contents.
30
+
31
+ Headings nest by level: a heading goes under the nearest heading before it with a lower level, so a skipped level (e.g. an `<h4>` after an `<h2>`) adds no empty item. Each level is marked in its own list style, with the options `level1` to `level6`:
32
+
33
+ | Style | Marker |
34
+ | -------------- | ------------------------------------------------------- |
35
+ | `number` | 1. 2. 3. |
36
+ | `number-depth` | its parent's marker, then its number, e.g. 1.1. or b.2. |
37
+ | `alpha` | a. b. c. |
38
+ | `number-roman` | i. ii. iii. |
39
+ | `disc` | • |
40
+ | `circle` | ◦ |
41
+ | `square` | ▪ |
42
+ | `diamond` | ⬩ |
43
+
44
+ By default: `level1` number, `level2` and `level3` number-depth, `level4` alpha, `level5` number-roman, `level6` circle.
45
+
46
+ Your layout template needs `{% embedHead %}` before `</head>` (the default theme has it). The editor's embed frames show one block on its own, so they show a note instead of the list; previews show it. See [Embeds](https://edaha.md/docs/07-embeds/) in the docs.
47
+
48
+ Licensed under the [Apache License 2.0](LICENSE).
@@ -0,0 +1,108 @@
1
+ import type { EmbedHeading } from "@lvl99dotcom/edaha.md/embeds";
2
+ /** How a level of the table of contents is marked, with the option's title in the embed dialog */
3
+ export declare const LIST_STYLES: {
4
+ readonly number: "Number (1.)";
5
+ readonly "number-depth": "Number with its parents' (1.1.)";
6
+ readonly alpha: "Letter (a.)";
7
+ readonly "number-roman": "Roman numeral (i.)";
8
+ readonly disc: "Disc (•)";
9
+ readonly circle: "Circle (◦)";
10
+ readonly square: "Square (▪)";
11
+ readonly diamond: "Diamond (⬩)";
12
+ };
13
+ /** A list style (see LIST_STYLES) */
14
+ export type ListStyle = keyof typeof LIST_STYLES;
15
+ /** The list style of each heading level by default, from `<h1>` to `<h6>` */
16
+ export declare const DEFAULT_LIST_STYLES: readonly ListStyle[];
17
+ /**
18
+ * A positive number in lowercase letters, going on to two letters after z.
19
+ *
20
+ * @example
21
+ * alphaNumber(2); // "b"
22
+ * alphaNumber(28); // "ab"
23
+ */
24
+ export declare const alphaNumber: (position: number) => string;
25
+ /**
26
+ * A positive number in lowercase roman numerals.
27
+ *
28
+ * @example
29
+ * romanNumber(4); // "iv"
30
+ * romanNumber(1994); // "mcmxciv"
31
+ */
32
+ export declare const romanNumber: (position: number) => string;
33
+ /** A heading in the table of contents, with its marker and the headings under it */
34
+ export interface TocItem {
35
+ heading: EmbedHeading;
36
+ /** What it is marked with, e.g. "b.1." or "◦" */
37
+ marker: string;
38
+ /** Whether the marker is only decoration (a bullet), so screen readers skip it */
39
+ bullet: boolean;
40
+ /** What `number-depth` markers under it start with, e.g. "b.1" (a bullet passes on its parent's) */
41
+ prefix: string;
42
+ children: TocItem[];
43
+ }
44
+ /**
45
+ * A heading's marker in its list style, from its position among the headings beside it and its parent's prefix (see
46
+ * TocItem).
47
+ *
48
+ * @example
49
+ * tocMarker("number-depth", 2, "b"); // { marker: "b.2.", bullet: false, prefix: "b.2" }
50
+ * tocMarker("circle", 2, "b"); // { marker: "◦", bullet: true, prefix: "b" }
51
+ */
52
+ export declare const tocMarker: (style: ListStyle, position: number, parentPrefix: string) => Pick<TocItem, "marker" | "bullet" | "prefix">;
53
+ /**
54
+ * Nest headings by their level, each under the nearest heading before it with a lower level (so a skipped level adds
55
+ * no empty item), marked in their level's list style.
56
+ *
57
+ * @param styles - the list style of each level, from `<h1>` to `<h6>`
58
+ * @example
59
+ * tocTree([{ level: 2, text: "A", id: "a" }, { level: 4, text: "B", id: "b" }], DEFAULT_LIST_STYLES);
60
+ * // [{ heading: A, marker: "1.", ..., children: [{ heading: B, marker: "a.", ... }] }]
61
+ */
62
+ export declare const tocTree: (headings: readonly EmbedHeading[], styles: readonly ListStyle[]) => TocItem[];
63
+ /**
64
+ * Nested ordered lists of links to headings. Markers are text, so they are the same in every browser; `role="list"`
65
+ * keeps the list's meaning for screen readers that drop it from lists without CSS markers.
66
+ *
67
+ * @example
68
+ * tocList(tocTree([{ level: 2, text: "A", id: "a" }], DEFAULT_LIST_STYLES));
69
+ * // '<ol role="list"><li><span class="toc-marker">1.</span> <a href="#a">A</a></li></ol>'
70
+ */
71
+ export declare const tocList: (items: readonly TocItem[]) => string;
72
+ /**
73
+ * A table of contents of the article's headings (not its title, nor headings in embed blocks), as nested ordered
74
+ * lists of links, under the block's own Markdown (if any):
75
+ *
76
+ * ```
77
+ * <!-- embed:toc -->
78
+ * <!--yaml
79
+ * level2: number
80
+ * -->
81
+ * ## Contents
82
+ * <!-- /embed:toc -->
83
+ * ```
84
+ *
85
+ * Headings nest by level, and each level is marked in its own list style (options `level1` to `level6`; see
86
+ * LIST_STYLES and DEFAULT_LIST_STYLES). The editor's embed frames don't show the article, so they show a note instead.
87
+ *
88
+ * @example
89
+ * await toc.build.render("", { headings: [{ level: 2, text: "A", id: "a" }], options: { level2: "number" }, ... });
90
+ * // '<nav aria-label="Table of contents"><ol role="list"><li><span class="toc-marker">1.</span> <a href="#a">A</a></li></ol></nav>'
91
+ */
92
+ export declare const toc: {
93
+ key: string;
94
+ name: string;
95
+ description: string;
96
+ info: string;
97
+ contentOptional: true;
98
+ options: import("@lvl99dotcom/edaha.md/generator/metadata").MetadataDefinitions;
99
+ build: {
100
+ render: (content: string, { headings, options }: import("@lvl99dotcom/edaha.md/embeds").EmbedRenderContext) => Promise<string>;
101
+ renderHead: () => Promise<string>;
102
+ };
103
+ editor: {
104
+ icon: string;
105
+ render: (content: string) => Promise<string>;
106
+ };
107
+ };
108
+ export default toc;
package/dist/index.js ADDED
@@ -0,0 +1,208 @@
1
+ import { escapeHtml } from "@lvl99dotcom/edaha.md/generator/format";
2
+ import { createMarkdown, renderMarkdown, } from "@lvl99dotcom/edaha.md/generator/markdown";
3
+ /** How a level of the table of contents is marked, with the option's title in the embed dialog */
4
+ export const LIST_STYLES = {
5
+ number: "Number (1.)",
6
+ "number-depth": "Number with its parents' (1.1.)",
7
+ alpha: "Letter (a.)",
8
+ "number-roman": "Roman numeral (i.)",
9
+ disc: "Disc (•)",
10
+ circle: "Circle (◦)",
11
+ square: "Square (▪)",
12
+ diamond: "Diamond (⬩)",
13
+ };
14
+ /** The marks of the list styles that aren't counted: disc, circle and square as browsers mark nested bulleted lists */
15
+ const BULLETS = {
16
+ disc: "•",
17
+ circle: "◦",
18
+ square: "▪",
19
+ diamond: "⬩",
20
+ };
21
+ /** The list style of each heading level by default, from `<h1>` to `<h6>` */
22
+ export const DEFAULT_LIST_STYLES = [
23
+ "number",
24
+ "number-depth",
25
+ "number-depth",
26
+ "alpha",
27
+ "number-roman",
28
+ "circle",
29
+ ];
30
+ /** Roman numerals' values, largest first */
31
+ const ROMAN_NUMERALS = [
32
+ [1000, "m"],
33
+ [900, "cm"],
34
+ [500, "d"],
35
+ [400, "cd"],
36
+ [100, "c"],
37
+ [90, "xc"],
38
+ [50, "l"],
39
+ [40, "xl"],
40
+ [10, "x"],
41
+ [9, "ix"],
42
+ [5, "v"],
43
+ [4, "iv"],
44
+ [1, "i"],
45
+ ];
46
+ /**
47
+ * A positive number in lowercase letters, going on to two letters after z.
48
+ *
49
+ * @example
50
+ * alphaNumber(2); // "b"
51
+ * alphaNumber(28); // "ab"
52
+ */
53
+ export const alphaNumber = (position) => {
54
+ let letters = "";
55
+ for (let rest = position; rest > 0; rest = Math.floor((rest - 1) / 26))
56
+ letters = String.fromCharCode(97 + ((rest - 1) % 26)) + letters;
57
+ return letters;
58
+ };
59
+ /**
60
+ * A positive number in lowercase roman numerals.
61
+ *
62
+ * @example
63
+ * romanNumber(4); // "iv"
64
+ * romanNumber(1994); // "mcmxciv"
65
+ */
66
+ export const romanNumber = (position) => {
67
+ let numerals = "";
68
+ let rest = position;
69
+ for (const [value, numeral] of ROMAN_NUMERALS)
70
+ for (; rest >= value; rest -= value)
71
+ numerals += numeral;
72
+ return numerals;
73
+ };
74
+ /**
75
+ * A heading's marker in its list style, from its position among the headings beside it and its parent's prefix (see
76
+ * TocItem).
77
+ *
78
+ * @example
79
+ * tocMarker("number-depth", 2, "b"); // { marker: "b.2.", bullet: false, prefix: "b.2" }
80
+ * tocMarker("circle", 2, "b"); // { marker: "◦", bullet: true, prefix: "b" }
81
+ */
82
+ export const tocMarker = (style, position, parentPrefix) => {
83
+ const bullet = BULLETS[style];
84
+ if (bullet)
85
+ return { marker: bullet, bullet: true, prefix: parentPrefix };
86
+ const own = style === "alpha"
87
+ ? alphaNumber(position)
88
+ : style === "number-roman"
89
+ ? romanNumber(position)
90
+ : style === "number-depth" && parentPrefix
91
+ ? `${parentPrefix}.${position}`
92
+ : String(position);
93
+ return { marker: `${own}.`, bullet: false, prefix: own };
94
+ };
95
+ /**
96
+ * Nest headings by their level, each under the nearest heading before it with a lower level (so a skipped level adds
97
+ * no empty item), marked in their level's list style.
98
+ *
99
+ * @param styles - the list style of each level, from `<h1>` to `<h6>`
100
+ * @example
101
+ * tocTree([{ level: 2, text: "A", id: "a" }, { level: 4, text: "B", id: "b" }], DEFAULT_LIST_STYLES);
102
+ * // [{ heading: A, marker: "1.", ..., children: [{ heading: B, marker: "a.", ... }] }]
103
+ */
104
+ export const tocTree = (headings, styles) => {
105
+ const root = [];
106
+ const parents = [];
107
+ for (const heading of headings) {
108
+ while ((parents.at(-1)?.heading.level ?? 0) >= heading.level)
109
+ parents.pop();
110
+ const parent = parents.at(-1);
111
+ const siblings = parent?.children ?? root;
112
+ const item = {
113
+ heading,
114
+ ...tocMarker(styles[heading.level - 1] ?? "number", siblings.length + 1, parent?.prefix ?? ""),
115
+ children: [],
116
+ };
117
+ siblings.push(item);
118
+ parents.push(item);
119
+ }
120
+ return root;
121
+ };
122
+ /**
123
+ * Nested ordered lists of links to headings. Markers are text, so they are the same in every browser; `role="list"`
124
+ * keeps the list's meaning for screen readers that drop it from lists without CSS markers.
125
+ *
126
+ * @example
127
+ * tocList(tocTree([{ level: 2, text: "A", id: "a" }], DEFAULT_LIST_STYLES));
128
+ * // '<ol role="list"><li><span class="toc-marker">1.</span> <a href="#a">A</a></li></ol>'
129
+ */
130
+ export const tocList = (items) => items.length
131
+ ? `<ol role="list">${items
132
+ .map(({ heading, marker, bullet, children }) => `<li><span class="toc-marker"${bullet ? ' aria-hidden="true"' : ""}>${marker}</span> <a href="#${escapeHtml(heading.id)}">${escapeHtml(heading.text)}</a>${tocList(children)}</li>`)
133
+ .join("")}</ol>`
134
+ : "";
135
+ /** The list style options, `level1` to `level6`, with their defaults */
136
+ const OPTIONS = Object.fromEntries(DEFAULT_LIST_STYLES.map((style, i) => [
137
+ `level${i + 1}`,
138
+ {
139
+ title: `Level ${i + 1} list style`,
140
+ description: `How headings of level ${i + 1} (<h${i + 1}>) are marked`,
141
+ type: "string",
142
+ enum: Object.keys(LIST_STYLES),
143
+ $edahaField: {
144
+ select: {
145
+ options: Object.entries(LIST_STYLES).map(([value, title]) => ({
146
+ value,
147
+ title,
148
+ })),
149
+ },
150
+ defaultValue: style,
151
+ },
152
+ },
153
+ ]));
154
+ /** Renders the Markdown written in a block, above its list */
155
+ const markdown = createMarkdown();
156
+ /** The block's own Markdown, rendered (relative URLs are left as they are, as the article's URL isn't known) */
157
+ const introHtml = (content) => content.trim() ? renderMarkdown(markdown, content, "") : "";
158
+ /** Lists indented by level, with their markers in front of the links */
159
+ const STYLE = `<style>
160
+ .embed-toc ol { list-style: none; margin: 0; padding-left: 1.5em; }
161
+ .embed-toc nav > ol { padding-left: 0; }
162
+ .embed-toc li { margin: 0.25em 0; }
163
+ .embed-toc .toc-marker { font-variant-numeric: tabular-nums; }
164
+ </style>`;
165
+ /** A table of contents, as the editor's icon */
166
+ const ICON = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -960 960 960" aria-hidden="true"><path d="M120-280v-80h560v80H120Zm0-160v-80h560v80H120Zm0-160v-80h560v80H120Zm680 320q-17 0-28.5-11.5T760-320q0-17 11.5-28.5T800-360q17 0 28.5 11.5T840-320q0 17-11.5 28.5T800-280Zm0-160q-17 0-28.5-11.5T760-480q0-17 11.5-28.5T800-520q17 0 28.5 11.5T840-480q0 17-11.5 28.5T800-440Zm0-160q-17 0-28.5-11.5T760-640q0-17 11.5-28.5T800-680q17 0 28.5 11.5T840-640q0 17-11.5 28.5T800-600Z"/></svg>';
167
+ /**
168
+ * A table of contents of the article's headings (not its title, nor headings in embed blocks), as nested ordered
169
+ * lists of links, under the block's own Markdown (if any):
170
+ *
171
+ * ```
172
+ * <!-- embed:toc -->
173
+ * <!--yaml
174
+ * level2: number
175
+ * -->
176
+ * ## Contents
177
+ * <!-- /embed:toc -->
178
+ * ```
179
+ *
180
+ * Headings nest by level, and each level is marked in its own list style (options `level1` to `level6`; see
181
+ * LIST_STYLES and DEFAULT_LIST_STYLES). The editor's embed frames don't show the article, so they show a note instead.
182
+ *
183
+ * @example
184
+ * await toc.build.render("", { headings: [{ level: 2, text: "A", id: "a" }], options: { level2: "number" }, ... });
185
+ * // '<nav aria-label="Table of contents"><ol role="list"><li><span class="toc-marker">1.</span> <a href="#a">A</a></li></ol></nav>'
186
+ */
187
+ export const toc = {
188
+ key: "toc",
189
+ name: "Table of contents",
190
+ description: "A table of contents of the article's headings.",
191
+ info: "Optional; Markdown shown above the table of contents, e.g. a heading",
192
+ contentOptional: true,
193
+ options: OPTIONS,
194
+ build: {
195
+ render: async (content, { headings, options }) => {
196
+ const styles = DEFAULT_LIST_STYLES.map((style, i) => options[`level${i + 1}`] ?? style);
197
+ const list = tocList(tocTree(headings, styles));
198
+ return (introHtml(content) +
199
+ (list ? `<nav aria-label="Table of contents">${list}</nav>` : ""));
200
+ },
201
+ renderHead: async () => STYLE,
202
+ },
203
+ editor: {
204
+ icon: ICON,
205
+ render: async (content) => `${introHtml(content)}<p><em>Table of contents: the article's headings are listed here on the website.</em></p>`,
206
+ },
207
+ };
208
+ export default toc;
package/package.json CHANGED
@@ -1,6 +1,49 @@
1
1
  {
2
2
  "name": "@lvl99dotcom/edaha-plugin-toc",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.4.0",
4
+ "description": "Tables of contents of articles' headings in edaha.md sites.",
5
+ "keywords": [
6
+ "edaha.md",
7
+ "edaha-plugin",
8
+ "embed",
9
+ "table of contents",
10
+ "toc"
11
+ ],
12
+ "author": "Matt Scheurich <matt@lvl99.com>",
13
+ "license": "Apache-2.0",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/lvl99/edaha.md.git",
17
+ "directory": "packages/edaha-plugin-toc"
18
+ },
19
+ "homepage": "https://github.com/lvl99/edaha.md#readme",
20
+ "bugs": "https://github.com/lvl99/edaha.md/issues",
21
+ "type": "module",
22
+ "engines": {
23
+ "node": ">=22"
24
+ },
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/index.d.ts",
28
+ "default": "./dist/index.js"
29
+ },
30
+ "./package.json": "./package.json"
31
+ },
32
+ "files": [
33
+ "dist",
34
+ "README.md",
35
+ "LICENSE"
36
+ ],
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "peerDependencies": {
41
+ "@lvl99dotcom/edaha.md": "^0.4.0"
42
+ },
43
+ "devDependencies": {
44
+ "@lvl99dotcom/edaha.md": "^0.4.0"
45
+ },
46
+ "scripts": {
47
+ "build": "node ../edaha/scripts/build-plugin.ts"
48
+ }
6
49
  }