@md-plugins/md-plugin-containers 0.1.0-alpha.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.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024-present, Jeff Galbraith
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,166 @@
1
+ # @md-plugins/md-plugin-containers
2
+
3
+ A **Markdown-It** plugin that provides custom container support for enhanced Markdown rendering. Containers allow you to create stylized blocks with custom rendering logic, ideal for notes, warnings, callouts, and other visual elements.
4
+
5
+ ## Features
6
+
7
+ - Define custom containers with specific classes or components.
8
+ - Add titles to containers for better context (e.g., "Warning", "Note").
9
+ - Supports flexible rendering logic for different container types.
10
+ - Compatible with Markdown-It environments for additional customization.
11
+
12
+ ## Installation
13
+
14
+ Install the plugin via your preferred package manager:
15
+
16
+ ```bash
17
+ # With npm:
18
+ npm install @md-plugins/md-plugin-containers
19
+ # Or with Yarn:
20
+ yarn add @md-plugins/md-plugin-containers
21
+ # Or with pnpm:
22
+ pnpm add @md-plugins/md-plugin-containers
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ### Basic Setup
28
+
29
+ ```js
30
+ import MarkdownIt from 'markdown-it';
31
+ import { containersPlugin } from '@md-plugins/md-plugin-containers';
32
+
33
+ const md = new MarkdownIt();
34
+ md.use(containersPlugin, {
35
+ containers: [
36
+ { type: 'note', defaultTitle: 'Note' },
37
+ { type: 'warning', defaultTitle: 'Warning' },
38
+ ],
39
+ });
40
+
41
+ const markdownContent = `
42
+ :::note
43
+ This is a note.
44
+ :::
45
+
46
+ :::warning
47
+ This is a warning!
48
+ :::
49
+ `;
50
+
51
+ const renderedOutput = md.render(markdownContent);
52
+
53
+ console.log('Rendered Output:', renderedOutput);
54
+ ```
55
+
56
+ ### Example Output
57
+
58
+ The rendered output will look like this:
59
+
60
+ ```html
61
+ <div class="note">
62
+ <p>This is a note.</p>
63
+ </div>
64
+
65
+ <div class="warning">
66
+ <p>This is a warning!</p>
67
+ </div>
68
+ ```
69
+
70
+ ## Options
71
+
72
+ The `md-plugin-containers` plugin supports the following options:
73
+
74
+ | Option | Type | Default | Description |
75
+ | ---------- | --------------------------------------------- | ------- | ------------------------------------------------------- |
76
+ | containers | Array<{ type: string; defaultTitle: string }> | [] | List of containers with their types and default titles. |
77
+ | render | Function | null | Custom rendering function for containers. |
78
+
79
+ ## Defining Custom Containers
80
+
81
+ You can define custom containers with their own styles or components:
82
+
83
+ ```js
84
+ md.use(containersPlugin, {
85
+ containers: [
86
+ { type: 'tip', defaultTitle: 'Tip' },
87
+ { type: 'important', defaultTitle: 'Important' },
88
+ ],
89
+ });
90
+ ```
91
+
92
+ ## Advanced Usage
93
+
94
+ ### Custom Rendering Logic
95
+
96
+ Override the default rendering logic for containers:
97
+
98
+ ```js
99
+ md.use(containersPlugin, {
100
+ containers: [{ type: 'note', defaultTitle: 'Note' }],
101
+ render(tokens, idx) {
102
+ const token = tokens[idx];
103
+ if (token.nesting === 1) {
104
+ // Opening tag
105
+ const title = token.info.trim() || 'Note';
106
+ return `<div class="custom-note"><strong>${title}:</strong>\n`;
107
+ } else {
108
+ // Closing tag
109
+ return `</div>\n`;
110
+ }
111
+ },
112
+ });
113
+ ```
114
+
115
+ ## Adding Titles
116
+
117
+ Containers can include titles by default or allow custom titles to be specified:
118
+
119
+ ```markdown
120
+ :::note Custom Note Title
121
+ This is a custom note with a title.
122
+ :::
123
+ ```
124
+
125
+ Rendered Output:
126
+
127
+ ```html
128
+ <div class="note">
129
+ <strong>Custom Note Title</strong>
130
+ <p>This is a custom note with a title.</p>
131
+ </div>
132
+ ```
133
+
134
+ ## Nested Containers
135
+
136
+ Containers can be nested if your rendering logic supports it:
137
+
138
+ ```markdown
139
+ :::note Outer Note
140
+ :::warning Inner Warning
141
+ Be cautious!
142
+ ::::::
143
+ ```
144
+
145
+ Rendered Output:
146
+
147
+ ```html
148
+ <div class="note">
149
+ <p>Outer Note</p>
150
+ <div class="warning">
151
+ <p>Be cautious!</p>
152
+ </div>
153
+ </div>
154
+ ```
155
+
156
+ ## Testing
157
+
158
+ To run the tests for this plugin, use the following command:
159
+
160
+ ```bash
161
+ pnpm test
162
+ ```
163
+
164
+ ## License
165
+
166
+ This plugin is licensed under the MIT License. See the [LICENSE](LICENSE.md) file for details.
@@ -0,0 +1,64 @@
1
+ import MarkdownIt from 'markdown-it';
2
+ import Token from 'markdown-it/lib/token.mjs';
3
+ import container from 'markdown-it-container';
4
+
5
+ type Container = typeof container;
6
+ type CreateContainerFn = (container: Container, type: string, defaultTitle: string) => [Container, string, any?];
7
+ interface ContainerDetails {
8
+ type: string;
9
+ defaultTitle: string;
10
+ }
11
+ interface ContainerOptions {
12
+ render(tokens: Token[], idx: number): string;
13
+ }
14
+
15
+ /**
16
+ * Adds container support to a MarkdownIt instance.
17
+ *
18
+ * This function applies custom container plugins to the MarkdownIt parser.
19
+ *
20
+ * @param md - The MarkdownIt instance to which the container plugins will be added.
21
+ * @param containers - An array of ContainerDetails objects, each specifying a container type and its default title.
22
+ * @param createContainer - A function that creates and returns the container plugin configuration.
23
+ *
24
+ * @example
25
+ * const containers: ContainerDetails[] = [
26
+ * { type: 'warning', defaultTitle: 'Warning' },
27
+ * { type: 'tip', defaultTitle: 'Tip' },
28
+ * { type: 'details', defaultTitle: 'Details' },
29
+ * ];
30
+ *
31
+ * function createContainer(
32
+ * container: Container,
33
+ * containerType: string,
34
+ * defaultTitle: string
35
+ * ): [Container, string, ContainerOptions] {
36
+ * const containerTypeLen = containerType.length;
37
+ *
38
+ * return [
39
+ * container,
40
+ * containerType,
41
+ * {
42
+ * render(tokens: Token[], idx: number): string {
43
+ * const token = tokens[idx];
44
+ * const title =
45
+ * token.info.trim().slice(containerTypeLen).trim() || defaultTitle;
46
+ *
47
+ * if (containerType === 'details') {
48
+ * return token.nesting === 1
49
+ * ? `<details class="markdown-note markdown-note--${containerType}"><summary class="markdown-note__title">${title}</summary>\n`
50
+ * : '</details>\n';
51
+ * }
52
+ *
53
+ * return token.nesting === 1
54
+ * ? `<div class="markdown-note markdown-note--${containerType}"><p class="markdown-note__title">${title}</p>\n`
55
+ * : '</div>\n';
56
+ * },
57
+ * },
58
+ * ];
59
+ * }
60
+ *
61
+ */
62
+ declare function containersPlugin(md: MarkdownIt, containers: ContainerDetails[], createContainer: CreateContainerFn): void;
63
+
64
+ export { type Container, type ContainerDetails, type ContainerOptions, type CreateContainerFn, containersPlugin };
@@ -0,0 +1,64 @@
1
+ import MarkdownIt from 'markdown-it';
2
+ import Token from 'markdown-it/lib/token.mjs';
3
+ import container from 'markdown-it-container';
4
+
5
+ type Container = typeof container;
6
+ type CreateContainerFn = (container: Container, type: string, defaultTitle: string) => [Container, string, any?];
7
+ interface ContainerDetails {
8
+ type: string;
9
+ defaultTitle: string;
10
+ }
11
+ interface ContainerOptions {
12
+ render(tokens: Token[], idx: number): string;
13
+ }
14
+
15
+ /**
16
+ * Adds container support to a MarkdownIt instance.
17
+ *
18
+ * This function applies custom container plugins to the MarkdownIt parser.
19
+ *
20
+ * @param md - The MarkdownIt instance to which the container plugins will be added.
21
+ * @param containers - An array of ContainerDetails objects, each specifying a container type and its default title.
22
+ * @param createContainer - A function that creates and returns the container plugin configuration.
23
+ *
24
+ * @example
25
+ * const containers: ContainerDetails[] = [
26
+ * { type: 'warning', defaultTitle: 'Warning' },
27
+ * { type: 'tip', defaultTitle: 'Tip' },
28
+ * { type: 'details', defaultTitle: 'Details' },
29
+ * ];
30
+ *
31
+ * function createContainer(
32
+ * container: Container,
33
+ * containerType: string,
34
+ * defaultTitle: string
35
+ * ): [Container, string, ContainerOptions] {
36
+ * const containerTypeLen = containerType.length;
37
+ *
38
+ * return [
39
+ * container,
40
+ * containerType,
41
+ * {
42
+ * render(tokens: Token[], idx: number): string {
43
+ * const token = tokens[idx];
44
+ * const title =
45
+ * token.info.trim().slice(containerTypeLen).trim() || defaultTitle;
46
+ *
47
+ * if (containerType === 'details') {
48
+ * return token.nesting === 1
49
+ * ? `<details class="markdown-note markdown-note--${containerType}"><summary class="markdown-note__title">${title}</summary>\n`
50
+ * : '</details>\n';
51
+ * }
52
+ *
53
+ * return token.nesting === 1
54
+ * ? `<div class="markdown-note markdown-note--${containerType}"><p class="markdown-note__title">${title}</p>\n`
55
+ * : '</div>\n';
56
+ * },
57
+ * },
58
+ * ];
59
+ * }
60
+ *
61
+ */
62
+ declare function containersPlugin(md: MarkdownIt, containers: ContainerDetails[], createContainer: CreateContainerFn): void;
63
+
64
+ export { type Container, type ContainerDetails, type ContainerOptions, type CreateContainerFn, containersPlugin };
package/dist/index.mjs ADDED
@@ -0,0 +1,22 @@
1
+ import container from 'markdown-it-container';
2
+
3
+ function containersPlugin(md, containers, createContainer) {
4
+ if (!Array.isArray(containers) || containers.length === 0) {
5
+ console.warn("No containers provided to containersPlugin.");
6
+ return;
7
+ }
8
+ if (typeof createContainer !== "function") {
9
+ throw new Error(
10
+ "Invalid createContainer function provided to containersPlugin."
11
+ );
12
+ }
13
+ containers.forEach(({ type, defaultTitle }) => {
14
+ try {
15
+ md.use(...createContainer(container, type, defaultTitle));
16
+ } catch (error) {
17
+ console.error(`Failed to create container for type: ${type}`, error);
18
+ }
19
+ });
20
+ }
21
+
22
+ export { containersPlugin };
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@md-plugins/md-plugin-containers",
3
+ "version": "0.1.0-alpha.1",
4
+ "description": "A markdown-it plugin for handling custom containers.",
5
+ "keywords": [
6
+ "markdown-it",
7
+ "quasarframework",
8
+ "vue",
9
+ "types"
10
+ ],
11
+ "homepage": "https://github.com/md-plugins",
12
+ "bugs": {
13
+ "url": "https://github.com/md-plugins/md-plugins/issues"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/md-plugins/md-plugins.git"
18
+ },
19
+ "license": "MIT",
20
+ "author": "hawkeye64 <galbraith64@gmail.com>",
21
+ "type": "module",
22
+ "exports": {
23
+ ".": {
24
+ "import": {
25
+ "types": "./dist/index.d.mts",
26
+ "default": "./dist/index.mjs"
27
+ }
28
+ }
29
+ },
30
+ "module": "./dist/index.mjs",
31
+ "types": "./dist/index.d.ts",
32
+ "files": [
33
+ "./dist"
34
+ ],
35
+ "dependencies": {
36
+ "@types/markdown-it": "^14.1.2",
37
+ "markdown-it": "^14.1.0",
38
+ "markdown-it-container": "^4.0.0",
39
+ "@md-plugins/shared": "0.1.0-alpha.1"
40
+ },
41
+ "publishConfig": {
42
+ "access": "public"
43
+ },
44
+ "devDependencies": {
45
+ "@types/markdown-it-container": "^2.0.10"
46
+ },
47
+ "scripts": {
48
+ "build": "unbuild",
49
+ "clean": "rm -rf dist",
50
+ "test": "vitest"
51
+ }
52
+ }